Java .class 文件的语言命名约定
Java Language naming convention for .class files
我了解到 here,即使这是最佳实践并且尽管大多数编译器都要求您这样做,但 .class
文件以 [=25= 命名并不是那么严格] class 它们包含,至少从语言规范的角度来看是这样。
我搜索了 JLS 和 JVM 规范,但找不到解决该主题的实际章节。我觉得这个问题根本没有得到处理 .
似乎很奇怪
Oracle 官方文档如果存在,哪里可以证实或揭穿以下声明?
Giving .class
files the same name as the public class they contain - whether they do - is simply a must, but according to the JLS this is not that strict. From the JLS's perspective, it is left to the compiler to choose whether to set such a restriction or not.
这在 JLS §13.1., The Form of a Binary 中得到解决:
Programs must be compiled either into the class
file format specified by The Java Virtual Machine Specification, Java SE 8 Edition, or into a representation that can be mapped into that format by a class loader written in the Java programming language.
所以您根本不需要将编译后的代码作为 .class
文件交付,只要您有一个 class 加载程序实现能够加载您的代码表示。请注意,在这个级别上,例如将映射数据传递给 ClassLoader.defineClass
时,class 文件的文件名根本不存在。 JLS 和 JVM 这两个规范始终使用术语“class 文件”作为“class 文件格式中的字节序列”的同义词,而不是“文件系统或 zip 存档中的条目”因此,永远不要提及文件名。
这也符合 Java 9 的发展方向,交付 class 库是一种自定义的、可能优化的库格式,而不是一个充满 .class
文件的 zip 容器。
不过,非常不鼓励以 .class
结尾命名包含不同表示的文件。
您引用的陈述有点奇怪。它以
开头
Giving .class
files the same name as the public class they contain is simply a must,…
但是 .class
文件根本不需要包含 public
class。在当前形式中,它最多包含一个 class,不一定是 public
。但它也可能包含一个虚拟 class 来传递 package
元信息,或者以 Java 9 开头的模块规范。
另外,为了能被默认查找规则找到,例如使用 URLClassLoader
,您没有为 .class
文件提供所包含的 class 的名称,而是使用其短名称并将其放置在从 structure/path 派生的目录中其限定名称的包组件。
但是第二部
… but according to the JLS this is not that strict. From the JLS's perspective, it is left to the compiler to choose whether to set such a restriction or not.
是正确的。如上所述,甚至不需要传送 class 个文件。比较
JLS §1., Introduction:
The Java programming language is normally compiled to the bytecode instruction set and binary format defined in The Java Virtual Machine Specification, Java SE 8 Edition.
注意单词“通常”
和
JVMS §2.1., The class File Format:
Compiled code to be executed by the Java Virtual Machine is represented using a hardware- and operating system-independent binary format, typically (but not necessarily) stored in a file, known as the class
file format.
“通常(但不一定)”…
我了解到 here,即使这是最佳实践并且尽管大多数编译器都要求您这样做,但 .class
文件以 [=25= 命名并不是那么严格] class 它们包含,至少从语言规范的角度来看是这样。
我搜索了 JLS 和 JVM 规范,但找不到解决该主题的实际章节。我觉得这个问题根本没有得到处理 .
似乎很奇怪Oracle 官方文档如果存在,哪里可以证实或揭穿以下声明?
Giving
.class
files the same name as the public class they contain - whether they do - is simply a must, but according to the JLS this is not that strict. From the JLS's perspective, it is left to the compiler to choose whether to set such a restriction or not.
这在 JLS §13.1., The Form of a Binary 中得到解决:
Programs must be compiled either into the
class
file format specified by The Java Virtual Machine Specification, Java SE 8 Edition, or into a representation that can be mapped into that format by a class loader written in the Java programming language.
所以您根本不需要将编译后的代码作为 .class
文件交付,只要您有一个 class 加载程序实现能够加载您的代码表示。请注意,在这个级别上,例如将映射数据传递给 ClassLoader.defineClass
时,class 文件的文件名根本不存在。 JLS 和 JVM 这两个规范始终使用术语“class 文件”作为“class 文件格式中的字节序列”的同义词,而不是“文件系统或 zip 存档中的条目”因此,永远不要提及文件名。
这也符合 Java 9 的发展方向,交付 class 库是一种自定义的、可能优化的库格式,而不是一个充满 .class
文件的 zip 容器。
不过,非常不鼓励以 .class
结尾命名包含不同表示的文件。
您引用的陈述有点奇怪。它以
开头Giving
.class
files the same name as the public class they contain is simply a must,…
但是 .class
文件根本不需要包含 public
class。在当前形式中,它最多包含一个 class,不一定是 public
。但它也可能包含一个虚拟 class 来传递 package
元信息,或者以 Java 9 开头的模块规范。
另外,为了能被默认查找规则找到,例如使用 URLClassLoader
,您没有为 .class
文件提供所包含的 class 的名称,而是使用其短名称并将其放置在从 structure/path 派生的目录中其限定名称的包组件。
但是第二部
… but according to the JLS this is not that strict. From the JLS's perspective, it is left to the compiler to choose whether to set such a restriction or not.
是正确的。如上所述,甚至不需要传送 class 个文件。比较
JLS §1., Introduction:The Java programming language is normally compiled to the bytecode instruction set and binary format defined in The Java Virtual Machine Specification, Java SE 8 Edition.
注意单词“通常”
和
JVMS §2.1., The class File Format:Compiled code to be executed by the Java Virtual Machine is represented using a hardware- and operating system-independent binary format, typically (but not necessarily) stored in a file, known as the
class
file format.
“通常(但不一定)”…