[英]In Java, is there a logical reason why I cannot say “java MyClass.class ” from command line?
Maybe this question may be splitting hairs, but when I compile a file from command line like : 也许这个问题可能会让人毛骨悚然,但是当我从命令行编译文件时,例如:
javac MyClass.java
then afterward I cannot run it by saying 然后我不能说
java MyClass.class
I have to call: 我必须打电话给:
java MyClass
What is the motivation for this notation? 这种表示法的动机是什么?
Because you run a class on the classpath, which may be contained inside a jar for example. 因为您在类路径上运行一个类,例如,它可能包含在jar中。 You couldn't use your syntax in that case. 在这种情况下,您将无法使用语法。
Java compiler needs a compilation unit; Java编译器需要一个编译单元。 this is by default (at least) a java source file, with the whole of classes defined in it and its dependencies. 默认情况下(至少),这是一个Java源文件,其中定义了整个类及其依赖项。
Java interpreter (the jvm) needs a single class with a main method as entry point of the execution - it must start somewhere. Java解释器(jvm)需要一个带有主方法的类作为执行的入口点-它必须从某个地方开始。
您必须向Sun(现在的Oracle)询问开发历史,但是我确实要指出,对于仅使用Java而不是开发Java的人们来说,“ java DoSomething”比起Java更容易记住和键入。 “ java DoSomething.class”
There is no way to run a Java program that is not a class. 无法运行不是类的Java程序。 For that reason, there is no reason to mandate typing the ".class". 因此,没有理由要求键入“ .class”。 You might also invoke a class from within a JAR on your path, or directly, but it's still instantiating a class (possibly a "default" class from the Manifest). 您也可以在路径上的JAR中或直接调用类,但是它仍在实例化一个类(可能是清单中的“默认”类)。
Because the name of the class is MyClass
and not MyClass.class
. 因为该类的名称是MyClass
而不是MyClass.class
。 And when running java
you specify the CLASS NAME and not the PATH to the actual compiled file. 并且在运行java
您指定CLASS NAME,而不是实际编译文件的PATH。 For more in depth knowledge I guess Sun & Oracle will have to answer :) 要获得更深入的知识,我想Sun和Oracle必须回答:)
Imagine that you have a class named package
and you have a class named Class
, in a package named package
, 假设您有一个名为package
Class
,并且在名为package
的包中有一个名为Class
,
--CurrentFolder
--package
Class.class
package.class
so executing java package.class
may lead to an undecidability to the compiler! 因此执行java package.class
可能会导致编译器无法确定!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.