• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

-classpath switch

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?





 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read about setting classpath - Setting Classpath
 
reply
    Bookmark Topic Watch Topic
  • New Topic