Comp Sci: Java: a bit about classpath

Self-tutoring about Java: the tutor mentions classpath.

It might properly be called CLASSPATH. It’s where Java will search for the class you want it to run.

Yet, if you’re running a Java program from the command line after compiling it, you might get an error something like “Can’t find the main class [YourClass.class].” Why does such an error happen?

Sometimes, a program that uses Java might have set your CLASSPATH without your knowning. Then, perhaps Java will look where the CLASSPATH points, rather than the current directory. This can be a confusing error to face when you’re in an unfamiliar environment.

In Windows, one way around this seems to be to use the command line option -cp . when you go to run the program. Suppose, for instance, you’ve compiled P1 to the current directory and now want to run it:

java -cp . P1

might do the trick.

-cp .

tells java to “look in the current directory.” It’s worked for me when, for some reason, java couldn’t find the class it was supposed to run, yet was in the current directory.

Source:

docs.oracle.com

docs.oracle.com

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply