Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How does one locate the java executable and various JAR files in the classpath?

Reply
Thread Tools

How does one locate the java executable and various JAR files in the classpath?

 
 
Oliver Wong
Guest
Posts: n/a
 
      12-09-2005
Note: This post mentions Eclipse to show what parts of the design I have
no control over, but I believe you don't actually need to know much about
Eclipse development to help contribute a useful answer.

I have a JAR file which does some useful stuff, including generating
some files into the "current directory". That is, it creates new File
objects using relative paths, and so these files appear relative to the
directory from which the JAR was invoked. I can read the source code for
this JAR, but I'm pretty much not allowed to make changes to the code in it.

I'm writing a plugin for Eclipse, and one of the functionalities is to
invoke this JAR. That is, I want to give the user the ability to right click
somewhere in their project hierarchy, and select "Generate files here" from
the context menu, and in responce to that, I'll invoke the JAR file, passing
it command line arguments based on settings that the user specified earlier.

The cleanest solution, I think, would have been to invoke the static
main function in the JAR directly, 'cause then I could catch any exceptions
thrown from there and handle them specially, but I think there is no way to
set the "current directory" in Java, and so doing this would result in my
not being able to control where the files get generated.

The next solution that came to my mind was to use Runtime.exec(String,
String[], File)

http://java.sun.com/j2se/1.5.0/docs/...va.lang.String[],%20java.io.File)

Thanks to the last argument, I can specify the working directory for the
subprocess.

However, I'm not at a lost of how to construct the String for the first
parameter. I want to run the "java" interpreter, something of the form:
"java -jar myHelper.jar Other Command Line Arguments", but this assumes both
that "java" and "myHelper.jar" can be found from the current working
directory, which will not nescessarily be the case.

Any advice on how to find both the java executable and the jar file? Or
how to programmatically affect the current working directory (so that I may
use the original cleaner solution)?

- Oliver


 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      12-09-2005
Oliver Wong wrote:

> Any advice on how to find both the java executable and the jar file?


URL urlToClass = this.getClass().getResource(
"get/any/class/in/mysterious/Jar.class" );

I am sure there is also something more specific for classes,
just cannot recall it.

--
Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
 
Reply With Quote
 
 
 
 
Rhino
Guest
Posts: n/a
 
      12-09-2005

"Oliver Wong" <(E-Mail Removed)> wrote in message
news:qllmf.143932$S4.136005@edtnps84...
> Note: This post mentions Eclipse to show what parts of the design I
> have no control over, but I believe you don't actually need to know much
> about Eclipse development to help contribute a useful answer.
>
> I have a JAR file which does some useful stuff, including generating
> some files into the "current directory". That is, it creates new File
> objects using relative paths, and so these files appear relative to the
> directory from which the JAR was invoked. I can read the source code for
> this JAR, but I'm pretty much not allowed to make changes to the code in
> it.
>
> I'm writing a plugin for Eclipse, and one of the functionalities is to
> invoke this JAR. That is, I want to give the user the ability to right
> click somewhere in their project hierarchy, and select "Generate files
> here" from the context menu, and in responce to that, I'll invoke the JAR
> file, passing it command line arguments based on settings that the user
> specified earlier.
>
> The cleanest solution, I think, would have been to invoke the static
> main function in the JAR directly, 'cause then I could catch any
> exceptions thrown from there and handle them specially, but I think there
> is no way to set the "current directory" in Java, and so doing this would
> result in my not being able to control where the files get generated.
>
> The next solution that came to my mind was to use Runtime.exec(String,
> String[], File)
>
> http://java.sun.com/j2se/1.5.0/docs/...va.lang.String[],%20java.io.File)
>
> Thanks to the last argument, I can specify the working directory for
> the subprocess.
>
> However, I'm not at a lost of how to construct the String for the first
> parameter. I want to run the "java" interpreter, something of the form:
> "java -jar myHelper.jar Other Command Line Arguments", but this assumes
> both that "java" and "myHelper.jar" can be found from the current working
> directory, which will not nescessarily be the case.
>
> Any advice on how to find both the java executable and the jar file? Or
> how to programmatically affect the current working directory (so that I
> may use the original cleaner solution)?
>

Wouldn't it be more appropriate to ask your questions on a suitable Eclipse
forum? (Don't ask me which one: there are lots of them and I always get lost
trying to find the right one

Rhino


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      12-09-2005

"Andrew Thompson" <(E-Mail Removed)> wrote in message
newsynmf.15765$(E-Mail Removed)...
> Oliver Wong wrote:
>
>> Any advice on how to find both the java executable and the jar file?

>
> URL urlToClass = this.getClass().getResource(
> "get/any/class/in/mysterious/Jar.class" );
>
> I am sure there is also something more specific for classes,
> just cannot recall it.


I had actually tried:

this.getClass().getClassLoader().getResource("myMy steriousJARFile.jar");
//[1]

.... but null was returned. The JAR file is on my classpath at compile time,
but I'm not so sure it's there at invocation time when Eclipse invokes my
plugin (though I suspect it should be, or else how could I call methods
located within that JAR?)

I also strongly suspect that Eclipse uses its own custom classloader for
loading plugins. Not sure how that might affect the above code's ability to
locate my JAR.

- Oliver

[1]: I want the JAR file, and not any particular class within the JAR,
because I want to construct a string to pass to Runtime.exec(). Something
like Runtime.exec("java -jar /path/to/jar/myMysteriousJARFile.jar", null,
myDesiredWorkingDirectory); I was hoping to take the URL from the
classloader, assuming it points to the local filesystem, and then convert it
to an absolute path using the File class.


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      12-09-2005

"Rhino" <(E-Mail Removed)> wrote in message
news:Kznmf.9599$(E-Mail Removed).. .

> Wouldn't it be more appropriate to ask your questions on a suitable
> Eclipse forum? (Don't ask me which one: there are lots of them and I
> always get lost trying to find the right one


I posted it to eclipse.newcomer on Eclipse.org's usenet server, but
since I thought the question doesn't specifically rely on knowledge of
eclipse, it might be worth asking it here as well. Hopefully people will
excuse the multi-posting as the messages lie on completely detached servers.

- Oliver


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-09-2005
On Fri, 09 Dec 2005 22:28:45 GMT, Andrew Thompson
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>I am sure there is also something more specific for classes,
>just cannot recall it.


you were thinking of getProctectionDomain?

import java.net.URL;
import java.security.CodeSource;

/**
* Find out where a class on the classpath will be loaded from.
* Fully qualified classname goes on the command line.
*/
public class Where
{
/**
* main
* @param args name of fully qualified class to find, using dots,
but no dot class.
* e.g. java.exe Where javax.mail.internet.MimeMessage
*/
public static void main ( String[] args )
{
try
{
String qualifiedClassName = args[0];
Class qc = Class.forName( qualifiedClassName );
CodeSource source = qc.getProtectionDomain().getCodeSource();
if ( source != null )
{
URL location = source.getLocation();
System.out.println ( qualifiedClassName + " : " + location
);
}
else
{
System.out.println ( qualifiedClassName + " : " + "unknown
source, likely rt.jar" );
}
}
catch ( Exception e )
{
System.err.println( "Unable to locate class on command line."
);
e.printStackTrace();
}
}
}
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      12-09-2005
Roedy Green wrote:

> On Fri, 09 Dec 2005 22:28:45 GMT, Andrew Thompson
> <(E-Mail Removed)> wrote, quoted or indirectly quoted someone
> who said :
>
>>I am sure there is also something more specific for classes,
>>just cannot recall it.

>
> you were thinking of getProctectionDomain?


Spot on ..well, without the initial 'c', but yes.

--
Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      12-09-2005

"Roedy Green" <(E-Mail Removed) > wrote in
message news:(E-Mail Removed)...
>
> import java.net.URL;
> import java.security.CodeSource;
>
> /**
> * Find out where a class on the classpath will be loaded from.
> * Fully qualified classname goes on the command line.
> */
> public class Where
> {
> /**
> * main
> * @param args name of fully qualified class to find, using dots,
> but no dot class.
> * e.g. java.exe Where javax.mail.internet.MimeMessage
> */
> public static void main ( String[] args )
> {
> try
> {
> String qualifiedClassName = args[0];
> Class qc = Class.forName( qualifiedClassName );
> CodeSource source = qc.getProtectionDomain().getCodeSource();
> if ( source != null )
> {
> URL location = source.getLocation();
> System.out.println ( qualifiedClassName + " : " + location
> );
> }
> else
> {
> System.out.println ( qualifiedClassName + " : " + "unknown
> source, likely rt.jar" );
> }
> }
> catch ( Exception e )
> {
> System.err.println( "Unable to locate class on command line."
> );
> e.printStackTrace();
> }
> }
> }


Thanks, after embedding your code into my project, I was able to confirm
that the JAR I was seeking was not on the classpath at runtime. I've fixed
that, and now your code does return the correct path to the JAR file. You've
helped me a lot. Thank you.

- Oliver


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      12-10-2005
"Oliver Wong" <(E-Mail Removed)> wrote in message
news:EQomf.144006$S4.93460@edtnps84...
> I've fixed that, and now your code does return the correct path to the JAR
> file.


Just in case anyone is curious, I solved the other problem, of locating
Sun's java executable, like this:

System.getProperty("java.home") + "/bin/java";

- Oliver


 
Reply With Quote
 
Rhino
Guest
Posts: n/a
 
      12-10-2005

"Oliver Wong" <(E-Mail Removed)> wrote in message
news:C3omf.143997$S4.102476@edtnps84...
>
> "Rhino" <(E-Mail Removed)> wrote in message
> news:Kznmf.9599$(E-Mail Removed).. .
>
>> Wouldn't it be more appropriate to ask your questions on a suitable
>> Eclipse forum? (Don't ask me which one: there are lots of them and I
>> always get lost trying to find the right one

>
> I posted it to eclipse.newcomer on Eclipse.org's usenet server, but
> since I thought the question doesn't specifically rely on knowledge of
> eclipse, it might be worth asking it here as well. Hopefully people will
> excuse the multi-posting as the messages lie on completely detached
> servers.
>

I'm not complaining

Rhino


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Running an Executable Before Executable JAR Jason Cavett Java 8 05-16-2007 07:14 PM
java -cp a.jar -jar b.jar => Works on Windows, not on Debian cyberco Java 4 02-14-2006 06:27 AM
Re: Can I make .class files run like executable .jar files on a Windows platform? kamal Java 0 03-10-2005 10:42 AM
Can I make .class files run like executable .jar files on a Windows platform? phillipedison1891@yahoo.com Java 3 03-09-2005 10:44 PM
Classpath problem: javap is able to locate one class in a jar but not the other. Chandresh Prakash Java 1 12-05-2003 12:43 PM



Advertisments