Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Classpath" Watch "Classpath" New topic
Author

Classpath

Venkateshwar Rao Eturi
Greenhorn

Joined: May 24, 2008
Posts: 3
Hi friends,
I am very new to Java.

I installed java on my Windows XP machine, got problems with compilation. After setting classpath in system variables, my program could compile. But when try to run the prog from command line, I get " No class def found error ". It is definitely with the classpath settings, but can anybody help me to set it correctly please?



Thanks,
Venkat
Sumit Bisht
Ranch Hand

Joined: Jul 02, 2008
Posts: 329

That's a common problem with java/allied java technologies.
You can solve this by right-clicking My Computer icon,Select Properties,select the advanced tab & click Environment Variables.

Here create a variable named PATH with value as
.;<JDK installation dir>\bin;<JRE installation dir>\rt.jar;<JDK installation dir>\lib\tools.jar;
It should work fine.
Note: bin is needed for tools like java and javac,rt jarfile contains runtime classes needed by java(java.lang package)

At command prompt, now try set path
You should see the files/dirs mentioned previously.
For more info, please download the java tutorial from java.sun.com
Sumit Bisht
Ranch Hand

Joined: Jul 02, 2008
Posts: 329

For classpath related problems try setting CLASSPATH variable with .; at the start of the list of classpaths.
. means the jre will search the current folder first for the class file
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
You are not supposed to set the classpath variable, only the path. Old books told you how to update the classpath, but that is no longer necessary.

Least of all should you add C:\Java\jdk1.6.0_06\bin to your classpath. That belongs in the PATH only. There ought to be a ;.; somewhere in the classpath, but it is virtually always included by default. The . means "current folder."

What you do on Windows is to create a folder, let's call it C:\JavaPrograms, but you can have it anywhere and call is something different.
Then you navigate to it by calling

cd C:\JavaPrograms

and your prompt says c:\JavaPrograms>: or similar.

Then you compile your java source, maybe Foo.java with the "java Foo.java" command, and you can tell when it works because the prompt appears with no error messages.
Then you execute the class file with "java Foo" - no .java or .class, but you may need command-line arguments.

Reasons for the error you are getting:
  • You are in a different folder with the command prompt.
  • You have put a package declaration in your source and not gone to the base folder to run the class.
  •  
    wood burning stoves
     
    subject: Classpath
     
    Similar Threads
    getting class not found in running .sh
    program assignments won't run anymore
    crontab
    Servlets - Java Ranch Tutorial Help
    Problem with interpreter