简体   繁体   English

带有长参数的java runtime.getRuntime.exec(cmd)

[英]java runtime.getRuntime.exec( cmd ) with long parameters

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. 我正在使用netbeans,它似乎无法打印出堆栈跟踪。 It stops on the run.exec(cmd). 它在run.exec(cmd)上停止。 Is there something wrong with java? 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. 所以我无法解析。

+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. 您可以编写一个简单的脚本,输出用于调用的参数以测试实际通过的参数。

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();

an Array was the answer. 数组就是答案。 I also used an ArrayList because of the complexity of the commands. 由于命令的复杂性,我还使用了ArrayList。 Anyways... Defined arraylist, added commands, converted to array, displayed array, sent commands.. Everything worked well. 无论如何...定义了arraylist,添加了命令,转换为数组,显示了数组,发送了命令。 Each param must be in it's own String within the array. 每个参数都必须在数组中自己的String中。

    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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM