Run command line argument through java code
You can run any kind of command line argument (like mkdir, netstat) using following code.
Copy, Paste and run following code.

RunCommand class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RunCommand {
public static void main(String[] args) {
try {
// Execute a command without arguments
String command = "netstat -ano";
Process child = Runtime.getRuntime().exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(child.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
if(line.contains("LISTENING")){
System.out.println(line);
}
}
int exitVal = child.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch (IOException e) {
} catch (Exception e) {
}
}
}
Copy, Paste and run following code.
RunCommand class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RunCommand {
public static void main(String[] args) {
try {
// Execute a command without arguments
String command = "netstat -ano";
Process child = Runtime.getRuntime().exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(child.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
if(line.contains("LISTENING")){
System.out.println(line);
}
}
int exitVal = child.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch (IOException e) {
} catch (Exception e) {
}
}
}
Comments
Post a Comment