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

北城余情 提交于 2020-01-13 07:32:17

问题


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?


回答1:


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




回答2:


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.




回答3:


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




回答4:


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.

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



来源:https://stackoverflow.com/questions/4442722/can-you-set-the-current-running-directory-from-the-java-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!