Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

-classpath switch  RSS feed

 
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

 
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?





 
lowercase baba
Bartender
Posts: 12627
50
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?

 
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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!