Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Determine the absolute path of a jar file in classpath

 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Is there any way to determine the absolute path of jar file within the class path. If anybody knows, Please tell me how to do this?

Thanks,
Arun
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the classpath like this:

 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Officially you can't, but a few hacks can help you out. One of them uses ProtectionDomain, search Javaworld.com for the article. The other one uses the fact that with most class loaders you can use the .class file itself as a resource. For instance, try this:
If the class file is located in a JAR file cut off the leading "jar:" and everything after the !. What remains is a URL to the JAR file. If the class file is located in a folder cut off the full class name with / instead of . (e.g. java/lang/String.class).

To convert the remaining URL to a File the following code can help you:
That will only work if the remainingString is a URL with protocol "file".
 
Zon Horn
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's upgrade to other comments, that seem to me incomplete for the specifics of:
- using a relative "folder" outside .jar file (in the jar's same location).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic