Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a bash shell script in java

Tags:

java

shell

I want to run a shell script from my program below but it doesn't seem to do anything. I've run the same command directly in the linux terminal and it works fine so I'm guessing it's my java code. As you can see, I was first writing the command to the shell script using a PrintWriter but I expect that this would not effect the running of the shell script itself. Any help would be appreciated!

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    String nfdump = "nfdump -o csv -r /home/shane/Documents/nfdump/nfcapd.201211211526>blank.txt";

    try {
        FileWriter fw = new FileWriter("/home/shane/Documents/script.sh");

        PrintWriter pw = new PrintWriter(fw);

        pw.println("#!/bin/bash");
        pw.println(nfdump);

        pw.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    Process proc = null;

    try {
        proc = Runtime.getRuntime().exec("sh /home/shane/Documents/script.sh");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
like image 452
Shane Avatar asked Dec 04 '12 16:12

Shane


People also ask

How do you run a bash script in Java?

You should call Process#waitFor so that your program waits until the new process finishes. Then, you can invoke Process. html#getOutputStream() on the returned Process object to inspect the output of the executed command. An alternative way of creating a process is to use ProcessBuilder .

How do I run a terminal command in Java?

Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.

What is bash in Java?

BASH is an acronym for Bourne Again Shell, a punning name, which is a tribute to Bourne Shell (i.e., invented by Steven Bourne). Bash is a shell program written by Brian Fox as an upgraded version of Bourne Shell program 'sh'. It is an open source GNU project.


1 Answers

You should use the returned Process to get the result.

Runtime#exec executes the command as a separate process and returns an object of type Process. You should call Process#waitFor so that your program waits until the new process finishes. Then, you can invoke Process.html#getOutputStream() on the returned Process object to inspect the output of the executed command.

An alternative way of creating a process is to use ProcessBuilder.

Process p = new ProcessBuilder("myCommand", "myArg").start();

With a ProcessBuilder, you list the arguments of the command as separate arguments.

See Difference between ProcessBuilder and Runtime.exec() and ProcessBuilder vs Runtime.exec() to learn more about the differences between Runtime#exec and ProcessBuilder#start.

like image 101
reprogrammer Avatar answered Nov 09 '22 02:11

reprogrammer