This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

-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
Posts: 12628
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!