如何在Java程序中更改当前工作目录?我能找到的关于这个问题的一切声称你根本做不到,但我不相信那是真的.
我有一段代码,它使用来自通常启动的目录的硬编码相对文件路径打开文件,我只是希望能够在不同的Java程序中使用该代码,而无需从内部启动它一个特定的目录.看起来你应该只能调用System.setProperty( "user.dir", "/path/to/dir" ),但据我所知,调用该行只是默默地失败并且什么都不做.
我会理解,如果Java不允许你这样做,如果它不是因为它允许你获得当前的工作目录,甚至允许你使用相对文件路径打开文件....
我正在使用带有以下命令的Exec Maven插件:
mvn exec:java
我没有设法用这种执行模式设置工作目录.我想使用mainClass(在特定的包中),我希望我的执行的根文件夹在$ {basedir}之外的另一个目录中.
谢谢您的帮助.
我的pom.xml目标<workingDirectory>对我不起作用:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<workingDirectory>${project.build.directory}\classes</workingDirectory>
<mainClass>com.package.MyMainClass</mainClass>
<includeProjectDependencies>true</includeProjectDependencies>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
使用-X选项的结果
[DEBUG] Configuring mojo org.codehaus.mojo:exec-maven-plugin:1.3.2:java from plugin realm ClassRealm[plugin>org.codehaus.mojo:exec-maven-plugin:1.3.2,parent: sun.misc.Launcher$AppClassLoader@11b86e7]
[DEBUG] Configuring mojo 'org.codehaus.mojo:exec-maven-plugin:1.3.2:java' with basic configurator -->
[DEBUG] (f) arguments = []
[DEBUG] (f) classpathScope = runtime
[DEBUG] (f) cleanupDaemonThreads = true
[DEBUG] (f) daemonThreadJoinTimeout = 15000
[DEBUG] (f) includePluginDependencies = false
[DEBUG] (f) includeProjectDependencies = true
[DEBUG] (f) keepAlive = false
[DEBUG] (f) killAfter = 1 …Run Code Online (Sandbox Code Playgroud)