1

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

1
  • You can use ProcessBuilder to achieve this task. Or instead of writing whole command in s single line break it and execute it. Commented May 27, 2011 at 10:15

2 Answers 2

2

To solve the issue,

cmd /c "command"

is enough.

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

Comments

0
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.