It has nothing to do with public vs. private; it's a classic first program problem. Something has set the CLASSPATH environment variable on your system; this tells Java where to look for .class files. The CLASSPATH setting does not, apparently, include "." (dot), which would stand for the current directory. The best way to deal with this is to tell Java where to look right when you execute the program:
Probably the same JVM you are using. Latest version for Windows. "1.6.0_26"
See page 8 Head first Java. They have an example, public class MyFirstapp etc Note says public so everyone can access it.
Although having said all that both version run ok for me.
I was compiling at one stage using
java ../classes/Test and then getting
Exception in thread "main" java.lang.NoClassDefFoundError: //\classes\Test
Caused by: java.lang.ClassNotFoundException: ..\classes\Test
at java.security.AccessController.doPrivileged(Native Method)
Could not find the main class: ..\classes\Test. Program will exit.
But if I ran directly in the classes folder it ran without a problem.
Where I saw the problem I was in source - which is on same level as classes.
Angus Comber wrote:
Why am I seeing this behaviour?
Because the argument to "java.exe" is not a path, it's the name of a class. java.exe must find the class by looking along the CLASSPATH. If you were to have typed
java -cp ../classes Test
it would have worked just fine.
The name of a class includes the name of the package, if any; if you try to run java Foo.class, Java complains that there's no class named Foo.class. There's only a class named Foo, in a file named Foo.class.
But what should i add to my classpath....
I am using jdk 1.6...
i have set environment variable "path" as "C:\Program Files\Java\jdk1.6.0\bin"...
and "classpath" ass "C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar" for servlets....
Is there anything i need to add in environment variables
Thanks Ernest Friedman-Hill for telling one more way to deal with CLASSPATH i.e you told
java -cp . FileName.......................Thanks........But how come java knows about our class file using this on our cmd??Because we haven't mentioned the directory the folder in which our class file is located???
posted 7 years ago
Rameshwar, have a think about your question. It doesn't. By using -cp . <Filename> you are telling the JVM that your classes are in the current directory. . means current directory.
Generally, you would not use java -cp . <file> because it is equivalent to java <file> which is obviously less typing. But the original poster (maybe that is you) had a problem with their classpath. So -cp is a switch which overrides the environment set CLASSPATH so you can just tell it - my java class is here.
What the original poster should of course do is fix their CLASSPATH problem.
The important thing to remember is that you pass the class name to the java command. You can use -cp on the command line to tell the JVM which folder to look for your class files. OR you can set the folder to look in using the CLASSPATH environment variable.