Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #23020 > unrolled thread

Getting path to jar passed on command line

Started byraphfrk@gmail.com
First post2013-03-21 03:55 -0700
Last post2013-03-21 17:31 -0400
Articles 8 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Getting path to jar passed on command line raphfrk@gmail.com - 2013-03-21 03:55 -0700
    Re: Getting path to jar passed on command line lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-03-21 12:37 +0000
      Re: Getting path to jar passed on command line Arne Vajhøj <arne@vajhoej.dk> - 2013-03-21 08:49 -0400
    Re: Getting path to jar passed on command line Arne Vajhøj <arne@vajhoej.dk> - 2013-03-21 08:55 -0400
    Re: Getting path to jar passed on command line lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-03-21 13:41 +0000
      Re: Getting path to jar passed on command line raphfrk@gmail.com - 2013-03-22 09:57 -0700
    Re: Getting path to jar passed on command line Roedy Green <see_website@mindprod.com.invalid> - 2013-03-21 14:11 -0700
      Re: Getting path to jar passed on command line Arne Vajhøj <arne@vajhoej.dk> - 2013-03-21 17:31 -0400

#23020 — Getting path to jar passed on command line

Fromraphfrk@gmail.com
Date2013-03-21 03:55 -0700
SubjectGetting path to jar passed on command line
Message-ID<98cfa140-0b4a-476a-bf1c-9cc2c39f22d8@googlegroups.com>
Is there a way to get the path of the jar file that was passed on the command line?

java -jar <some path>

[toc] | [next] | [standalone]


#23024

Fromlipska the kat <"nospam at neversurrender dot co dot uk">
Date2013-03-21 12:37 +0000
Message-ID<QoqdnbXcXNBzY9fMnZ2dnUVZ7o-dnZ2d@bt.com>
In reply to#23020
On 21/03/13 10:55, raphfrk@gmail.com wrote:
> Is there a way to get the path of the jar file that was passed on the command line?
>
> java -jar<some path>
>

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.

lipska

-- 
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun

[toc] | [prev] | [next] | [standalone]


#23025

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-03-21 08:49 -0400
Message-ID<514b01be$0$32115$14726298@news.sunsite.dk>
In reply to#23024
On 3/21/2013 8:37 AM, lipska the kat wrote:
> On 21/03/13 10:55, raphfrk@gmail.com wrote:
>> Is there a way to get the path of the jar file that was passed on the
>> command line?
>>
>> java -jar<some path>
>>
>
> 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

[toc] | [prev] | [next] | [standalone]


#23026

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-03-21 08:55 -0400
Message-ID<514b0332$0$32116$14726298@news.sunsite.dk>
In reply to#23020
On 3/21/2013 6:55 AM, raphfrk@gmail.com wrote:
> 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


[toc] | [prev] | [next] | [standalone]


#23029

Fromlipska the kat <"nospam at neversurrender dot co dot uk">
Date2013-03-21 13:41 +0000
Message-ID<fdidnd_45PyGk9bMnZ2dnUVZ8kadnZ2d@bt.com>
In reply to#23020
On 21/03/13 10:55, raphfrk@gmail.com wrote:
> Is there a way to get the path of the jar file that was passed on the command line?
>
> java -jar<some path>
>

http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file

Gives some good advice

The solution

String path = 
Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();
		String decodedPath = URLDecoder.decode(path, "UTF-8");

Works for me and gives me the path to the jar file containing the class 
that contains the above code.

lipska

-- 
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun

[toc] | [prev] | [next] | [standalone]


#23065

Fromraphfrk@gmail.com
Date2013-03-22 09:57 -0700
Message-ID<199c654e-c187-4885-aeac-ab6c38cb6b2c@googlegroups.com>
In reply to#23029
On Thursday, March 21, 2013 1:41:46 PM UTC, lipska the kat wrote:
> On 21/03/13 10:55, raph...@gmail.com wrote:
> 
> > Is there a way to get the path of the jar file that was passed on the command line?
> 
> >
> 
> > java -jar<some path>
> 
> >
> 
> 
> 
> http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file
> 
> 
> 
> Gives some good advice
> 
> 
> 
> The solution
> 
> 
> 
> String path = 
> 
> Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();
> 
> 		String decodedPath = URLDecoder.decode(path, "UTF-8");
> 
> 
> 
> Works for me and gives me the path to the jar file containing the class 
> 
> that contains the above code.

Thanks for the info.

[toc] | [prev] | [next] | [standalone]


#23044

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-03-21 14:11 -0700
Message-ID<iqtmk8hmilobjo1k4e2psk2goug3cm82sl@4ax.com>
In reply to#23020
On Thu, 21 Mar 2013 03:55:50 -0700 (PDT), raphfrk@gmail.com wrote,
quoted or indirectly quoted someone who said :

>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.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Every method you use to prevent or find bugs leaves a residue of subtler 
bugs against which those methods are ineffectual. 
 ~ Bruce Beizer  Pesticide Paradox

[toc] | [prev] | [next] | [standalone]


#23046

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-03-21 17:31 -0400
Message-ID<514b7c41$0$32116$14726298@news.sunsite.dk>
In reply to#23044
On 3/21/2013 5:11 PM, Roedy Green wrote:
> On Thu, 21 Mar 2013 03:55:50 -0700 (PDT), raphfrk@gmail.com wrote,
> quoted or indirectly quoted someone who said :
>
>> 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.

Is that relevant for the question?

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web