| Author |
Java Programm doesn't start when not in default package
|
Sascha Balkau
Greenhorn
Joined: Jan 25, 2006
Posts: 8
|
|
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>java Test 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. Thanks! Sascha
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12924
|
|
Instead of this: E:\Works\Eclipse\Java Test\bin\testpack>java Test try this: E:\Works\Eclipse\Java Test\bin>java testpack.Test i.e., run it from the "bin" directory and include the package name.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Sascha Balkau
Greenhorn
Joined: Jan 25, 2006
Posts: 8
|
|
|
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!??
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12924
|
|
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: Main-Class: testpack.Test Then you could run the application by using a command line like this: java -jar myjarfile.jar To learn more about JAR files, see JavaTM Archive (JAR) Files in the JDK documentation.
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
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. Layne
|
Java API Documentation
The Java Tutorial
|
 |
 |
|
|
subject: Java Programm doesn't start when not in default package
|
|
|