Can't use && operator in Runtime.exec () + linux

I am trying to run an executable from Java using the code pasted below. Using the && operator in the terminal, I can both navigate and run the executable with a single command. I am trying to pass the same command using Runtime.getRuntime () command. Exec (), but she doesn't seem to like the && operator. Does anyone know about this? In the code below, I am simply passing in "cd && pwd" as a test case; an even simpler command, but still doesn't work. Thanks to

                int c;
                textArea.setText("Converting Source Code to XML");
                //String[] commands = {"/bin/bash", "-c", "cd /home/Parallel/HomeMadeXML", "&&", "./src2srcml --position" + selectedFile.getName() + "-o targetFile.xml"};
                String commands = "bash -c cd && pwd";
                Process src2XML = Runtime.getRuntime().exec(commands);
                InputStream in1 = src2XML.getErrorStream();
                InputStream in2 = src2XML.getInputStream();
                while ((c = != -1 || (c = != -1) {
            catch(Exception exc){/*src2srcml Fail*/}



source to share

1 answer

You want to run a command, you must use exactly three arguments: the bash

executable, the -c

flag for executing the command, and the third shell command.

You are trying to pass 5 where the last three are separate chunks of the same command.

Here's an example. Note that the shell command is only one argument:

String[] commands = { "/bin/bash", "-c", "cd /tmp && touch foobar" };


When executed, you will find the file foobar

created in /tmp




All Articles