简体   繁体   English

“无法找到主类”

[英]“Could not find the main class”

I'm trying to run a sample Java application from the command promopt but I'm getting the following error:我正在尝试从命令提示符运行示例 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.

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.java is (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.我正在尝试以 C# 开发人员的身份学习 Java,所以当我在编程概念方面有很强的背景时,整个 java 工具链目前完全让我感到困惑。 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. 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 ?为什么找不到HelloWorldDesktop

Right - the problem is that you've got HelloWorldDesktop.class in 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.您可以使用 javac 命令修复此问题 - 只需使用-d. to tell it to treat "."告诉它治疗“。” as the package root directory for output.作为 output 的 package 根目录。

Normally you would want to also organize your source code by package, but for this "hello world" test it may not be worth it.通常,您希望通过 package 组织您的源代码,但是对于这个“hello world”测试,它可能不值得。

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.步骤1:javac.exe,它将.java文件编译成.class文件。 Example: javac.exe ProgramFolder\*.java (where ProgramFolder = File System Directory)示例: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 "."第 2 步:java.exe 并将您要运行的应用程序(包括路径)作为参数提供,但不要使用“\”作为文件夹,而是使用“。” and the name of your class Example: ProgramFolder.ClassProgram和您的 class 的名称示例: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. 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看看这张图片的前两行http://3.bp.blogspot.com/-FO4Hmg9LrI0/Td7FoSIi_XI/AAAAAAAAF6g/FVAiP0h8CSc/s1600/fiborial_java.PNG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM