0

I am running a script in a java program using:

 Runtime.getRuntime().exec()

I am able to open the terminal application using this.

If I give command to run the script. It's happening but I am not able to get the logs in the terminal. I am using MAC. I want to get the logs in the terminal.

1
  • i am having a script to build the application war.i run the script in terminal and get it done.i am trying to create a runnable jar so if i run that it will automatically do the build. but in the mid if i want to stop the build its not possible in this way... so i want to write a program which will open the terminal application and run those scripts in that terminal window so that i can see the current logs,etc..... Commented Nov 21, 2013 at 6:58

3 Answers 3

1

You can use a Process variable to get what return from that command, and use method such as: getInputStream(), getOutputStream(), getErrorStream(). Example:

    Process p = null;
    try {
        p = Runtime.getRuntime().exec(....your stuff here)
        p.getOutputStream().close(); // close stdin of child

        InputStream processStdOutput = p.getInputStream();
        Reader r = new InputStreamReader(processStdOutput);
        BufferedReader br = new BufferedReader(r);
        String line;
        while ((line = br.readLine()) != null) {
             //System.out.println(line); // the output is here
        }

        p.waitFor();
    }
    catch (InterruptedException e) {
            ... 
    }
    catch (IOException e){
            ...
    }
    finally{
        if (p != null)
            p.destroy();
    }
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for the general design. But you need to be aware that the original code will block if the process writes more than 4KB of output. A better approach is to close p.getOutputStream, then read processStdInput until EOF and then call p.waitFor();
@AaronDigulla this prints the logs in the console window in eclipse. but i want to see the logs in terminal. i will export the project as runnable jar and when ever i run that jar it will execute the script and the user should see the current logs in terminal. currently its executing the script but i am not getting the logs in terminal.
@kaviyarasu: Show us how you start the executable JAR. If the code can print to the Eclipse console, then it can also print to a terminal. If it doesn't, then you are redirecting stdio somehow.
0

The Process object returned by the method call above has an getInputStream() method (as well as ones for the error and output streams). You have to read from those if you want to grap the inputs and outputs of your script.

For reference: http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html

Comments

0

in terminal, using > to output the log to file. For example: ls / > rootfolder.txt

Using that way, you can output the log to file and then read the log from the file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.