Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute system commands (linux/bsd) using Java

I am attempting to be cheap and execute a local system command (uname -a) in Java. I am looking to grab the output from uname and store it in a String. What is the best way of doing this? Current code:

public class lame {

    public static void main(String args[]) {
        try {
            Process p = Runtime.getRuntime().exec("uname -a");
            p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line=reader.readLine();

            while (line != null) {    
                System.out.println(line);
                line = reader.readLine();
            }

        }
        catch(IOException e1) {}
        catch(InterruptedException e2) {}

        System.out.println("finished.");
    }
}
like image 679
Eric Schulman Avatar asked Apr 27 '09 01:04

Eric Schulman


People also ask

Can we run Linux command in Java?

You can use java. lang. Runtime. exec to run simple code.

How do I run a system command in Java?

After obtaining a Runtime object, you can run a system command by either passing a complete system command with arguments as a one-string or an array of strings with the command and each argument as separate strings to the exec method. As soon as the “exec” method is called the input command will be run.

How to execute a system command in Java?

Basically, to execute a system command, pass the command string to the exec () method of the Runtime class. The exec () method returns a Process object that abstracts a separate process executing the command. From the Process object we can get outputs from and send inputs to the command. The following code snippet explains the principle:

How to run a Java program in Linux?

To run the java program in Linux, we need to verify if Java Development Kit (JDK) is available in the system and its version. To confirm it, type the following command: ( Javac command-line tool is used for the compilation of java programs) The Javac command tool is not available in my system.

How to execute a string command in a separate process?

Runtime class has a exec (String command) method that Executes the specified string command in a separate process. Before invoking this method, we must ensure that running program on correct OS. Runtime.getRuntime().exec("sh -c chmod -R 777 /home/java_w3schools/start.sh"); Changing the file permissions to read/write/execute to all users.

How to compile and execute a Java program in terminal?

Compile the testing.java file on the terminal using the javac command: Now, execute the Java program by calling its class name in the terminal: Java is the high-level language of the modern era supported by the Java Development Kit (JDK). JDK is a package that helps to run java and is used for the development of software packages.


4 Answers

Your way isn't far off from what I'd probably do:

Runtime r = Runtime.getRuntime(); Process p = r.exec("uname -a"); p.waitFor(); BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = "";  while ((line = b.readLine()) != null) {   System.out.println(line); }  b.close(); 

Handle whichever exceptions you care to, of course.

like image 123
John Feminella Avatar answered Sep 22 '22 01:09

John Feminella


That is the best way to do it. Also you can use the ProcessBuilder which has a variable argument constructor, so you could save a line or two of code

like image 29
Azder Avatar answered Sep 25 '22 01:09

Azder


What you are doing looks fine. If your command is only returning a single string, you don't need the while loop, just store the reader.readLine() value in a single String variable.

Also, you probably should do something with those exceptions, rather than just swallowing them.

like image 42
Andy White Avatar answered Sep 23 '22 01:09

Andy White


I know this is very old but still...

Reading the article here: http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html
It is my understanding that you should first read the output and error streams of your executed command and only then waitFor the return value of your process.

like image 44
epeleg Avatar answered Sep 24 '22 01:09

epeleg