Can java class file not work without setting classpath ?

nirjari patel

Ranch Hand

Posts: 386

posted 3 years ago

I have a java source file in a path, when I compile this source file by typing path to this file and also path to javacon cmd prompt , it compile and classfile is generated in same location where source file is.
Now when I am trying to execute classfile by typing path to class file and also path to java exe file on cmd prompt , I am getting main not found error as below

Sure you can type the classpath for the class as part of the cmd prompt. The syntax is like this:

java -cp classpath class

Have a look at this tutorial for a better description than what I could provide. Notice also that setting the PATH environment variable allows you to just type "java" instead of "C:\Program Files\java\jdk1.5.0_22\jre\bin\java" as you did, so read that part of the tutorial carefully as well.

(And why are you using Java 5? It has been obsolete and unsupported for years. Unless you have some reason for using Java 5 -- like somebody else is making you do that -- you should be using Java 7 now.)

That is a usage error -- which is completely different from a class not found error. You get this message when the JVM has no idea what you are trying to say, and is giving you a usage message with the correct parameters.

Are you sure you are cut and pasting what you are typing? You shouldn't be getting that message based on what you showed.