Getting path to jar passed on command line

R

raphfrk

Is there a way to get the path of the jar file that was passed on the command line?

java -jar <some path>
 
A

Arne Vajhøj

You could try

RuntimeMXBean rtmxb = ManagementFactory.getRuntimeMXBean();
List<String> inArgs = rtmxb.getInputArguments();

although the documentation seems to suggest that what is actually
passed to the Runtime bean is dependent on the particular implementation
of the jvm you happen to be using.

He could try it.

But I think he will have better luck with rtmxb.getClassPath().

Arne
 
A

Arne Vajhøj

Is there a way to get the path of the jar file that was passed on the command line?

java -jar <some path>

Given how many different ways there are to get your code
activated (java -jar, java -cp, wrapper main), then my
recommendation would be:

NameOfYourClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()

as this will locate where that class is being loaded from no matter
how it gets activated.

Arne
 
R

Roedy Green

Is there a way to get the path of the jar file that was passed on the command line?

You can use -cp to specify the classpath or fully qualify the jar
name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top