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,
Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
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
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.
Make visible what, without you, might perhaps never have been seen. - Robert Bresson