Reputation: 23
I know there are a lot of similar questions and answers. But no one solution work for me! I don't understand why(( So I have runnable jar for example 'program.jar' and inside of it I want to take name of this jar (namely program.jar). I've tried following:
ClassInsideJar.class.getProtectionDomain().getCodeSource().getLocation().getPath()
new java.io.File(ClassInsideJar.class.getProtectionDomain().getCodeSource().getLocation().getPath())
new File(".")).getAbsolutePath()
(and different variations like getCanonicalPath etc)ClassLoader.getSystemClassLoader().getResource(".").getPath()
And in all these cases I can take only path to this jar. For example full path is J:/folder/program.jar
. All that I can see are J:/folder
.
What's the mistake? I'm trying to get name of jar in main class and others.
I've tried to run this jar by double click and with command line. Result is the same
Upvotes: 0
Views: 243
Reputation: 12332
Try this:
(new File(ClassInsideJar.class.getProtectionDomain().getCodeSource().getLocation().toURI())).getName();
Upvotes: 1