• 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

name of a jar

Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a jar that needs to perform an operation on itself(like read a text file inside itself). This is easy enough, however, here is the catch. How can a jar determine what its own file name is? This will need to be determined at runtime.
1.) foo.jar is created using the JDK jar tool.
2.) John Q. User renames the jar to bar.jar.
3.) When executed, bar.jar knows its name is "bar.jar" and sets up a connection to do its operation.

Question: How can I get bar.jar to know what its name is at runtime?
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess a solution is not as simple as having only 1 jar file??? If you have only 1, then it would be easy to determine the name of it, with an instance of the File class. But if you have more than 1, hmmmmmm... You could possibly get the instance of the Class you are running, then try to find that class in the jar files. But, if you have the same class in multiple jar files, then that wouldn't work. I do not have a simple solution, but maybe someone else does, or I have jogged your brain a little.
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there is an easy way to do it, but I don't think it matters, either. Your JAR represents a self-contained directory structure, even if it's completely flat. The "document root" of your JAR is the top level directory of all the contained elements. So if your Main-Class knows how to get to the top of the JAR, and can traverse down to the location of the text file, voila! You're good to go.
This one time, at bandcamp, I had relations with a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic