`

java调用shell

阅读更多
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;
        }
    }
}
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics