如何运行 Java .class 文件?
我已经编译了一个 HelloWorld 程序,并使用命令提示符来运行它。 .class 文件名为 HelloWorld2.class
该文件位于 C:\Users\Matt\workspace\HelloWorld2\bin 以下是当我进入命令提示符并输入“Java HelloWorld2”时得到的结果:
C:\Users\Matt>Java HelloWorld2
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld2
Caused by: java.lang.ClassNotFoundException: HelloWorld2
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: HelloWorld2. Program will exit.
我期望看到打印出的 HelloWorld。我做错了什么?我已经安装了JDK。
I've compiled a HelloWorld program, and I'm using the command prompt to run it. The .class file is named HelloWorld2.class
The file is located in C:\Users\Matt\workspace\HelloWorld2\bin
Here's what I'm getting when I go to command prompt, and type "Java HelloWorld2" :
C:\Users\Matt>Java HelloWorld2
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld2
Caused by: java.lang.ClassNotFoundException: HelloWorld2
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: HelloWorld2. Program will exit.
I was expecting to see a HelloWorld printed out. What am I doing wrong? I have the JDK installed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你的类没有包,你只需要设置类路径即可找到你编译的类:
java -cp C:\Users\Matt\workspace\HelloWorld2\bin HelloWorld2
如果你的类有包package,那么它需要位于与包名称对应的目录中,并且类路径必须设置为代表包的目录树的根。
If your class does not have a package, you only need to set the classpath to find your compiled class:
java -cp C:\Users\Matt\workspace\HelloWorld2\bin HelloWorld2
If your class has a package, then it needs to be in a directory corresponding to the package name, and the classpath must be set to the root of the directory tree that represents the package.
要从命令行运行 Java 类文件,语法为:
where packageName(通常以
com< /code> 或
org
) 是类文件所在的文件夹名称。例如,如果您的主类名称是 App,并且应用程序的 Java 包名称 是
com.foo.app< /code>,那么您的类文件需要位于
com/foo/app
文件夹中(每个点都有单独的文件夹),因此您可以将应用程序运行为:注意:
$< /code> 表示 shell 提示符,输入时忽略它
如果您的类没有定义任何
package
名称,只需运行:java App
。如果您有任何其他 jar 依赖项,请确保指定了 classpath 参数使用
-cp
/-classpath
或使用CLASSPATH
变量指向您的文件夹jar/war/ear/zip/class 文件。因此,在 Linux 上,您可以在命令前加上:CLASSPATH=/path/to/jars
,在 Windows 上,您需要将文件夹添加到系统变量中。如果未设置,则用户类路径由当前目录 (.
) 组成。实际示例
假设我们使用 Maven 创建了示例项目:
并且我们通过
mvncompile 编译了我们的项目
在我们的my-app/
目录中,它将在target/classes/com/foo/app/App.class
中生成我们的类文件。要运行它,我们可以通过
-cp
指定类路径或直接访问它,请检查下面的示例:要仔细检查您的类和包名称,您可以使用 Java 类文件反汇编工具,例如:
注意:如果编译后的文件被混淆,
javap
将无法工作。To run Java class file from the command line, the syntax is:
where packageName (usually starts with either
com
ororg
) is the folder name where your class file is present.For example if your main class name is App and Java package name of your app is
com.foo.app
, then your class file needs to be incom/foo/app
folder (separate folder for each dot), so you run your app as:Note:
$
is indicating shell prompt, ignore it when typingIf your class doesn't have any
package
name defined, simply run as:java App
.If you've any other jar dependencies, make sure you specified your classpath parameter either with
-cp
/-classpath
or usingCLASSPATH
variable which points to the folder with your jar/war/ear/zip/class files. So on Linux you can prefix the command with:CLASSPATH=/path/to/jars
, on Windows you need to add the folder into system variable. If not set, the user class path consists of the current directory (.
).Practical example
Given we've created sample project using Maven as:
and we've compiled our project by
mvn compile
in ourmy-app/
dir, it'll generate our class file is intarget/classes/com/foo/app/App.class
.To run it, we can either specify class path via
-cp
or going to it directly, check examples below:To double check your class and package name, you can use Java class file disassembler tool, e.g.:
Note:
javap
won't work if the compiled file has been obfuscated.这可能意味着很多事情,但最常见的是文件中包含的类与文件本身的名称不同。因此,检查您的类是否也称为 HelloWorld2。
This can mean a lot of things, but the most common one is that the class contained in the file doesn't have the same name as the file itself. So, check if your class is also called HelloWorld2.
C:/blah/blah/foldercontainJava
javac javafile.java
C:/blah/blah/foldercontainJava
javac javafile.java