Can you set the current running directory from the

2019-04-18 13:00发布

I am running a Java application from the command line. Can I specify a command line argument to set the current running directory to something other than where the application is actually going to run?

4条回答
The star\"
2楼-- · 2019-04-18 13:24

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

查看更多
Rolldiameter
3楼-- · 2019-04-18 13:24

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.

"Working Directory" field has lower precedence than the -Duser.dir flag.

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

查看更多
姐就是有狂的资本
4楼-- · 2019-04-18 13:27

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.

查看更多
相关推荐>>
5楼-- · 2019-04-18 13:29

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

查看更多
登录 后发表回答