java调用shell_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java调用shell

java调用shell

 2019/3/9 12:33:21  zw7534313  程序员俱乐部  我要评论(0)
  • 摘要:java调用shellpublicclassCallShell{publicstaticfinalStringOS_NAME=System.getProperty("os.name");staticbooleanisWindowsPlat=false;publicstaticvoidmain(String[]args){System.out.println("os:"+OS_NAME);StringshellString="ls";if(isWindowsPlat)
  • 标签:Java
java调用shell

public class CallShell {

public static final String OS_NAME = System.getProperty("os.name");
static boolean isWindowsPlat = false;

public static void main(String[] args) {
System.out.println("os: "+OS_NAME);
String shellString = "ls ";
if(isWindowsPlat){
shellString = "cd c:\\users";
}
callShell(shellString);
}

public static void callShell(final String shellString) {
        Process process = null;
        try {
//            String[] cmdArray = shellString.split(" ");
            process = Runtime.getRuntime().exec(shellString);
            process.waitFor();
            System.out.println("CallShell: "+shellString);
        } catch (Throwable e) {
            System.out.println("CallShell: IOException" +shellString);
            e.printStackTrace();
        } finally {
            if (null != process)
                process.destroy();
        }
    }

static {
        if (OS_NAME != null && OS_NAME.toLowerCase().contains("linux")) {
        isWindowsPlat = false;
        }

        if (OS_NAME != null && OS_NAME.toLowerCase().contains("windows")) {
            isWindowsPlat = true;
        }
    }
}
上一篇: 文件锁 下一篇: 没有下一篇了!
发表评论
用户名: 匿名