How to Execute Windows Commands Using Java - Change Network Settings
Runtime.getRuntime().exec("netsh");
See Runtime Javadoc.
EDIT: A later answer by leet suggests that this process is now deprecated. However, as per the comment by DJViking, this appears not to be the case: Java 8 documentation. The method is not deprecated.
Use ProcessBuilder
ProcessBuilder pb=new ProcessBuilder(command);pb.redirectErrorStream(true);Process process=pb.start();BufferedReader inStreamReader = new BufferedReader( new InputStreamReader(process.getInputStream())); while(inStreamReader.readLine() != null){ //do something with commandline output.}
You can run the command with Runtime.getRuntime().exec("<command>")
(eg. Runtime.getRuntime().exec("tree")
). But, this will only run executables found in path, not commands like echo
, del
, ... But only stuff like tree.com
, netstat.com
, ... To run regular commands, you will have to put cmd /c
before the command (eg Runtime.getRuntime().exec("cmd /c echo echo")
)