aspose file tools*
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classpath trouble" Watch "classpath trouble" New topic
Author

classpath trouble

Anand Shrivastava
Ranch Hand

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


Anand Shrivastava
SCJA
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14429
    
  23

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 ]

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
Sheriff

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
sscce.org
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
Greenhorn

Joined: Sep 26, 2007
Posts: 16
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classpath trouble