Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

-classpath switch

 
Peter Caulfield
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Caulfield
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read about setting classpath - Setting Classpath
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic