Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This question already has an answer here:

Is there a way to run this command line within a Java application?

java -jar map.jar time.rel test.txt debug

I can run it with command but I couldn't do it within Java.

share|improve this question

marked as duplicate by Jarrod Roberson java Sep 8 '15 at 16:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
I have tried, Runtime rt = Runtime.getRuntime();Process proc = rt.exec("ping localhost"); – Ataman Dec 13 '11 at 21:38
    
Do you want to launch a VM from within another VM? – chance Dec 13 '11 at 21:39
up vote 111 down vote accepted
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java -jar map.jar time.rel test.txt debug");

http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

share|improve this answer
    
is it normal that when i try to output it to console it only outputs something like this: java.lang.ProcessImpl@6ecec5288 – Ataman Dec 13 '11 at 21:40
7  
Use pr.getInputStream(). Here is a detailed example: linglom.com/2007/06/06/… – kol Dec 13 '11 at 21:43
5  
It's useful to check what the process returns with. You can get that with pr.waitFor(). So it looks like this: int retVal = pr.waitFor(). So if it's not 0, you can abort / clean up. – Shiki Dec 10 '13 at 15:32
    
Is there a practical/meaningful difference between pr.exitValue and pr.waitFor()? Update: I think exitValue will throw an exception if the process has not finished where as waitFor... you know – mmcrae Dec 11 '14 at 20:04
1  
nothing it's more java than "Runtime rt = Runtime.getRuntime();" – Andrea Bori Jun 21 '16 at 10:30

You can also watch the output like this:

final Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");

new Thread(new Runnable() {
    public void run() {
     BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
     String line = null; 

     try {
        while ((line = input.readLine()) != null)
            System.out.println(line);
     } catch (IOException e) {
            e.printStackTrace();
     }
    }
}).start();

p.waitFor();

And don't forget, if you are running in Windows, you need to put "cmd /c " in front of your command.

share|improve this answer
4  
You can also use p.getErrorStream to understand why your command is broken! – jakebeal Jan 29 '15 at 4:27
    
@Craigo: any reference regarding your "cmd /c" statement? thx – Campa Dec 15 '15 at 8:44
1  
Actually, I think you only need the "cmd /c", if you are wanting to run a windows command, like "copy". Apologies for the confusion. – Craigo Dec 15 '15 at 23:56

To avoid the called process to be blocked if it outputs a lot of data on the standard output and/or error, you have to use the solution provided by Craigo. Note also that ProcessBuilder is better than Runtime.getRuntime().exec(). This is for a couple of reasons: it tokenizes better the arguments, and it also takes care of the error standard output (check also here).

ProcessBuilder builder = new ProcessBuilder("cmd", "arg1", ...);
builder.redirectErrorStream(true);
final Process process = builder.start();

// Watch the process
watch(process);

I use a new function "watch" to gather this data in a new thread. This thread will finish in the calling process when the called process ends.

private static void watch(final Process process) {
    new Thread() {
        public void run() {
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null; 
            try {
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.start();
}
share|improve this answer
import java.io.*;

Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");

Consider the following if you run into any further problems, but I'm guessing that the above will work for you:

Problems with Runtime.exec()

share|improve this answer
Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");
share|improve this answer

Have you tried the exec command within the Runtime class?

Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug")

Runtime - Java Documentation

share|improve this answer
Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");
share|improve this answer

what about

public class CmdExec {

public static Scanner s = null;


public static void main(String[] args) throws InterruptedException, IOException {
    s = new Scanner(System.in);
    System.out.print("$ ");
    String cmd = s.nextLine();
    final Process p = Runtime.getRuntime().exec(cmd);

    new Thread(new Runnable() {
        public void run() {
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null; 

            try {
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();

    p.waitFor();
     }

 }
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.