This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Hi, I was asking this a couple of posts before in an old thread but I guess nobody took notice so I'd like to try it once more with it's own topic ...
When I compile a class which is in the default package, everything works but if it's not in the default package I get the following error when I try to run the class ...
E:\Works\Eclipse\Java Test\bin\testpack>javaTest Exception in thread "main" java.lang.NoClassDefFoundError: Test (wrong name: testpack/Test) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Does anyone have a hint what I should do to make it run? Also when I run it in Eclipse, it works so I guess the problem has a relation to a classpath that is not included. But I have no clue how I should tell Java how to handle the package path.
Would be nice to get some help on this since it really keeps me from progressing at all.
If you don't want to type the whole package name in front of the class name, you could package all your classes in a JAR file. You'd have to include a manifest file into the JAR file with an entry that looks like this:
Then you could run the application by using a command line like this:
Originally posted by Sascha Balkau: Thanks Jesper! Yes, that way it works! I guess then to able to execute the program with just 'Java Test' I need to put it in a Jar file!??
Even if you put the Test.class file in a JAR file, you still have to use the package name when you run the program. Jesper's suggestion makes it so you don't have to type as much on the command-line, but you STILL need to specify the package name along with the class even though it is done in the manifest file instead.