如何从命令行在 Windows 上运行 .class 文件?
我正在尝试从命令行运行 .class 文件。当我手动移动到它存储的目录时它可以工作,但是当我尝试这样的操作时:
java C:\Peter\Michael\Lazarus\Main
它说它找不到主类。除了制作 .jar 文件之外,还有其他解决方案吗(我知道 .jar 是最好的解决方案,但目前这不是我正在寻找的解决方案)?
I'm trying to run .class file from command line. It works when I manually move to the directory it's stored in, but when I try something like this:
java C:\Peter\Michael\Lazarus\Main
it says it can't find the main class. Is there any solution to this other than making a .jar file (I know that .jar is the best solution, but at this moment isn't the one I'm looking for)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Java 应用程序启动器(又名
java .exe
或简称java
)支持最多四种不同的方式来指定要启动的内容(取决于您使用的 Java 版本)。指定类名是最基本的方法。请注意,类名与文件名不同。
这里我们启动类
mypackage.Main
并使用-cp
开关指定用于查找类的类路径(类的完整路径< code>mypackage.Main 将是path/to/classFiles/mypackage/Main.class
。启动 jar 文件。
这会将 jar 本身以及其
Class-Path
条目上指定的任何内容放在类路径上,并启动通过Main-Class
条目指示的类。请注意,在这种情况下,您不能指定任何其他类路径条目(它们将被静默忽略)。此选项对于第一次尝试 Java 很有用,但很快就会达到其限制,因为它不允许您访问在另一个源文件中定义的类(除非您单独编译它们并将它们放在类路径中) ,这损害了此方法的易用性,并且意味着在这种情况下您可能应该切换回选项 #1)。
此功能被开发为JEP 330,有时仍然这样称呼。< /p>
对于您的具体情况,您可以使用选项 #1 并使用
-classpath
选项(或其缩写形式-cp)告诉
):java
在哪里查找该类如果您的
Main.java
包含整个源代码(并且位于同一目录中),那么您可以使用选项#4,跳过编译步骤,直接编译执行:The Java application launcher (a.k.a
java.exe
or simplyjava
) supports up to four different ways to specify what to launch (depending on which Java version you use).Specifying a class name is the most basic way. Note that the class name is different from the file name.
Here we start the class
mypackage.Main
and use the-cp
switch to specify the classpath which is used to find the class (the full path to the classmypackage.Main
will bepath/to/classFiles/mypackage/Main.class
.Starting a jar file.
This puts the jar itself and anything specified on its
Class-Path
entry on the class path and starts the class indicated via theMain-Class
entry. Note that in this case you can not specify any additional class path entries (they will be silently ignored).Java 9 introduced modules and with that it introduce a way to launch a specific module in a way similar to how option #2 works (either by starting that modules dedicated main class or by starting a user-specified class within that module):
Java 11 introduces support for Single-File Source Code Programs, which makes it very easy to execute Java programs that fit into a single source file. It even does the compile step for you:
This option can be useful for experimenting with Java for the first time, but quickly reaches its limits as it will not allow you to access classes that are defined in another source file (unless you compile them separately and put them on the classpath, which defeats the ease of use of this method and means you should probably switch back to option #1 in that case).
This feature was developed as JEP 330 and is still sometimes referred to as such.
For your specific case you'd use option #1 and tell
java
where to look for that class by using the-classpath
option (or its short form-cp
):If your
Main.java
contains the entirety of your source code (and it is in the same directory), then you can use option #4, skip the compile step and directly compile-and-execute it:假设
Main.class
没有包声明:Java在“类路径”中查找类,可以通过
-cp
选项在命令行上设置。Assuming that
Main.class
does not have a package declaration:Java looks for classes in a "classpath", which can be set on the command line via the
-cp
option.我刚刚遇到了同样的问题,我尝试运行
java hello.class
,这是错误的。命令应该是
java hello
。请勿包含文件扩展名。它正在寻找类文件,并将自行添加名称。
因此,运行“
java hello.class
”将告诉它去寻找“hello.class.class
”文件。I just had the same issue, I tried running
java hello.class
, this is wrong.The command should be
java hello
.Do not include the file extension. It is looking for a class file, and will add the name on its own.
So running '
java hello.class
' will tell it to go looking for 'hello.class.class
' file.试试这个:
您需要定义类路径。
Try this:
You need to define the classpath.