Runtime.getruntime(cmd.exe C () Usage

Source: Internet
Author: User

The latest project was used to test the situation of the linuxlinuxsystem. A lot of data was found on the internet, and runtime.getruntime(cmd.exe C () was the most final method. I have never written a blog before. I learned to write a blog for the first time today.

First, run the correct code:

Khan, how can I paste the code? --

package com.fajun.shell.demo;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.Properties;public class DoShell { private static boolean getShellReturn(String shell_cmd,String shell_result) throws IOException{    Process process = Runtime.getRuntime().exec(shell_cmd);  InputStream inputStream = process.getInputStream();  InputStreamReader inputStreamReader = new InputStreamReader(inputStream);  BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  List<String> strings = new ArrayList<String>();  String str = null;  while ((str = bufferedReader.readLine())!= null) {   strings.add(str);  }  for (int i = 0; i < strings.size(); i++) {   if (strings.get(i).contains(shell_result)) {    return true;   }  }    return false;  } private static  String getOSName(){    Properties properties = System.getProperties();  String os = properties.getProperty("os.name");  return os;  } public static void main(String[] args) {  String os = getOSName();  if (os.contains("inux")) {      String shell_cmd = "mount";   String shell_result="sda1";   boolean flag =false;   try {    flag = getShellReturn(shell_cmd, shell_result);   } catch (IOException e) {    e.printStackTrace();   }   System.out.println("the result is "+ flag);  }  System.out.println("os name is "+os);  }}

The above is a test code I wrote. Please refer to it for correction.

 

When I write this code, a problem occurs, that is, if the getshellresult () method is passed in for an exact search like "Mount | grep sda1, NO content can be found, but if you enter this command on the Linux console, it is a value. Why?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.