my dog learned polymorphism
The moose likes Beginning Java and the fly likes classpath trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classpath trouble" Watch "classpath trouble" New topic

classpath trouble

Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
hello friends,

i have made a program called 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(
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
at java.lang.ClassLoader.loadClassInternal(
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.

Anand Shrivastava
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15082

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\

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
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

Joined: Aug 31, 2004
Posts: 11343

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.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
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

Joined: Sep 26, 2007
Posts: 16
Check this thread, scroll down, I've had same 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 ]
I agree. Here's the link:
subject: classpath trouble
It's not a secret anymore!