• 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

Jar Files and Classpath

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a question and any help would be greatly appreciated. My question is regarding setting classpath and jar files. Say I have a typical 'HelloWorld.class' and it lives in '/home/me/' dir. I execute it as follows:



Now say I package that class up in a jar file, using a manifest file that points to HelloWorld as the entry point, do I need to give a classpath when I execute that jar file, or is the jar format aware of the classpath? I understand if I would like to reference other jar files then I would need to make sure the appropriate classpath is defined for each jar file in the manifest, but what about just that one class in simplest form?

I know this probably sounds confusing so please let me know if it needs further clarification.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. In fact when you run an executable jar (via "java -jar executable.jar") you cannot specify a classpath at the command line. The classpath is entirely based on the jar file and nothing else (no -cp argument, no CLASSPATH environment variable, nothing else).

So if your executable jar contained only the HelloWorld class, you just run it using the "java -jar" command. If it happens that the HelloWorld class needs access to other classes which aren't in the jar, then the jar's manifest should have a "Class-Path" entry which says what those jars are and where they are relative to the executable jar.
reply
    Bookmark Topic Watch Topic
  • New Topic