0

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.

4
  • Check cmevoli user solution: <stackoverflow.com/questions/3468987/…> Commented Nov 6, 2013 at 10:12
  • 2
    "it does not work" - everytime you write this here on SO without further explanation, you should loose a finger. Commented Nov 6, 2013 at 10:23
  • From now on, never use "does not work" again. It's an insult to your intelligence and it wastes everybody's time. Instead say what you expect to happen and what happened instead. Commented Nov 6, 2013 at 10:45
  • I edit the question sorry. Commented Nov 6, 2013 at 10:48

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

maybe the problem is windows 8?
No. Do you read and print the standard output of the process?
System.out.println(p.waitFor()); this as output from 0. If I run your command start the gui.
Can you post the code? If I print p.getInputStream() and p.getErrorStream() the output is this: java.io.BufferedInputStream@5636bc0a java.io.BufferedOutputStream@237360be
Use code similar to IOUtils.copyLarge.
|
1

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);

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.