Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classpath error

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
 
Ted North
Ranch Hand
Posts: 200
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nice, thanks ted for link.
 
Ted North
Ranch Hand
Posts: 200
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:nice, thanks ted for link.


You bet sir. Did it work?

-Ted
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic