tags:

views:

98

answers:

3

Hi All,

when using RunTime.exec(), one can specify the working directory of the subprocess (very useful, if the executed app can only be launched from its location as it access some local config files for instance)

If the external app is a jar file, on can read the manifest in the jar to get information about the main class and the classpath and then call invoke() to run the application...

Is there a way to set the working directory there too ? I cannot seem to find a way for doing that? Or do I have to use RunTime.exec() and actually make a call to 'java -jar app.jar'...

thanks in advance for your help David

+1  A: 

I believe the answer is no. You're going to have to use Runtime.exec(), I'm afraid.

Brian Agnew
+4  A: 

I would say that if you have an application that you have written that depends on what the working directory is set to you have done it wrong. Make use of Class.getResource and Class.getResoruceAsStream and passing fill paths to the arguments for "main(String[])" to remove path dependencies.

Also there is the System.getProperty("user.dir"); method to get the working direcvtory, but you cannot call System.setProperty("user.dir" ... ) to change it.

TofuBeer
I haven't written the external apps.. that's why I'm stuck. I'll have a look at Class.getResource and getResourceAsStream and see what I can do with them
DavidM
Ah... well if the apps already exist and depend on the working directory you can a) smack the developer who wrote the broken code upside the head :-), and then b) use Runtime.exec, and then c) if you have the ability to modify the other applications to not depend on the working directory do that and change your code from using Runtime.exec :-)
TofuBeer
smack him in the head..mmm I like that :-)... if only I knew who he was :-)
DavidM
A: 

According to Mindprod site and this article, Since the current directory cannot change throughout the execution of a program, the File constructor does not need to embed it in each File object.

chburd