How does one locate the java executable and various JAR files in the classpath? [message #123082] |
Fri, 09 December 2005 18:01  |
Eclipse User |
|
|
|
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/api/java/lang/Runtime.ht ml#exec(java.lang.String,%20java.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
|
|
|
FIXED (WAS Re: How does one locate the java executable and various JAR files in the classpath?) [message #123108 is a reply to message #123082] |
Fri, 09 December 2005 18:57  |
Eclipse User |
|
|
|
"Oliver Wong" <owong@castortech.com> wrote in message
news:dnd2d6$7bp$1@news.eclipse.org...
>
> 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)?
A kind soul posted the following code which allows me to get the path to
the JAR file when you give it a class name:
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();
}
}
}
- Oliver
|
|
|
Powered by
FUDForum. Page generated in 0.08591 seconds