带有长参数的 java runtime.getRuntime.exec( cmd )
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6434009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java runtime.getRuntime.exec( cmd ) with long parameters
提问by AdamOutler
I'm making a frontend for a command line app. It has a very long The command line is something simliar to this:
我正在为命令行应用程序制作前端。它有一个很长的命令行与此类似:
public String liveShellCommand(){
String cmd="command mode --parameter arg --parameter2 arg2 --parameter3 arg3";
Runtime run = Runtime.getRuntime() ;
Process pr ;
try {
log.progress("sending command: " +cmd);
pr = run.exec( cmd );
pr.waitFor() ;
Everything seems to work until I add the "mode" switch into it. The "mode" switch executes from the command line. I've tried a few combinations splitting the parameters into an array which does not execute either. I think it has something to do with "mode" not having a -- in front of it, and it cannot have a -- in front of it.
在我将“模式”开关添加到其中之前,一切似乎都可以正常工作。“模式”开关从命令行执行。我尝试了几种组合,将参数拆分为一个也不执行的数组。我认为这与“模式”没有 - 在它前面,它不能在它前面有 - 有关。
What am I doing wrong?
我究竟做错了什么?
edit: I forgot to mention that all I can see is this: Debugger stopped on uncompilable source code. I'm using netbeans and it does not seem to print out a stack trace. It stops on the run.exec(cmd). Is there something wrong with java?
编辑:我忘了提到我所能看到的就是:调试器在无法编译的源代码上停止。我正在使用 netbeans,它似乎没有打印出堆栈跟踪。它在 run.exec(cmd) 上停止。java有什么问题吗?
I was able to use the ProcessBuilder in order to run it without just simply failing...
我能够使用 ProcessBuilder 来运行它而不仅仅是失败......
It parses "command" just fine, but when I add "command mode"
它解析“命令”就好了,但是当我添加“命令模式”时
java.io.IOException: Cannot run program "command mode": java.io.IOException: error=2, No such file or directory
So it can't parse that I guess.
所以我猜它无法解析。
回答by pimaster
+1 for sending the arguments through as an array.
+1 用于将参数作为数组发送。
Sending everything through as a string may work on some systems but fail on others.
将所有内容作为字符串发送可能在某些系统上有效,但在其他系统上可能会失败。
Process start = Runtime.getRuntime().exec(new String[]
{ "java", "-version" });
BufferedReader r = new BufferedReader(
new InputStreamReader(start.getErrorStream()));
String line = null;
while ((line = r.readLine()) != null)
{
System.out.println(line);
}
I know you have said that you tried sending the arguments through as an array of Strings without success but were you receiving a different type of error? If that other program has a log you might want to see what is going wrong. You could write a simple script that outputs the parameters it was called with to test what is actually coming through.
我知道您曾说过您尝试将参数作为字符串数组发送但没有成功,但是您收到了不同类型的错误吗?如果其他程序有日志,您可能想看看出了什么问题。您可以编写一个简单的脚本来输出调用它的参数,以测试实际通过的内容。
回答by Vaibhav Verma
Use ProcessBuilder and pass it a String[]
使用 ProcessBuilder 并传递一个 String[]
String[] cmmm = {arg3,arg4,arg5, arg6,arg7 };
ProcessBuilder pb = new ProcessBuilder(cmmm);
pb.directory(new File(tDir));
Process p = pb.start();
回答by AdamOutler
an Array was the answer. I also used an ArrayList because of the complexity of the commands. Anyways... Defined arraylist, added commands, converted to array, displayed array, sent commands.. Everything worked well. Each param must be in it's own String within the array.
一个数组就是答案。由于命令的复杂性,我还使用了 ArrayList。无论如何...定义数组列表,添加命令,转换为数组,显示数组,发送命令..一切正常。每个参数必须在它自己的数组中的字符串中。
List<String> list = new ArrayList<>();
list.add("command");
list.add("param");
String[] command = (String[]) list.toArray(new String[0]);
log.progress (list);
run.exec (command);