Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute external program

I tried to make an application that calls an external program that I have to pass two parameters. It doesn't give any errors.

The program.exe, written in C++, takes a picture and modifies the content of a .txt file.

The Java program runs but it does nothing-

Here is my sample code:

    String[] params = new String [3];     params[0] = "C:\\Users\\user\\Desktop\\program.exe";     params[1] = "C:\\Users\\user\\Desktop\\images.jpg";     params[2] = "C:\\Users\\user\\Desktop\\images2.txt";     Runtime.getRuntime().exec(params); 
like image 584
sqtd Avatar asked Dec 21 '12 13:12

sqtd


People also ask

How do I run a Java program from another program?

In JavaSW, you can run another application by calling the exec method on the Runtime object, which may be obtained via a call to Runtime. getRuntime().


2 Answers

borrowed this shamely from here

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.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); } 

More information here

Other issues on how to pass commands here and here

like image 71
Steven Avatar answered Oct 11 '22 23:10

Steven


You might also try its more modern cousin, ProcessBuilder:

Java Runtime.getRuntime().exec() alternatives

like image 29
duffymo Avatar answered Oct 11 '22 23:10

duffymo