user4913383
user4913383

Reputation: 171

Runtime.getRuntime().exec() not working

I am trying to run a jar file from a java program. When I execute following code nothing happens. Name of jar file is Testing.jar. File paths are correct still neither an exception is thrown nor program from Testing.jar runs.

package helloworld;

import java.io.IOException;
import java.util.Scanner;

public class HelloWorld {

    public static void main(String[] args) throws IOException {
        Runtime.getRuntime().exec("java -jar C:\\Users\\Home\\Documents\\NetBeansProjects\\Testing\\dist\\Testing.jar");
    }

}

This same jar file runs correctly from CMD. I used following command for that:

java -jar "C:\\Users\\Home\\Documents\\NetBeansProjects\\Testing\\dist\\Testing.jar"

Upvotes: 0

Views: 2454

Answers (1)

dogant
dogant

Reputation: 1386

Created process does not have its own console so you need to provide one to see it's running.

Process testing= Runtime.getRuntime().exec("java -jar C:\\Users\\Home\\Documents\\NetBeansProjects\\Testing\\dist\\Testing.jar");
BufferedInputStream testOutput= new BufferedInputStream(testing.getInputStream());
int read = 0;
byte[] output = new byte[1024];
while ((read = testOutput.read(output)) != -1) {
    System.out.println(output[read]);
}

Upvotes: 3

Related Questions