开发者

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 s开发者_如何学运维ave this to a file.

Thanks


To solve the issue,

cmd /c "command"

is enough.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜