Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Running command in linux environment fails

I am trying to execute a command via Runtime.getRuntime().exec().
When i run the following command in my linux bash it works fine.

Command: bash -c "npm -v"

But when i try to run it with Java it fails with the following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

-v": -c: line 0: unexpected EOF while looking for matching `"'
-v": -c: line 1: syntax error: unexpected end of file

Reproducible example:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class RunACommandTest
{

   public static void main(String[] args)
   {
      try
      {
         Process exec = Runtime.getRuntime().exec("bash -c \"npm -v\"");

         new BufferedReader(new InputStreamReader(exec.getInputStream(), StandardCharsets.UTF_8))
               .lines()
               .forEachOrdered(line -> System.out.println("IN " + line));

         new BufferedReader(new InputStreamReader(exec.getErrorStream(), StandardCharsets.UTF_8))
               .lines()
               .forEachOrdered(line -> System.out.println("ERR " + line));

      }
      catch(IOException e)
      {
         throw new RuntimeException(e);
      }
   }

}

I have also tried to use single quotes instead of double quotes.

>Solution :

You should separate out the parameter list. It is easier to use ProcessBuilder rather than Runtime.exec:

ProcessBuilder pb = new ProcessBuilder("bash","-c", "npm -v");
Process exec = pb.start();

Note that you must consume stdout + stderr in separate threads. What you have above may work, but it may freeze for other processes or conditions. You can avoid by redirect to file, or redirect error to stdout with:

pb.redirectErrorStream(true);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading