开发者

Can you set the current running directory from the java command line?

I am running a Java application from the command line. Can I specify a comma开发者_开发知识库nd line argument to set the current running directory to something other than where the application is actually going to run?


There is a JVM argument -Duser.dir which can be used to set working directory for JVM.


If it all possible I would rather use a script to run the java application and set the directory in the script:

#!/bin/sh
cd <your dir>
java <some arguments>

The JNI-solution may affect all kinds of relative paths in your application; for examples the classpath you put in.


If you want to change the current directory, you'll have to use JNI and invoke a native API from your Java code. For example, for Windows you would use SetCurrentDirectory


I found this SO post and it helped me solve my problem. Though I wanted to share something specific about IntelliJ that might trip somebody else up.

I have posted a picture below where the -Duser.dir flag is used and also the Working Directory text field is filled in.

Can you set the current running directory from the java command line?

In this situation, the Working Directory will be set to "JarLearning" rather than "2ndTry".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜