[英]java compilation: classname Vs classname with file-extension
Running basic java programs from commands line is a 3 steps process:从命令行运行基本的 Java 程序是一个 3 步过程:
Write code:编写代码:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } } } }
Compile by javac HellWorld.java
which would check for errors & generate HelloWorld.class
file.由javac HellWorld.java
编译,它会检查错误并生成HelloWorld.class
文件。
run code by giving the class name --> java HelloWorld
通过给出类名来运行代码 --> java HelloWorld
Now, I am curious to know why:现在,我很想知道为什么:
java HelloWorld
works but when we give fullpath of the classfile, it throws an error java HelloWorld
可以工作,但是当我们提供类文件的完整路径时,它会引发错误
$ java HelloWorld.class
Error: Could not find or load main class HelloWorld.class
What does it make a difference if we give just the classname Vs classname with file-extension?如果我们只给出类名与带有文件扩展名的类名,这有什么区别?
What does it make a difference if we give just the classname Vs classname with file-extension?如果我们只给出类名与带有文件扩展名的类名,这有什么区别?
The argument you give to the java
binary isn't meant to be a filename.您提供给java
二进制文件的参数并不意味着是文件名。 It's meant to be a class name.它应该是一个类名。 So in particular, if you're trying to start a class called Baz
in package foo.bar
you would run:因此,特别是,如果您尝试在foo.bar
包中启动一个名为Baz
的类,您将运行:
java foo.bar.Baz
So similarly, if you try to run java HelloWorld.class
it's as if you're trying to run a class called class
in a package HelloWorld
, which is incorrect.同样,如果您尝试运行java HelloWorld.class
,就好像您正在尝试在包HelloWorld
运行名为class
,这是不正确的。
Basically, you shouldn't view the argument as a filename - you should view it as a fully-qualified class name.基本上,您不应该将参数视为文件名 - 您应该将其视为完全限定的类名。 Heck there may not even be a simple Baz.class
file on the file system - it may be hidden away within a jar file.哎呀,文件系统上甚至可能没有一个简单的Baz.class
文件——它可能隐藏在一个 jar 文件中。
What does it make a difference if we give just the classname Vs classname with file-extension?如果我们只给出类名与带有文件扩展名的类名,这有什么区别?
It is because that is the way it is.这是因为它就是这样。 Sun / Oracle have implemented the java
command to work that way since Java 1.0, and changing it would be massively disruptive.自 Java 1.0 以来,Sun / Oracle 已经实现了java
命令以这种方式工作,并且更改它会产生巨大的破坏性。
As Jon says, the argument to the command is a fully qualified class name, not a filename.正如 Jon 所说,命令的参数是完全限定的类名,而不是文件名。 In fact, it is quite possible that a file with the name HelloWorld.class
does not exist.实际上,很可能不存在名为HelloWorld.class
的文件。 It could be a member of a JAR file ... or in some circumstances, just about anything.它可能是 JAR 文件的成员……或者在某些情况下,几乎任何东西。
In Java 11 and later it is also possible to compile and run a single Java source file with a single command as follows:在 Java 11 及更高版本中,还可以使用单个命令编译和运行单个 Java 源文件,如下所示:
java HelloWorld.java
(This possible because Oracle no longer supports Java distributions without a Java bytecode compiler.) (这可能是因为 Oracle 不再支持没有 Java 字节码编译器的 Java 发行版。)
In the Java programming language, source files (.java files) are compiled into (virtual) machine-readable class files which have a .class extension.在 Java 编程语言中,源文件(.java 文件)被编译成(虚拟)机器可读的类文件,这些文件具有 .class 扩展名。
When you run java class file after compile then run the following command:编译后运行java类文件时,请运行以下命令:
java HelloWorld你好世界
Note: Need to setup java classpath注意:需要设置java classpath
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.