This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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
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
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.