Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Execute Windows Commands Using Java - Change Network Settings

In Java, I want to be able to execute a Windows command.

The command in question is netsh. This will enable me to set/reset my IP address.

Note that I do not want to execute a batch file.

Instead of using a batch file, I want to execute such commands directly. Is this possible?


Here is my implemented Solution for Future Reference:

public class JavaRunCommand {     private static final String CMD =          "netsh int ip set address name = \"Local Area Connection\" source = static addr = 192.168.222.3 mask = 255.255.255.0";     public static void main(String args[]) {          try {             // Run "netsh" Windows command             Process process = Runtime.getRuntime().exec(CMD);              // Get input streams             BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));             BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));              // Read command standard output             String s;             System.out.println("Standard output: ");             while ((s = stdInput.readLine()) != null) {                 System.out.println(s);             }              // Read command errors             System.out.println("Standard error: ");             while ((s = stdError.readLine()) != null) {                 System.out.println(s);             }         } catch (Exception e) {             e.printStackTrace(System.err);         }     } } 
like image 313
mre Avatar asked Aug 18 '11 18:08

mre


People also ask

How do you execute a command in Java?

Executing a Command from String Process process = Runtime. getRuntime(). exec("ping www.stackabuse.com"); Running this code will execute the command we've supplied in String format.

How do I get Java to work in command prompt?

Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.

How do I run a command prompt as administrator in Java?

runtimeProcess = Runtime. getRuntime(). exec(new String[] { "runas /profile /user:Administrator \"cmd.exe", executeCmd }); Does any one has any idea regarding this.


2 Answers

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.

like image 100
badroit Avatar answered Sep 17 '22 09:09

badroit


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. } 
like image 42
leet Avatar answered Sep 21 '22 09:09

leet