0

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.

5
  • 1
    Java shouldn't care if the command you run has -- in front of its parameters or not - it's just executing the command in the shell as you specify it. Could it be that there is something this "command" requires in your environment that isn't getting pulled in because of the way java is running its shell? Commented Jun 22, 2011 at 1:50
  • It does.. I learned this when I tried "gksudo myCommand parameter" It wraps each space in its own quotation. the gksudo example shows up as 3 separate commands. Doesn't the exec run the command on the system? Commented Jun 22, 2011 at 2:08
  • 1
    Not exactly. I'm not sure, so don't mark my comment down if I'm wrong, but maybe try escaping the space with \ Commented Jun 22, 2011 at 2:18
  • 1
    Yep, I should have done my due diligence before posting my comment. As you say, it wraps each parameter delimited by a space in its own string. Have you tried making an array out of the parameters and passing it to that overloaded exec call? Also, I've seen a lot of mention of the ProcessBuilder class - maybe that would help you (if you're running Java 1.5+). Commented Jun 22, 2011 at 4:10
  • I actually just spent the last hour learning about Process Builder. It gets me further along. but no cigar... thanks.. I updated the first post.. I was able to get an error at least. Commented Jun 22, 2011 at 4:18

3 Answers 3

7

+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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have not tried with process builder but it tails with exec because it sees mode as another command.
Process.exec fails at this. ProcessBuilder works. I ended up using a list which is much easier then an array.
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.