have shell Root permission - Java Native OS

Java examples for Native OS:Linux

Description

have shell Root permission

Demo Code


//package com.java2s;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Main {

    protected static boolean haveRoot() throws IOException,
            InterruptedException {
        int i = execRootCmdSilent("echo test");
        System.out.println("i->" + i);
        if (i == 0)
            return false;
        return true;
    }//from w  w  w .  j av a 2  s .  c  o m

    protected static int execRootCmdSilent(String paramString)
            throws IOException, InterruptedException {
        Process localProcess = Runtime.getRuntime().exec("su");
        DataOutputStream localDataOutputStream = new DataOutputStream(
                (OutputStream) localProcess.getOutputStream());
        localDataOutputStream.writeBytes((String) (String
                .valueOf(paramString) + "\n"));
        localDataOutputStream.flush();
        localDataOutputStream.writeBytes("exit\n");
        localDataOutputStream.flush();
        localProcess.waitFor();
        return localProcess.exitValue();
    }
}

Related Tutorials