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?