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
| Type | Field | What it holds |
|---|---|---|
u2 | attribute_name_index | a Utf8: "SourceFile" |
u4 | attribute_length | bytes after this field |
u2 | sourcefile_index | a 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:
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 1039 | 8 bytes | SourceFile | |
| 1039 | 00 29 | attribute_name_index | #41 |
| 1041 | 00 00 00 02 | attribute_length | 2 |
| 1045 | 00 2a | sourcefile_index | #42 |
Specification: JVMS §4.7, The SourceFile Attribute (Java SE 25).