This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes -classpath switch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "-classpath switch" Watch "-classpath switch" New topic
Author

-classpath switch

Peter Caulfield
Greenhorn

Joined: Sep 27, 2010
Posts: 10
Hi,

Learning Java on OS X. Can someone please explain exactly the purpose of compiling and running java code using the -classpath switch from the terminal and how this works.

Thanks

Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Peter Caulfield wrote:Hi,

Learning Java on OS X. Can someone please explain exactly the purpose of compiling and running java code using the -classpath switch from the terminal and how this works.

Thanks



The operating system is irrelevant in this question.

The ClassPath is used to point out to the Java virtual machine where it should start looking when it tries to resolve the classes it needs. The classpath can be a directory or an archive (jar).

If we have this structure:



and you declare java -classpath /myjavafiles/project/bin se.lime.HelloWorld then this is the point where the JVM will start looking for the class se.lime.HelloWorld in the java command.

Same if you need classes from an external library. Then you add that to your classpath.
Peter Caulfield
Greenhorn

Joined: Sep 27, 2010
Posts: 10
Thanks for the reply Ove.

Just referenced operating system as the command line syntax would be different on windows ;)

Correct me if I'm wrong, but is this equivalent to cd-ing to the directory that contains a class\src you wish to compile/run?





fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

except that is is possible your class files are in multiple directories, especially if you are using third party packages. you can't CD into more than one at a time, so the classpath lets you list as many as you need.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Peter Caulfield
Greenhorn

Joined: Sep 27, 2010
Posts: 10
Ah I see. Now I know the purpose of the switch. How do you list multiple class paths in the one javac invocation? Also, is it true that any source files in the working directory are compiled as well?

Ex:



What's being compiled here?

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
In the above command the only class compiled is HelloWorld.java

If you want multiple locations to be specified in the classpath switch you separate them using a ;. Even in the above command there are two locations set as the classpath. One is the 3rdPartyCode\classes\ folder and the other the current working folder specified by the .

I believe all java source files present in the current working folder will be compiled only by the command javac -cp . *.java and not other wise.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Read about setting classpath - Setting Classpath
 
Don't get me started about those stupid light bulbs.
 
subject: -classpath switch
 
Similar Threads
How classpath works for executable Jars
SOURCEPATH
How do I install JGoodies Look and Feel??
Runtime Eror
Using a jar in classpath on command line