问题
I know very well how to execute commands via Runtime.getRuntime().exec(command) and handle the output, but this is VERY limited.
Take Windows for example (Vista specifically but that shouldn't matter).
How can I execute commands like 'echo', 'cd', 'md', 'rd', and any other command rooted inside of the cmd.exe through ProcessBuilder (or whatever class) in Java?
回答1:
You just call cmd /c dir
rather than just dir
in Runtime.exec or ProcessBuilder or some other way to run external applications in JAVA.
回答2:
Another trick is to leverage the Ant libraries and put together a programmed version of <exec ...>
(Ant Exec Task). This also gives you access to all kinds of Ant-supported input/output processing and filtering.
This is not writing an Ant XML script here, but calling Ant from your code.
来源:https://stackoverflow.com/questions/8190394/how-to-execute-native-commands-in-java