Java execute CMD command

See: https://blog.csdn.net/lixingshi/article/details/50467840

 

public static void runtimeCommand() throws Exception {
    	
    	Process process = Runtime.getRuntime().exec("cmd.exe /c dir");
    	int status = process.waitFor();
    	
    	System.out.println(status);
    	InputStream in = process.getInputStream();
    	
    	BufferedReader br = new BufferedReader(new InputStreamReader(in));
    	String line = br.readLine();
    	while(line!=null) {
    		System.out.println(line);
    		line = br.readLine();
    	}
    	
    }  

Use /c on the command line to close the executed window, otherwise the input stream cannot be obtained. But under Linux, you can directly use the following code to get the input stream:

Process process = Runtime.getRuntime().exec("ls");
    	int status = process.waitFor();
    	
    	System.out.println(status);
    	InputStream in = process.getInputStream();
    	
    	BufferedReader br = new BufferedReader(new InputStreamReader(in));
    	String line = br.readLine();
    	while(line!=null) {
    		System.out.println(line);
    		line = br.readLine();
    	}

  You can also use ProcessBuilder to create a process, which is more flexible. code show as below:

public static void processBuilderCommand() throws Exception {
	 
    	List<String> commands = new ArrayList<>();
    	commands.add("cmd.exe");
    	commands.add("/c");
    	commands.add("dir");
    	commands.add("E:\\flink");
	 	ProcessBuilder pb =new ProcessBuilder(commands);
	 	//You can modify process environment variables
	 	pb.environment().put("DAXIN_HOME", "/home/daxin");
	 	System.out.println(pb.directory());
	 	
    	Process process = pb.start();
    	int status = process.waitFor();
    	System.out.println(pb.environment());
    	
    	System.out.println(status);
    	InputStream in = process.getInputStream();
    	
    	BufferedReader br = new BufferedReader(new InputStreamReader(in));
    	String line = br.readLine();
    	while(line!=null) {
    		System.out.println(line);
    		line = br.readLine();
    	}
    	
    }
    

  

Call the java command to execute the class file and get the output:

public static void processBuilderCommand() throws Exception {
	 
    	List<String> commands = new ArrayList<>();
    	commands.add("cmd.exe");
    	commands.add("/c");
    	commands.add("java HelloWorld");
	 	ProcessBuilder pb =new ProcessBuilder(commands);
	 	pb.directory(new File("C:\\Users\\liuguangxin\\oxygen--workspace\\java8\\bin\\"));//Set the working directory
    	Process process = pb.start();
    	int status = process.waitFor();
    	
    	InputStream in = process.getInputStream();
    	
    	BufferedReader br = new BufferedReader(new InputStreamReader(in));
    	String line = br.readLine();
    	while(line!=null) {
    		System.out.println(line);
    		line = br.readLine();
    	}
    	
    }

  

 

java C:\\Users\\liuguangxin\\oxygen--workspace\\java8\\bin\\HelloWorld will resolve the directory as a class name, so it cannot be executed.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325296705&siteId=291194637