SourceFile attribute

The name of the source file, which stack traces print next to the line number..

Appears on
class
Since
class file 45.0 (Java 1.0)
javac writes it
by default (not with -g:none)
Needed by the JVM
no: it is for the class libraries (reflection), compilers or tools

Layout

TypeFieldWhat it holds
u2attribute_name_indexa Utf8: "SourceFile"
u4attribute_lengthbytes after this field
u2sourcefile_indexa Utf8: the file name, without a path

What it is for

The name of the source file, which stack traces print next to the line number.

How common it is

JDK 25.0.5's java.base has 7,382 SourceFile attributes, in 7,382 of its 7,391 classes (7,382 on classs).

A real one, byte by byte

From Hello.class (compiled by javac 25), on the class:

OffsetBytesFieldValue
10398 bytesSourceFile
103900 29attribute_name_index#41
104100 00 00 02attribute_length2
104500 2asourcefile_index#42

Open in the viewer

Specification: JVMS §4.7, The SourceFile Attribute (Java SE 25).