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?
Right – the problem is that you’ve got
HelloWorldDesktop.classin the current directory, whereas it should be in com/badlogic/gdx/helloworldYou 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.