java runtime.getRuntime.exec( cmd ) with long parameters

倖福魔咒の 提交于 2019-12-02 17:07:30

问题


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?

I was able to use the ProcessBuilder in order to run it without just simply failing...

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:


+1 for sending the arguments through as an array.

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.




回答2:


Use ProcessBuilder and pass it a String[]

     String[] cmmm = {arg3,arg4,arg5, arg6,arg7 };
     ProcessBuilder pb = new ProcessBuilder(cmmm);
     pb.directory(new File(tDir));
     Process p = pb.start();



回答3:


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.

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


来源:https://stackoverflow.com/questions/6434009/java-runtime-getruntime-exec-cmd-with-long-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!