ermanno.tedeschi
ermanno.tedeschi

Reputation: 96

Java: run executable program whit particular parameters

I need to run executable progam (.exe) in java. This program have two different operating modes: GUI and Command line. The syntax to launch the program from the command line is as follows : C:\Users\Ermanno\Desktop\ "programFolder"\"program.exe" /stext output.txt

in this way the program store the outoput in the file "output.txt".

I tired it:

Process p = new ProcessBuilder("C:\\Users\\Ermanno\\Desktop\\programFolder\\program.exe" ,"/stext a.txt").start();

does not create the output file.

I also tired to use a file batch that contains the command and run it to java but the result is the same.

Upvotes: 0

Views: 245

Answers (3)

ermanno.tedeschi
ermanno.tedeschi

Reputation: 96

I solved using file bath. This file contains the command.

String [] _s = {"cmd.exe", "/c", "start", "file.bat"}; Process pr = Runtime.getRuntime().exec(_s);

Upvotes: 0

Ayman
Ayman

Reputation: 1842

My experience was horrible with using the JDK ProcessBuilder and Runtime.getRuntime().exec. I then moved to Apache commons-exec. Here is an example:

String line = "AcroRd32.exe /p /h " + file.getAbsolutePath();
CommandLine cmdLine = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(cmdLine);

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328556

You need to pass each argument in a single string:

... program.exe", "/stext", "a.txt")...

Also make sure that you start a background thread which reads the output of the child process. If there is a problem, then the child will print an error message to it's standard output and if you don't actively read it, then this output will be lost.

For this, loop over the streams p.getInputStream() and p.getErrorStream().

The latter is especially important since you say "I also tired to use a file batch". Java doesn't do anything different than a batch script. If you can't run the command from batch, it won't work from Java, either.

Upvotes: 2

Related Questions