“Could not find the main class”

2019-02-22 11:16发布

问题:

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?

回答1:

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.



回答2:

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