Example usage for java.lang Runtime getRuntime

List of usage examples for java.lang Runtime getRuntime

Introduction

In this page you can find the example usage for java.lang Runtime getRuntime.

Prototype

public static Runtime getRuntime() 

Source Link

Document

Returns the runtime object associated with the current Java application.

Usage

From source file:Main.java

public static void chmod(String permission, String path) {
    try {/*from w w  w  .ja  v a2 s . c o  m*/
        String command = "chmod " + permission + " " + path;
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec(command);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void chmod(String permission, String path) {
    try {//from  w ww .  j  a v a 2 s  .c  o m
        String command = "chmod " + permission + " " + path;
        Runtime runtime = Runtime.getRuntime();
        runtime.exec(command);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

static void runCommandWithRoot(String command) {
    try {/*www .j  av a  2 s .  c om*/
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        os.writeBytes(command);
        os.writeBytes("exit\n");
        os.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void chmod(String permission, String path) {
    try {/*w  w  w  .  j  ava  2s .  c  o m*/
        String command = "chmod " + permission + " " + path;
        Runtime runtime = Runtime.getRuntime();
        java.lang.Process process = runtime.exec(command);
        process.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static int getPid(String tag) {
    Process p;//w  w  w. j a v  a 2 s  .c  o m
    try {
        p = Runtime.getRuntime().exec("ps ");
        BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = bufferedReader2.readLine()) != null) {
            if (line.contains(tag)) {
                return Integer.parseInt(line.split("\\s+")[1]);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return -1;
}

From source file:Main.java

public static String execCmd(String cmd) {

    String result = "";
    DataInputStream dis = null;/*w  w w  .  j av a 2 s.  c o m*/

    try {
        Process p = Runtime.getRuntime().exec(cmd);
        dis = new DataInputStream(p.getInputStream());
        String line = null;
        while ((line = dis.readLine()) != null) {
            line += "\n";
            result += line;
        }
        p.waitFor();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return result;
}

From source file:com.vmware.photon.controller.core.Main.java

public static void main(String[] args) throws Throwable {
    try {//from   ww  w. j a  va 2  s  .c  om
        LoggingFactory.bootstrap();

        logger.info("args: " + Arrays.toString(args));

        ArgumentParser parser = ArgumentParsers.newArgumentParser("PhotonControllerCore").defaultHelp(true)
                .description("Photon Controller Core");
        parser.addArgument("config-file").help("photon controller configuration file");
        parser.addArgument("--manual").type(Boolean.class).setDefault(false)
                .help("If true, create default deployment.");

        Namespace namespace = parser.parseArgsOrFail(args);

        PhotonControllerConfig photonControllerConfig = getPhotonControllerConfig(namespace);
        DeployerConfig deployerConfig = photonControllerConfig.getDeployerConfig();

        new LoggingFactory(photonControllerConfig.getLogging(), "photon-controller-core").configure();

        SSLContext sslContext;
        if (deployerConfig.getDeployerContext().isAuthEnabled()) {
            sslContext = SSLContext.getInstance(KeyStoreUtils.THRIFT_PROTOCOL);
            TrustManagerFactory tmf = null;

            tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            KeyStore keyStore = KeyStore.getInstance("JKS");
            InputStream in = FileUtils
                    .openInputStream(new File(deployerConfig.getDeployerContext().getKeyStorePath()));
            keyStore.load(in, deployerConfig.getDeployerContext().getKeyStorePassword().toCharArray());
            tmf.init(keyStore);
            sslContext.init(null, tmf.getTrustManagers(), null);
        } else {
            KeyStoreUtils.generateKeys("/thrift/");
            sslContext = KeyStoreUtils.acceptAllCerts(KeyStoreUtils.THRIFT_PROTOCOL);
        }

        ThriftModule thriftModule = new ThriftModule(sslContext);
        PhotonControllerXenonHost xenonHost = startXenonHost(photonControllerConfig, thriftModule,
                deployerConfig, sslContext);

        if ((Boolean) namespace.get("manual")) {
            DefaultDeployment.createDefaultDeployment(photonControllerConfig.getXenonConfig().getPeerNodes(),
                    deployerConfig, xenonHost);
        }

        // Creating a temp configuration file for apife with modification to some named sections in photon-controller-config
        // so that it can match the Configuration class of dropwizard.
        File apiFeTempConfig = File.createTempFile("apiFeTempConfig", ".tmp");
        File source = new File(args[0]);
        FileInputStream fis = new FileInputStream(source);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));

        FileWriter fstream = new FileWriter(apiFeTempConfig, true);
        BufferedWriter out = new BufferedWriter(fstream);

        String aLine = null;
        while ((aLine = in.readLine()) != null) {
            if (aLine.equals("apife:")) {
                aLine = aLine.replace("apife:", "server:");
            }
            out.write(aLine);
            out.newLine();
        }
        in.close();
        out.close();

        // This approach can be simplified once the apife container is gone, but for the time being
        // it expects the first arg to be the string "server".
        String[] apiFeArgs = new String[2];
        apiFeArgs[0] = "server";
        apiFeArgs[1] = apiFeTempConfig.getAbsolutePath();
        ApiFeService.setupApiFeConfigurationForServerCommand(apiFeArgs);
        ApiFeService.addServiceHost(xenonHost);
        ApiFeService.setSSLContext(sslContext);

        ApiFeService apiFeService = new ApiFeService();
        apiFeService.run(apiFeArgs);
        apiFeTempConfig.deleteOnExit();

        LocalApiClient localApiClient = apiFeService.getInjector().getInstance(LocalApiClient.class);
        xenonHost.setApiClient(localApiClient);

        // in the non-auth enabled scenario we need to be able to accept any self-signed certificate
        if (!deployerConfig.getDeployerContext().isAuthEnabled()) {
            KeyStoreUtils.acceptAllCerts(KeyStoreUtils.THRIFT_PROTOCOL);
        }

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                logger.info("Shutting down");
                xenonHost.stop();
                logger.info("Done");
                LoggingFactory.detachAndStop();
            }
        });
    } catch (Exception e) {
        logger.error("Failed to start photon controller ", e);
        throw e;
    }
}

From source file:Main.java

public static boolean hasRoot() {
    char[] arrayOfChar = new char[1024];
    try {/*  w w  w  .  j  ava2  s. co  m*/
        int j = new InputStreamReader(Runtime.getRuntime().exec("su -c ls").getErrorStream()).read(arrayOfChar);
        if (j == -1) {
            return true;
        }
    } catch (IOException e) {
    }
    return false;
}

From source file:Main.java

public static boolean startApplication(String namespace, String activity) {
    try {//from   w ww  .j  a v a2 s  .c  o m
        String cmd = "am start -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -n %s/%s";
        Runtime.getRuntime().exec(String.format(cmd, namespace, activity));
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

public static void chmod(String mode, String path) {
    try {//from   w  w  w  .j a v a 2s  .c om
        String command = "chmod " + mode + " " + path;
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);
        process.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}