File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

name of a jar

 
Jason Kretzer
Ranch Hand
Posts: 280
  • 0
  • 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.
eg.
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?
Thanks,
 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • 0
  • 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.
Barry
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic