“找不到主类”

发布于 2024-11-19 12:18:19 字数 1594 浏览 2 评论 0原文

我正在尝试从命令提示符运行示例 Java 应用程序,但出现以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/badlogic/gdx/helloworld/HelloWorldDesktop
Caused by: java.lang.ClassNotFoundException: com.badlogic.gdx.helloworld.HelloWorldDesktop
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.badlogic.gdx.helloworld.HelloWorldDesktop.  Program will exit.

我用来尝试运行此应用程序的命令是:

java -cp .;gdx.jar;gdx-backend-jogl.jar com.badlogic.gdx.helloworld.HelloWorldDesktop

所有相关文件都在当前工作目录中 (.java 、.class 和 .jar 文件)

我用来构建 .class 文件的命令如下(有 2 个 .java 文件):

javac -cp gdx.jar;gdx-backend-jogl.jar HelloWorld.java HelloWorldDesktop.java

同样,这是从同一工作目录运行的 - HelloWorldDesktop.java< 的内容/code> 是(更多或less):

package com.badlogic.gdx.helloworld;

public class HelloWorldDesktop {
    public static void main (String[] argv) {
        // Application
    }
}

我正在尝试以 C# 开发人员的身份学习 Java,因此虽然我在编程概念方面拥有强大的背景,但整个 java 工具链目前完全让我感到困惑。该异常表明找不到类 HelloWorldDesktop,但据我所知,我已经获得了正确的名称,并且已将正确的 .jar 文件添加到类路径中,所以Java应该能够加载这个类。

为什么找不到HelloWorldDesktop

I'm trying to run a sample Java application from the command promopt but I'm getting the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/badlogic/gdx/helloworld/HelloWorldDesktop
Caused by: java.lang.ClassNotFoundException: com.badlogic.gdx.helloworld.HelloWorldDesktop
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.badlogic.gdx.helloworld.HelloWorldDesktop.  Program will exit.

The command I'm using to try and run this app is:

java -cp .;gdx.jar;gdx-backend-jogl.jar com.badlogic.gdx.helloworld.HelloWorldDesktop

Where all relevant files are in the current working directory (.java, .class and .jar files)

The command I used to build the .class files was as follows (there are 2 .java files):

javac -cp gdx.jar;gdx-backend-jogl.jar HelloWorld.java HelloWorldDesktop.java

Again this was run from the same working directory - The contents of HelloWorldDesktop.java is (more or less):

package com.badlogic.gdx.helloworld;

public class HelloWorldDesktop {
    public static void main (String[] argv) {
        // Application
    }
}

I'm attempting to learn Java as a C# developer, so wheras I have a strong background in programming concepts the whole java toolchain is currently completely confusing me. The exception indicates that the class HelloWorldDesktop couldn't be found, but as far as I can tell I've got the correct name and I've added the correct .jar files to the class path and so Java should be able to load this class.

Why can't it find HelloWorldDesktop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦醒灬来后我 2024-11-26 12:18:19

是的 - 问题是您在当前目录中有 HelloWorldDesktop.class,而它应该在 com/badlogic/gdx/helloworld 中

您可以使用 javac 命令修复此问题 - 只需使用 -d . 告诉它处理“.”作为输出的包根目录。

通常您还希望按包组织源代码,但对于这个“hello world”测试来说可能不值得。

Right - the problem is that you've got HelloWorldDesktop.class in the current directory, whereas it should be in com/badlogic/gdx/helloworld

You can fix this with the javac command - just use -d . to tell it to treat "." as the package root directory for output.

Normally you would want to also organize your source code by package, but for this "hello world" test it may not be worth it.

绳情 2024-11-26 12:18:19

好的,首先您需要使用两个不同的工具编译并运行应用程序

第 1 步:javac.exe,它将 .java 文件编译为 .class 文件。
示例:javac.exe ProgramFolder\*.java(其中 ProgramFolder = 文件系统目录)

然后

步骤 2:java.exe 并给出要运行的应用程序(包括路径)作为参数,但不要对文件夹使用“\”,而使用“ ”。以及你班级的名字
示例:ProgramFolder.ClassProgram

可以。如果您尝试运行 Java.exe ProgramFolder\Program.class 或仅 ProgramFolder\Program 或进入类文件所在的文件夹并且仅执行 Java.exe Program.class,它将始终给您找不到主类错误。

看看这张图片的前两行 http://3.bp.blogspot.com/-FO4Hmg9LrI0/Td7FoSIi_XI/AAAAAAAAF6g/FVAiP0h8CSc/s1600/fiborial_java.PNG

Ok, first of all you need to compile and then run the app using two different tools

Step 1: javac.exe which compiles the .java files into .class files.
Example: javac.exe ProgramFolder\*.java (where ProgramFolder = File System Directory)

then

Step 2: java.exe and give as parameter the app you want to run including the path, but instead of using "\" for folders use "." and the name of your class
Example: ProgramFolder.ClassProgram

That will work. if you try to run Java.exe ProgramFolder\Program.class or just ProgramFolder\Program or go into the folder where the class files are and only do Java.exe Program.class it will always give you the cannot find Main class error.

Have a look at the first 2 lines of this picture http://3.bp.blogspot.com/-FO4Hmg9LrI0/Td7FoSIi_XI/AAAAAAAAF6g/FVAiP0h8CSc/s1600/fiborial_java.PNG

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文