• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Programm doesn't start when not in default package

 
Sascha Balkau
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15278
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sascha Balkau
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15278
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic