File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Classpath error 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Classpath error" Watch "Classpath error" New topic
Author

Classpath error

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583




F:\Project\OCA7\Chapter10\java\search_classes>javac -classpath new Original.java

F:\Project\OCA7\Chapter10\java\search_classes>java -classpath new Original
Error: Could not find or load main class Original


Tell the difficulties that i am difficult.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

have you tried using the dot in the classpath to put the current directory in the classpath with the new directory also? This way your program will find Original.class in the current directory and the Third.class in the new/third directory

try this script: java -classpath new;. Original

(if you are using some form of Unix the separator is more than likely a colon not a semi-colon)

If this doesn't help I apologize.

Here is a tutorial: http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html

Note the section on overriding the classpath...

Respectfully,

TN - juggernaut of IT and island buying
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

nice, thanks ted for link.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

saloni jhanwar wrote:nice, thanks ted for link.


You bet sir. Did it work?

-Ted
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

To sum things up the default place where javac and java binaries look for class files is in current directory which is represented by . also you can set your classpath in your environment variable as well. for windows you can right click my computer > advanced system settings >Advanced > Environment variables. there you define new variable named classpath and set it to your desired directory. for nix machines you have to edit your corresponding .bash_profile or .bashrc file.

You can also set the classpath in command prompt using set classpath=<semi-colon seperated values>. however keep in mind that this will be valid only for that command prompt window and the changes will be lost once you close the command prompt. to check whether the classpath is set or not you can type this on the command prompt SET CLASSPATH.

the third way to set the classpath (and the recommended way) is to use -classpath or its shortened form -cp switch. this is the recommended way as it gives you the flexibility to set your path individually depending upon your application.

Now this is IMPORTANT. when you set your classpath either throught environment variable or through command prompt it OVERRIDES the DEFAULT value which is the current directory. this means that if your classpath is set the javac or java binaries won't look in your current directory and will look only in the path you specified in your classpath.

In your case exactly same thing happened when you did java -classpath new Original. the java interpreter in this case cannot find the Original class file because it is in the current directory and the current directory is not in your classpath(because it has been overridden by the specified path which is new). so ALWAYS keep in mind to include the current directory in your classpath while specifying classpath in environment variable or throught classpath switch .

So to run Original you the command as said by Ted would be java -cp new;. Original


there is excellent article regarding classpath on ibm site. it is really very good and helped me understood this topic which i believe is not given as good in kb6 book as other chapters.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

gurpeet singh wrote:To sum things up the default place where javac and java binaries look for class files is in current directory which is represented by . also you can set your classpath in your environment variable as well. for windows you can right click my computer > advanced system settings >Advanced > Environment variables. there you define new variable named classpath and set it to your desired directory. for nix machines you have to edit your corresponding .bash_profile or .bashrc file.

You can also set the classpath in command prompt using set classpath=<semi-colon seperated values>. however keep in mind that this will be valid only for that command prompt window and the changes will be lost once you close the command prompt. to check whether the classpath is set or not you can type this on the command prompt SET CLASSPATH.

the third way to set the classpath (and the recommended way) is to use -classpath or its shortened form -cp switch. this is the recommended way as it gives you the flexibility to set your path individually depending upon your application.

Now this is IMPORTANT. when you set your classpath either throught environment variable or through command prompt it OVERRIDES the DEFAULT value which is the current directory. this means that if your classpath is set the javac or java binaries won't look in your current directory and will look only in the path you specified in your classpath.

In your case exactly same thing happened when you did java -classpath new Original. the java interpreter in this case cannot find the Original class file because it is in the current directory and the current directory is not in your classpath(because it has been overridden by the specified path which is new). so ALWAYS keep in mind to include the current directory in your classpath while specifying classpath in environment variable or throught classpath switch .

So to run Original you the command as said by Ted would be java -cp new;. Original


there is excellent article regarding classpath on ibm site. it is really very good and helped me understood this topic which i believe is not given as good in kb6 book as other chapters.

nice
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

thanks all
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Classpath error