O
Oliver Wong
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...ang.String, java.lang.String[], java.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
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...ang.String, java.lang.String[], java.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