Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

classpath trouble

 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends,

i have made a program called Mainprogram.java and saved it in d:\hosp directory. This program is put in drugstore package. It compiles well. However, when i try to run it using jdkcommander or command line saying java drugstore.Mainprogram, i get the following error :

java.lang.NoClassDefFoundError: Mainprogram (wrong name: drugstore/Mainprogram)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
Exception in thread "main"

However when i remove this program from package drugstore, it runs well.

i have set the classpath to .;d:\hosp;d:\hosp\drugstore

i am using jdk1.6.0

the class is compiled in d:\hosp\drugstore.

kindly help.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the class is in package drugstore, then it must be saved in a direcory named 'drugstore'.

Make a subdirectory 'drugstore' in your directory d:\hosp and put the source file in there (so, in d:\hosp\drugstore). Compile it with:

D:\hosp> javac drugstore\Mainprogram.java

Then run it with:

D:\hosp> java drugstore.Mainprogram

You have to add D:\hosp to the classpath if you want to be able to run it from a different directory. You do not need to put D:\hosp\drugstore to the classpath.
[ September 29, 2007: Message edited by: Jesper Young ]
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have done exactly like that, but it still gives the same error.

i am using jdk1.6. is there some bug with it.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you open a new command prompt after changing the classpath? On some operating systems (for example, Windows ME), you need to reboot before it takes effect.

If that still doesn't work, try running with the classpath specified at runtime...

>java -cp D:\hosp drugstore.Mainprogram

Let us know if that works.
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i have tried both the options.
I rebooted the system and tried the runtime classpath as well.
It still gives the same error.

I am using xp.
I have set the classpath to .;d:\hosp by going to mycomputer-system-advanced-environment variables.
Then rebooted the system.
Problem persists.
 
Sarah Jorden
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this thread, scroll down, I've had same trouble,

http://www.coderanch.com/t/408296/java/java/package-access-trouble

Besides that, make sure all these :

1) d:\hosp is in classpath. Which you did.

2) After compiling, the resulting .class file must be in d:\hosp\drugstore

3) execute it with this :
java drugstore.Mainprogram

if you try
java Mainprogram
even from the same folder as your .class file is in, it will give the same error.

One more thing, if you don't put package statement in source file then it will be considered in default package and then if you execute it with java drugstore.Mainprogram it will give error. Even if you put .class file in the drugstore folder. Hope I'm not making all these confusing.

I hope this helps
Good Luck.
[ September 30, 2007: Message edited by: Sarah Jorden ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic