Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Getting the path of the program's jar file

Thread view: 
mic123@gmail.com - 13 Jul 2006 11:27 GMT
Hey everyone,
Is there a java method that can help me obtain the path of the jar file
of the currently running program (from within the program itself)?

For example, if my jar's name is a.jar I want to be able to print a.jar
in the main app (dynamically of course)
Thomas Fritsch - 13 Jul 2006 12:53 GMT
> Hey everyone,
> Is there a java method that can help me obtain the path of the jar file
> of the currently running program (from within the program itself)?
Which jar file? Usually a Java application has more than 1 jar files.

> For example, if my jar's name is a.jar I want to be able to print a.jar
> in the main app (dynamically of course)

URL url =
YourClass.class.getProtectionDomain().getCodeSource().getLocation();

You should get something like  file://yourDirectory/a.jar or
http://www.yourServer.com/yourDirectory/a.jar

But there are issues with this approach:
  getProtectionDomain() may throw a SecurityException
  getCodeSource() may return null

See also the API docs of
  java.lang.Class#getProtectionDomain
  java.security.ProtectionDomain#getCodeSource
  java.security.CodeSource#getLocation

Signature

Thomas

Paul Davis - 13 Jul 2006 13:23 GMT
This should give you what you are looking for:

String resName = this.getClass().getName().replace('.','/')+".class";
String url = ClassLoader.getSystemResource(resName).getPath();

Just chop up the string to get what you need


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.