devoured elysium
devoured elysium

Reputation: 105217

Running a Command Prompt from a Java program in Windows

I currently have the following batch script I want to run from my Java program:

"C:\Program Files\Java\jdk1.6.0_25\bin\java.exe" -classpath "D:..." Main > "...\result.out"

Now, I've done a simple

Runtime.getRuntime().exec(command);

where command is that string I have shown above. The problem is that it is simply calling java.exe with the shown arguments, instead of calling the console with the given arguments. The difference is subtle, for if it is calling directly java.exe it will ignore the redirect of the output stream!

Is there a easy way to do this? I've tried prefixing command with "cmd " but that didn't seem to help.

I'd like to stay away from having to read the output stream and then having to manually save this to a file.

Thanks

Upvotes: 1

Views: 540

Answers (2)

Akintayo Olusegun
Akintayo Olusegun

Reputation: 917

Process proc = Runtime.getRuntime().exec("acpi -b");                            

Now you can use proc.getInputStream() and proc.getOutputStream() like any normal input and output streams.

You can then write the contents to your output file.

This is the method I mostly use.

Upvotes: 0

devoured elysium
devoured elysium

Reputation: 105217

To solve the issue,

cmd /c "command"

is enough.

Upvotes: 2

Related Questions