简体   繁体   English

为什么编译器需要.java后缀,但解释器不需要.class后缀?

[英]Why compiler needs .java suffix but interpreter doesn't need .class suffix?

To compile Foo.java: javac Foo.java 编译Foo.java:javac Foo.java

To run the program : java Foo 运行程序: java Foo

Why compiler needs .java suffix but interpreter doesn't need .class suffix? 为什么编译器需要.java后缀,但解释器不需要.class后缀?

As a couple of other answers have explained, the Java compiler takes a file name as an argument, whereas the interpreter takes a class name. 正如其他几个答案所解释的那样,Java编译器采用文件名作为参数,而解释器采用类名。 So you give the .java extension to the compiler because it's part of the file name, but you don't give it to the interpreter because it's not part of the class name. 因此,您将.java扩展名赋予编译器,因为它是文件名的一部分,但不将其提供给解释器,因为它不是类名的一部分。

But then, you might wonder, why didn't they just design the Java interpreter differently so that it would take a file name? 但是,然后,您可能想知道,为什么他们不只是以不同的方式设计Java解释器,以便采用文件名? The answer to that is that classes are not always loaded from .class files. 答案是,并非总是从.class文件中加载.class Sometimes they come from JAR archives, sometimes they come from the internet , sometimes they are constructed on the fly by a program, and so on. 有时它们来自JAR档案,有时它们来自互联网 ,有时它们是由程序动态构建的,依此类推。 A class could come from any source that can provide the binary data needed to define it . 一个类可以来自任何可以提供定义它的二进制数据的来源。 Perhaps the same class could have different implementations from different sources, for example a program might try to load the most up-to-date version of some class from a URL, but would fall back to a local file if that failed. 也许同一个类可能具有来自不同来源的不同实现,例如,程序可能尝试从URL加载某个类的最新版本,但是如果失败则将回退到本地文件。 The designers of Java thought it best that when you're trying to run a program, you don't have to worry about having to track down the source that defines the class you're running. Java的设计师认为,最好的是,当您尝试运行程序时,不必担心必须跟踪定义正在运行的类的源。 You just give the fully qualified class name and let Java (or rather, its ClassLoader s) do the hard work of finding it. 您只需提供完全合格的类名,然后让Java(或更确切地说,其ClassLoader )完成查找它的艰苦工作即可。

The Java compiler takes a filename as input, hence Foo.java. Java编译器采用文件名作为输入,因此采用Foo.java。

the Java interpreter takes the fully qualified class name and searches the classpath and current directory for the class. Java解释器将使用完全限定的类名称,并在类路径和当前目录中搜索该类。 If you use java Foo.class it would search for the class named "class" in the package "Foo", and return NoClassDefFoundError if the class is in the default package, as I understand from your example 如果您使用的是Java Foo.class,它将在包“ Foo”中搜索名为“ class”的类,如果该类在默认包中,则返回NoClassDefFoundError,据我从您的示例中了解

Foo.java is the file name while simply "Foo" is the class name (not a file name). Foo.java是文件名,而简单的“ Foo”是类名(不是文件名)。 The default ClassLoader will search your current working directory for the file Foo.class and load it. 默认的ClassLoader将在当前工作目录中搜索文件Foo.class并加载它。

Adding to David Zaslavsky's explanation: 补充大卫·扎斯拉夫斯基的解释:

The java source code are not necessarily from .java files either. Java源代码也不一定来自.java文件。 We can have a compiler that takes source code from difference character streams, as in the API javax.tools.JavaCompiler . 我们可以拥有一个从差异字符流中获取源代码的编译器,如API javax.tools.JavaCompiler

The cmd line util javac happens to be working with file sources only, so it desires file paths. cmd行util javac恰好仅与文件源一起使用,因此需要文件路径。

Every language will have syntax and semantics which will establish rules and understanding between similar parties. 每种语言都将具有语法和语义,这些语法和语义将在相似各方之间建立规则和理解。 Like how english has words, tenses, verbs etc., to simplfy communication between two english speaking parties. 就像英语中的单词,时态,动词等如何简化两个英语会话之间的交流一样。 Java specification established syntax and semantics that compiler can understand only java files and interpreter can understand only .class files. Java规范建立了语法和语义,编译器只能理解Java文件,而解释器只能理解.class文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM