java “无法找到主类”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6662665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:50:50  来源:igfitidea点击:

"Could not find the main class"

javaclasspathclassnotfoundexception

提问by Justin

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

我正在尝试从命令 promopt 运行示例 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.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)

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

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

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

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.javais (more or less):

再次从同一个工作目录运行 - 的内容HelloWorldDesktop.java是(或多或少):

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 HelloWorldDesktopcouldn'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.

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

Why can't it find HelloWorldDesktop?

为什么找不到HelloWorldDesktop

回答by Jon Skeet

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

对 - 问题是你HelloWorldDesktop.class在当前目录中,而它应该在 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.

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

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

通常你会想通过包组织你的源代码,但对于这个“Hello World”的测试可能并不值得。

回答by Carlos Quintanilla

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)

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

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

第 2 步:java.exe 并提供您要运行的应用程序(包括路径)作为参数,但不要对文件夹使用“\”,而是使用“.” 和您的类的名称示例: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.

那可行。如果您尝试运行 Java.exe ProgramFolder\Program.class 或 ProgramFolder\Program 或进入类文件所在的文件夹并且只运行 Java.exe Program.class 它总是会给您找不到主类错误。

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

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