开发者

Method.invoke() vs. RunTime.exec()

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 actuall开发者_运维知识库y make a call to 'java -jar app.jar'...

thanks in advance for your help David


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜