Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;
import java.io.BufferedWriter;

import java.io.IOException;
import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

public class Main {
    public static boolean isRooted() {
        Process p;
        try {
            p = new ProcessBuilder("su").start();
            BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
            BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));

            stdin.write("whoami");
            stdin.newLine();
            stdin.write("exit");
            stdin.newLine();
            stdin.close();
            try {
                p.waitFor();
                if (!stdout.ready())
                    return false;
                String user = stdout.readLine(); //We only expect one line of output
                stdout.close();
                if (user == "root") {
                    return true;
                } else {
                    return false;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}