sethulekshmis
sethulekshmis

Reputation: 21

Execute command using Java on Windows

I want to execute a command mspview -r "C:\\Users\\SS\\Desktop\\phantomjs-1.9.2-windows\\image.tif". How can I do it via Java code? I am trying to do this with a batch file. The same command when I run with the help of RUN. I am getting correct output. I have executed a .exe program with the help of a batch file with the following code C:\Users\SS\Desktop\phantomjs-1.9.2-windows\phantomjs.exe.

Upvotes: 0

Views: 212

Answers (2)

sandymatt
sandymatt

Reputation: 5612

You're basically asking how to run shell commands in java, right?

Runtime.getRuntime().exec("whatever system call you want");

Upvotes: 1

RamonBoza
RamonBoza

Reputation: 9038

You need to use ProcessBuilder

Process process = new ProcessBuilder(
"C:\\PathToExe\\exe.exe","param1","param2").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

System.out.printf("Output of running %s is:", Arrays.toString(args));

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

code that is already found on stackoverflow Execute external program in java

Upvotes: 0

Related Questions