How To Execute Native Commands In Java?

眉间皱痕 提交于 2020-01-04 09:14:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!