dcsimg
How can I get the path to the jar file my program was loaded from?
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jason_Adams
Posted On:   Saturday, July 10, 2004 06:05 AM

I want to modify the jar file that my program was loaded from at runtime (ie. add a file to it). I can't use the system property "user.dir" and search it because it may be different if the program was started from a link or short cut. The method call: getClass().getProtectionDomain().getCodeSource() seems to return null when an application is loaded from a jar file? Is there something I am missing or is there an easy way to get the path to the jar file the program was loaded from at Runtime from ClassLoader? The reason I am doing this is because the user can install the program anywhere on their system (I do not want to recursive   More>>


I want to modify the jar file that my program was loaded from at runtime (ie. add a file to it). I can't use the system property "user.dir" and search it because it may be different if the program was started from a link or short cut.



The method call:



getClass().getProtectionDomain().getCodeSource()



seems to return null when an application is loaded from a jar file?



Is there something I am missing or is there an easy way to get the path to the jar file the program was loaded from at Runtime from ClassLoader?



The reason I am doing this is because the user can install the program anywhere on their system (I do not want to recursively search a 40 gig harddrive for this file) and my user audience won't all be able to set up environment variables to store this location.


Any ideas for an elegant way to get this information?    <<Less

Re: How can I get the path to the jar file my program was loaded from?

Posted By:   Anonymous  
Posted On:   Wednesday, July 14, 2004 01:20 AM

I dont think that you can modify your own jar - that file is in use by the os.

Re: How can I get the path to the jar file my program was loaded from?

Posted By:   Cristiano_Sadun  
Posted On:   Monday, July 12, 2004 07:02 AM

Re: How can I get the path to the jar file my program was loaded from?

Posted By:   Jason_Adams  
Posted On:   Saturday, July 10, 2004 10:38 AM

I am answering my own question so no one will waste their time on this. The ProtectionDomain does indeed return a non null CodeSource which will give you a URL with a normal jar file. How ever for those of you who use Borland's JBuilder like I do and create a somewhat native executable you will find that even though Borlands executable is a jar file with a native launcher in it, it does not provide the CodeSource to the ProtectionDomain thus resulting in a null reference. I guess it is back to the drawing board for hiding my license keys in the jar. Or in this case the executable.
About | Sitemap | Contact