gpt4 book ai didi

java - 尝试使用 apache commons-exec jar 打开 cmd 时出错

转载 作者:行者123 更新时间:2023-11-29 11:32:49 25 4
gpt4 key购买 nike

我正在尝试从 apache commons-exec 进行 mysql 转储,但出现以下错误

Exception in thread "main" java.io.IOException: Cannot run program "cmd.exe \c" (in directory "."): CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:470) at java.lang.Runtime.exec(Runtime.java:593) at org.apache.commons.exec.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:61) at org.apache.commons.exec.DefaultExecutor.launch(DefaultExecutor.java:279) at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:336) at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166) at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:153) at com.etq.e2mc.platform.windows.WindowsProcess.execCommons(WindowsProcess.java:87) at com.etq.e2mc.platform.windows.WindowsProcess.main(WindowsProcess.java:79) Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(ProcessImpl.java:177) at java.lang.ProcessImpl.start(ProcessImpl.java:28) at java.lang.ProcessBuilder.start(ProcessBuilder.java:452) ... 8 more

这是我正在使用的代码,它非常简单和直接,但我无法弄清楚为什么它不调用cmd(注意:尝试在没有cmd的情况下直接调用mysql转储,并且我得到相同类型的错误),希望得到任何帮助

    public static void main(String[] args) throws Exception {
execCommons();
}

public static void execCommons() throws ExecuteException, IOException {
CommandLine cmd = new CommandLine("cmd.exe /c");
cmd.addArguments("mysqldump");
cmd.addArguments(new String[] { "-u", "root", " -P", "3316", " -h", "localhost", " -A", ">"});
cmd.addArguments("\"G:\\test.sql \"" , false);
new DefaultExecutor().execute(cmd);

}

最佳答案

出于某种原因,commons-exec 似乎不喜欢在问题中使用命令措辞(使用 "cmd.exe/c" 初始化 CommandLine ),将其改写为以下内容后,一切似乎都正常工作

    public static void main(String[] args) throws Exception {
execCommons();
}

public static void execCommons() throws ExecuteException, IOException {
CommandLine cmd = new CommandLine("cmd.exe ");
cmd.addArgument("/c");
String command = "mysqldump " + "-u" + "root" + " -P" + "3316" + " -h" + "localhost" + " -A >" + "\"G:\\test.sql \"";
cmd.addArgument(command,false);
new DefaultExecutor().execute(cmd);
}

我不知道为什么它会这样工作,因为文档中没有说明,但我将其留在这里,以防它对某人有帮助。但如果有人有任何想法请告诉

关于java - 尝试使用 apache commons-exec jar 打开 cmd 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37116062/

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