如何运行 Java .class 文件?

发布于 2024-11-02 18:21:26 字数 843 浏览 3 评论 0原文

我已经编译了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

維他命╮ 2024-11-09 18:21:26

如果你的类没有包,你只需要设置类路径即可找到你编译的类:

java -cp C:\Users\Matt\workspace\HelloWorld2\bin HelloWorld2

如果你的类有包package,那么它需要位于与包名称对应的目录中,并且类路径必须设置为代表包的目录树的根。

// Source file HelloWorld2/src/com/example/HelloWorld2.java
package com.example;
...

Compiled class file: HelloWorld2/bin/com/example/HelloWorld2.class

$ java -cp HelloWorld2/bin com.example.HelloWorld2

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.

// Source file HelloWorld2/src/com/example/HelloWorld2.java
package com.example;
...

Compiled class file: HelloWorld2/bin/com/example/HelloWorld2.class

$ java -cp HelloWorld2/bin com.example.HelloWorld2
吻泪 2024-11-09 18:21:26

要从命令行运行 Java 类文件,语法为:

java -classpath /path/to/jars <packageName>.<MainClassName>

where packageName(通常以 com< /code> 或 org) 是类文件所在的文件夹名称。

例如,如果您的主类名称是 App,并且应用程序的 Java 包名称com.foo.app< /code>,那么您的类文件需要位于 com/foo/app 文件夹中(每个点都有单独的文件夹),因此您可以将应用程序运行为:

$ java com.foo.app.App

注意:$< /code> 表示 shell 提示符,输入时忽略它

如果您的类没有定义任何package名称,只需运行:java App

如果您有任何其他 jar 依赖项,请确保指定了 classpath 参数使用 -cp/-classpath 或使用 CLASSPATH 变量指向您的文件夹jar/war/ear/zip/class 文件。因此,在 Linux 上,您可以在命令前加上:CLASSPATH=/path/to/jars,在 Windows 上,您需要将文件夹添加到系统变量中。如果未设置,则用户类路径由当前目录 (.) 组成。


实际示例

假设我们使用 Maven 创建了示例项目:

$ mvn archetype:generate -DgroupId=com.foo.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 

并且我们通过 mvncompile 编译了我们的项目 在我们的 my-app/ 目录中,它将在 target/classes/com/foo/app/App.class 中生成我们的类文件。

要运行它,我们可以通过 -cp 指定类路径或直接访问它,请检查下面的示例:

$ find . -name "*.class"
./target/classes/com/foo/app/App.class
$ CLASSPATH=target/classes/ java com.foo.app.App
Hello World!
$ java -cp target/classes com.foo.app.App
Hello World!
$ java -classpath .:/path/to/other-jars:target/classes com.foo.app.App
Hello World!
$ cd target/classes && java com.foo.app.App
Hello World!

要仔细检查您的类和包名称,您可以使用 Java 类文件反汇编工具,例如:

$ javap target/classes/com/foo/app/App.class
Compiled from "App.java"
public class com.foo.app.App {
  public com.foo.app.App();
  public static void main(java.lang.String[]);
}

注意:如果编译后的文件被混淆,javap将无法工作。

To run Java class file from the command line, the syntax is:

java -classpath /path/to/jars <packageName>.<MainClassName>

where packageName (usually starts with either com or org) 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 in com/foo/app folder (separate folder for each dot), so you run your app as:

$ java com.foo.app.App

Note: $ is indicating shell prompt, ignore it when typing

If 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 using CLASSPATH 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:

$ mvn archetype:generate -DgroupId=com.foo.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 

and we've compiled our project by mvn compile in our my-app/ dir, it'll generate our class file is in target/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:

$ find . -name "*.class"
./target/classes/com/foo/app/App.class
$ CLASSPATH=target/classes/ java com.foo.app.App
Hello World!
$ java -cp target/classes com.foo.app.App
Hello World!
$ java -classpath .:/path/to/other-jars:target/classes com.foo.app.App
Hello World!
$ cd target/classes && java com.foo.app.App
Hello World!

To double check your class and package name, you can use Java class file disassembler tool, e.g.:

$ javap target/classes/com/foo/app/App.class
Compiled from "App.java"
public class com.foo.app.App {
  public com.foo.app.App();
  public static void main(java.lang.String[]);
}

Note: javap won't work if the compiled file has been obfuscated.

初与友歌 2024-11-09 18:21:26

这可能意味着很多事情,但最常见的是文件中包含的类与文件本身的名称不同。因此,检查您的类是否也称为 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.

聚集的泪 2024-11-09 18:21:26
  1. 转到保存要编译的 java 文件的路径。
  2. 输入 cmd 并按 Enter 键替换路径。
  3. 将弹出命令提示符目录,其中包含路径文件,例如 C:/blah/blah/foldercontainJava
  4. 输入 javac javafile.java
  5. 按 Enter。它会自动生成java类文件
  1. Go to the path where you saved the java file you want to compile.
  2. Replace path by typing cmd and press enter.
  3. Command Prompt Directory will pop up containing the path file like C:/blah/blah/foldercontainJava
  4. Enter javac javafile.java
  5. Press Enter. It will automatically generate java class file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文