gpt4 book ai didi

java - 如何在 Java 中更改目录并执行其中的文件

转载 作者:行者123 更新时间:2023-11-30 02:43:26 24 4
gpt4 key购买 nike

我必须在Java中执行Windows的以下cmd命令。

cmd 命令:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

//First i have to change my default directory//

C:\Users\shubh>D:

//Then move to a specific folder in the D drive.//

D:\>cd MapForceServer2017\bin\

//then execute the .mfx file from there.

D:\MapForceServer2017\bin>mapforceserver run C:\Users\shubh\Desktop\test1.mfx

执行结果

Output files:
library: C:\Users\shubh\Documents\Altova\MapForce2017\MapForceExamples\Tutorial\library.xml
Execution successful.

最佳答案

我建议使用https://commons.apache.org/proper/commons-exec/用于从 Java 内部执行 o/s 命令,因为它处理您以后可能遇到的各种问题。

您可以使用:

CommandLine cmdLine = CommandLine.parse("cmd /c d: && cd MapForceServer2017\\bin\\ && mapforceserver run ...");
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(cmdLine);

关于java - 如何在 Java 中更改目录并执行其中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40945720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com