0

我面临以下问题:

我正在 Eclipse 中编写一个 java 应用程序。在我的应用程序中,我想启动一个 cmd 命令:

C:/Users/User1/Content-Integration Testing Framework/JDBC Connector/bin/connect -h

命令“connect -h”是一个运行良好的企业内部应用程序。如果我要使用命令行 a 将不得不像这样冒险我的当前目录:

cd C:/Users/User1/Content-Integration Testing Framework/JDBC Connector/bin/

然后我会输入connect -h 这很好用。但我并不确定如何在 Java 应用程序中执行此命令。

在这里,他们告诉我如何在 Java 应用程序中运行 cmd: How to use "cd" command using Java runtime?

但如果我这样做:

Runtime.getRuntime().exec("'C:/Users/User1/Content-Integration Testing Framework/JDBC Connector/bin/connect' -h");

日食告诉我:

java.io.IOException: Cannot run program "'C:/Users/User1/Content-Integration": CreateProcess error=2, Das System kann die angegebene Datei nicht finden
    at java.lang.ProcessBuilder.start(Unknown Source)

它在“内容集成”处切断了我的命令。

有人能帮助我吗?

4

3 回答 3

2

You should use the version of exec() that takes multiple args via a String array.

Runtime.exec(String s) will split your string using a tokenizer (this is why quoting the string won't work, and why you see the behaviour you do). If you resolve the executable and arguments yourself, and pass each as an array element in the above e.g.

String[] args = new String[]{"executable", "arg1", "arg2"};
Runtime.getRuntime().exec(args); // don't forget to collect stdout/err etc.

then you will bypass Runtime.exec(String s)'s splitting behaviour.

于 2012-07-30T08:53:56.303 回答
1

你有没有尝试过:

Runtime.getRuntime().exec("\"C:/Users/User1/Content-Integration Testing Framework/JDBC Connector/bin/connect\" -h");
于 2012-07-30T08:52:53.480 回答
0

This happens because your path contains spaces. Make sure to wrap it in "" and it will work.

Runtime.getRuntime().exec("\"C:/Users/User1/Content-Integration Testing Framework/JDBC Connector/bin/connect\" -h");
于 2012-07-30T09:33:39.717 回答