Java Examples for java.lang.ProcessBuilder

The following java examples will help you to understand the usage of java.lang.ProcessBuilder. These source code samples are taken from different open source projects.

Example 1
Project: XPrivacy-master  File: XProcessBuilder.java View source code
@Override
protected void before(XParam param) throws Throwable {
    String methodName = param.method.getName();
    if (methodName.equals("start")) {
        // Get commands
        ProcessBuilder builder = (ProcessBuilder) param.thisObject;
        List<String> listProg = (builder == null ? null : builder.command());
        // Check commands
        if (listProg != null) {
            String command = TextUtils.join(" ", listProg);
            if (XRuntime.matches(command, mCommand) && isRestrictedExtra(param, command))
                param.setThrowable(new IOException("XPrivacy"));
        }
    } else
        Util.log(this, Log.WARN, "Unknown method=" + methodName);
}
Example 2
Project: wildfly-core-master  File: Launcher.java View source code
/**
     * Launches a new process based on the commands from the {@link org.wildfly.core.launcher.CommandBuilder builder}.
     *
     * @return the newly created process
     *
     * @throws IOException if an error occurs launching the process
     */
public Process launch() throws IOException {
    final ProcessBuilder processBuilder = new ProcessBuilder(builder.build());
    if (outputDestination != null) {
        processBuilder.redirectOutput(outputDestination);
    }
    if (errorDestination != null) {
        processBuilder.redirectError(errorDestination);
    }
    if (workingDirectory != null) {
        processBuilder.directory(workingDirectory);
    }
    if (!env.isEmpty()) {
        processBuilder.environment().putAll(env);
    }
    processBuilder.redirectErrorStream(redirectErrorStream);
    return processBuilder.start();
}
Example 3
Project: ZjDroid-master  File: ProcessBuilderHook.java View source code
@Override
public void descParam(HookParam param) {
    // TODO Auto-generated method stub
    Logger.log_behavior("Create New Process ->");
    ProcessBuilder pb = (ProcessBuilder) param.thisObject;
    List<String> cmds = pb.command();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < cmds.size(); i++) {
        sb.append("CMD" + i + ":" + cmds.get(i) + " ");
    }
    Logger.log_behavior("Command" + sb.toString());
}
Example 4
Project: intellij-community-master  File: UseOfProcessBuilderInspection.java View source code
@Override
public void visitVariable(@NotNull PsiVariable variable) {
    super.visitVariable(variable);
    final PsiType type = variable.getType();
    final String typeString = type.getCanonicalText();
    if (!"java.lang.ProcessBuilder".equals(typeString)) {
        return;
    }
    final PsiTypeElement typeElement = variable.getTypeElement();
    if (typeElement == null) {
        return;
    }
    registerError(typeElement);
}
Example 5
Project: karaf-master  File: InstanceServiceImpl.java View source code
private int chmod(File serviceFile, String mode) throws IOException {
    java.lang.ProcessBuilder builder = new java.lang.ProcessBuilder();
    builder.command("chmod", mode, serviceFile.getCanonicalPath());
    java.lang.Process p = builder.start();
    //handler.start();
    try {
        return p.waitFor();
    } catch (InterruptedException e) {
        throw (IOException) new InterruptedIOException().initCause(e);
    }
//handler.stop();
}
Example 6
Project: bazel-master  File: Worker.java View source code
void createProcess() throws IOException {
    String[] command = workerKey.getArgs().toArray(new String[0]);
    // Follows the logic of {@link com.google.devtools.build.lib.shell.Command}.
    File executable = new File(command[0]);
    if (!executable.isAbsolute() && executable.getParent() != null) {
        command[0] = new File(workDir.getPathFile(), command[0]).getAbsolutePath();
    }
    ProcessBuilder processBuilder = new ProcessBuilder(command).directory(workDir.getPathFile()).redirectError(Redirect.appendTo(logFile.getPathFile()));
    processBuilder.environment().clear();
    processBuilder.environment().putAll(workerKey.getEnv());
    this.process = processBuilder.start();
}
Example 7
Project: JDK-master  File: ProcessImpl.java View source code
// Only for use by ProcessBuilder.start()
static Process start(String[] cmdarray, java.util.Map<String, String> environment, String dir, ProcessBuilder.Redirect[] redirects, boolean redirectErrorStream) throws IOException {
    assert cmdarray != null && cmdarray.length > 0;
    // Convert arguments to a contiguous block; it's easier to do
    // memory management in Java than in C.
    byte[][] args = new byte[cmdarray.length - 1][];
    // For added NUL bytes
    int size = args.length;
    for (int i = 0; i < args.length; i++) {
        args[i] = cmdarray[i + 1].getBytes();
        size += args[i].length;
    }
    byte[] argBlock = new byte[size];
    int i = 0;
    for (byte[] arg : args) {
        System.arraycopy(arg, 0, argBlock, i, arg.length);
        i += arg.length + 1;
    // No need to write NUL bytes explicitly
    }
    int[] envc = new int[1];
    byte[] envBlock = ProcessEnvironment.toEnvironmentBlock(environment, envc);
    int[] std_fds;
    FileInputStream f0 = null;
    FileOutputStream f1 = null;
    FileOutputStream f2 = null;
    try {
        if (redirects == null) {
            std_fds = new int[] { -1, -1, -1 };
        } else {
            std_fds = new int[3];
            if (redirects[0] == Redirect.PIPE)
                std_fds[0] = -1;
            else if (redirects[0] == Redirect.INHERIT)
                std_fds[0] = 0;
            else {
                f0 = new FileInputStream(redirects[0].file());
                std_fds[0] = fdAccess.get(f0.getFD());
            }
            if (redirects[1] == Redirect.PIPE)
                std_fds[1] = -1;
            else if (redirects[1] == Redirect.INHERIT)
                std_fds[1] = 1;
            else {
                f1 = new FileOutputStream(redirects[1].file(), redirects[1].append());
                std_fds[1] = fdAccess.get(f1.getFD());
            }
            if (redirects[2] == Redirect.PIPE)
                std_fds[2] = -1;
            else if (redirects[2] == Redirect.INHERIT)
                std_fds[2] = 2;
            else {
                f2 = new FileOutputStream(redirects[2].file(), redirects[2].append());
                std_fds[2] = fdAccess.get(f2.getFD());
            }
        }
        return new UNIXProcess(toCString(cmdarray[0]), argBlock, args.length, envBlock, envc[0], toCString(dir), std_fds, redirectErrorStream);
    } finally {
        // (although it is rather unlikely to happen here)
        try {
            if (f0 != null)
                f0.close();
        } finally {
            try {
                if (f1 != null)
                    f1.close();
            } finally {
                if (f2 != null)
                    f2.close();
            }
        }
    }
}
Example 8
Project: jdk7u-jdk-master  File: BigFork.java View source code
public static void main(String[] args) throws Throwable {
    showCommittedMemory();
    final int chunkSize = 1024 * 1024 * 100;
    List<byte[]> chunks = new ArrayList<byte[]>(100);
    try {
        for (; ; ) {
            byte[] chunk = new byte[chunkSize];
            touchPages(chunk);
            chunks.add(chunk);
        }
    } catch (OutOfMemoryError e) {
        chunks.set(0, null);
        System.gc();
        int size = chunks.size();
        System.out.printf("size=%.2gGB%n", (double) size / 10);
        showCommittedMemory();
        Process p = new ProcessBuilder("/bin/true").start();
        p.waitFor();
    }
}
Example 9
Project: ofbiz-master  File: UtilXmlTests.java View source code
public void testUnsupportedClassConverter() throws Exception {
    String unsupportedClassInXml = "<handler class=\"java.beans.EventHandler\">" + " <target class=\"java.lang.ProcessBuilder\">" + " <command>" + " <string>open</string>" + " <string>.</string>" + " </command>" + " </target>" + " <action>start</action>" + "</handler>";
    try {
        @SuppressWarnings("unused") Object unsupportedObject = UtilXml.fromXml(unsupportedClassInXml);
        fail("Unsupported class in XML");
    } catch (Exception e) {
    }
}
Example 10
Project: gradle-master  File: ProcessBuilderFactory.java View source code
public ProcessBuilder createProcessBuilder(ProcessSettings processSettings) {
    List<String> commandWithArguments = new ArrayList<String>();
    commandWithArguments.add(processSettings.getCommand());
    commandWithArguments.addAll(processSettings.getArguments());
    ProcessBuilder processBuilder = new ProcessBuilder(commandWithArguments);
    processBuilder.directory(processSettings.getDirectory());
    processBuilder.redirectErrorStream(processSettings.getRedirectErrorStream());
    Map<String, String> environment = processBuilder.environment();
    environment.clear();
    environment.putAll(processSettings.getEnvironment());
    return processBuilder;
}
Example 11
Project: ysoserial-master  File: BeanShell1.java View source code
public PriorityQueue getObject(String command) throws Exception {
    // BeanShell payload
    String payload = "compare(Object foo, Object bar) {new java.lang.ProcessBuilder(new String[]{\"" + command + "\"}).start();return new Integer(1);}";
    // Create Interpreter
    Interpreter i = new Interpreter();
    // Evaluate payload
    i.eval(payload);
    // Create InvocationHandler
    XThis xt = new XThis(i.getNameSpace(), i);
    InvocationHandler handler = (InvocationHandler) Reflections.getField(xt.getClass(), "invocationHandler").get(xt);
    // Create Comparator Proxy
    Comparator comparator = (Comparator) Proxy.newProxyInstance(Comparator.class.getClassLoader(), new Class<?>[] { Comparator.class }, handler);
    // Prepare Trigger Gadget (will call Comparator.compare() during deserialization)
    final PriorityQueue<Object> priorityQueue = new PriorityQueue<Object>(2, comparator);
    Object[] queue = new Object[] { 1, 1 };
    Reflections.setFieldValue(priorityQueue, "queue", queue);
    Reflections.setFieldValue(priorityQueue, "size", 2);
    return priorityQueue;
}
Example 12
Project: AURA-master  File: ProcessExecutor.java View source code
// ---------------------------------------------------
// Public.
// ---------------------------------------------------
public ProcessExecutor execute(String[] jvmOpts, String... params) {
    final String javaRuntime = System.getProperty("java.home") + "/bin/java";
    final String classpath = System.getProperty("java.class.path") + ":" + executeableClazz.getProtectionDomain().getCodeSource().getLocation().getPath();
    final String canonicalName = executeableClazz.getCanonicalName();
    try {
        final List<String> commandList = new ArrayList<String>();
        commandList.add(javaRuntime);
        commandList.add("-cp");
        commandList.add(classpath);
        commandList.addAll(Arrays.asList(jvmOpts));
        commandList.add(canonicalName);
        commandList.addAll(Arrays.asList(params));
        final ProcessBuilder builder = new ProcessBuilder(commandList);
        builder.redirectErrorStream(true);
        builder.redirectOutput(Redirect.INHERIT);
        process = builder.start();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    return this;
}
Example 13
Project: streamsx.topology-master  File: PublishSubscribePython.java View source code
Path genPythonToolkit(String module) throws IOException, InterruptedException {
    Path pubsub = Paths.get(getTestRoot().getAbsolutePath(), "python", "pubsub");
    Path pyTk = Files.createTempDirectory("pytk").toAbsolutePath();
    Path pyPackages = Paths.get(System.getProperty("topology.toolkit.release"), "opt", "python", "packages").toAbsolutePath();
    String pythonversion = System.getProperty("topology.test.python");
    ProcessBuilder pb = new ProcessBuilder(pythonversion, module, pyTk.toAbsolutePath().toString());
    pb.redirectOutput(Redirect.INHERIT);
    pb.redirectError(Redirect.INHERIT);
    Map<String, String> env = pb.environment();
    env.put("PYTHONPATH", pyPackages.toString());
    pb.directory(pubsub.toFile());
    Process proc = pb.start();
    assertEquals(0, proc.waitFor());
    return pyTk;
}
Example 14
Project: sonar-java-master  File: OSCommandInjectionCheck.java View source code
@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
            MethodInvocationTree mit = (MethodInvocationTree) tree;
            Arguments arguments = mit.arguments();
            if (RUNTIME_EXEC_MATCHER.matches(mit)) {
                checkForIssue(tree, arguments.get(0));
            } else if (PROCESS_BUILDER_COMMAND_MATCHER.matches(mit) && !arguments.isEmpty()) {
                checkForIssue(tree, arguments);
            }
        } else if (((NewClassTree) tree).symbolType().is("java.lang.ProcessBuilder")) {
            checkForIssue(tree, ((NewClassTree) tree).arguments());
        }
    }
}
Example 15
Project: activemq-artemis-master  File: ProcessBuilder.java View source code
/**
    * *
    *
    * @param logname  the prefix for log output
    * @param location The location where this command is being executed from
    * @param hook     it will finish the process upon shutdown of the VM
    * @param args     The arguments being passwed to the the CLI tool
    * @return
    * @throws Exception
    */
public static Process build(String logname, File location, boolean hook, String... args) throws Exception {
    boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().trim().startsWith("win");
    String[] newArgs;
    if (IS_WINDOWS) {
        newArgs = rebuildArgs(args, "cmd", "/c", "artemis.cmd");
    } else {
        newArgs = rebuildArgs(args, "./artemis");
    }
    java.lang.ProcessBuilder builder = new java.lang.ProcessBuilder(newArgs);
    builder.directory(new File(location, "bin"));
    Process process = builder.start();
    ProcessLogger outputLogger = new ProcessLogger(true, process.getInputStream(), logname, false);
    outputLogger.start();
    // Adding a reader to System.err, so the VM won't hang on a System.err.println as identified on this forum thread:
    ProcessLogger errorLogger = new ProcessLogger(true, process.getErrorStream(), logname, true);
    errorLogger.start();
    processes.add(process);
    cleanupProcess();
    return process;
}
Example 16
Project: Turnierserver-master  File: AiExecutor.java View source code
protected void executeAi() throws IOException {
    List<String> cmd = new LinkedList<>();
    cmd.add(getString("isolate.bin", "isolate"));
    String command;
    if (commands.get(getJob().getLang()).isEmpty())
        command = start.getProperty("command");
    else
        command = commands.get(getJob().getLang());
    if (command.startsWith(".")) {
        SandboxMain.getLogger().debug("Flagging " + new File(binDir, command.substring(2)).getAbsolutePath() + " as executable");
        new File(binDir, command.substring(2)).setExecutable(true);
        command = "/box/bin" + command.substring(1);
    }
    cmd.add("--cg");
    cmd.add("-p");
    cmd.add("--share-net");
    cmd.add("-q");
    cmd.add("0,0");
    cmd.add("--time=" + job.getTimeout());
    cmd.add("--wall-time=" + job.getTimeout());
    cmd.add("--dir=/etc/=" + etc.getAbsolutePath());
    cmd.add("--dir=/usr/lib/jvm/");
    cmd.add("-c");
    cmd.add("/box/bin/");
    cmd.add("--env=LANG");
    for (int i = 0; i < Integer.parseInt(start.getProperty("environment.size")); i++) {
        cmd.add("--env=" + start.getProperty("environment." + i + ".key") + "=" + start.getProperty("environment." + i + ".value"));
    }
    cmd.add("--run");
    cmd.add("-b");
    cmd.add(Integer.toString(job.getBoxid()));
    cmd.add("--");
    cmd.add(command);
    for (int i = 0; i < Integer.parseInt(start.getProperty("arguments.size")); i++) {
        cmd.add(start.getProperty("arguments." + i));
    }
    cmd.add("/box/" + aiProp.getName());
    SandboxMain.getLogger().debug("Der Befehl ist " + cmd);
    ProcessBuilder pb = new ProcessBuilder(cmd);
    pb.redirectErrorStream(true);
    pb.redirectOutput(Redirect.INHERIT);
    proc = pb.start();
    SandboxMain.getClient().sendMessage(getJob().getUuid(), 'S');
    new Thread(() -> {
        int ret;
        try {
            ret = proc.waitFor();
            SandboxMain.getLogger().debug("Die KI hat sich mit dem Statuscode " + ret + " beendet");
            SandboxMain.getClient().sendMessage(getJob().getUuid(), 'F');
            ProcessBuilder pb0 = new ProcessBuilder(getString("isolate.bin", "isolate"), "--cleanup", "-b", Integer.toString(job.getBoxid()));
            SandboxMain.getLogger().debug(pb0.command());
            pb0.redirectErrorStream(true);
            pb0.redirectOutput(Redirect.INHERIT);
            if (pb0.start().waitFor() != 0)
                SandboxMain.getLogger().critical("Fehler beim Aufräumen von isolate");
            jobControl.jobFinished(getJob().getUuid());
        } catch (Exception e) {
            Airbrake.log(e).printStackTrace();
        }
    }, "IsolateCleanup").start();
}
Example 17
Project: azkaban-master  File: AzkabanWebServer.java View source code
public void logTopMemoryConsumers() throws Exception, IOException {
    if (new File("/bin/bash").exists() && new File("/bin/ps").exists() && new File("/usr/bin/head").exists()) {
        logger.info("logging top memeory consumer");
        java.lang.ProcessBuilder processBuilder = new java.lang.ProcessBuilder("/bin/bash", "-c", "/bin/ps aux --sort -rss | /usr/bin/head");
        Process p = processBuilder.start();
        p.waitFor();
        InputStream is = p.getInputStream();
        java.io.BufferedReader reader = new java.io.BufferedReader(new InputStreamReader(is));
        String line = null;
        while ((line = reader.readLine()) != null) {
            logger.info(line);
        }
        is.close();
    }
}
Example 18
Project: hk-master  File: RuntimeHooker.java View source code
/**
	 * Attach on ProcessBuilder class
	 */
private void attachOnProcessBuilderClass() {
    final String className = "java.lang.ProcessBuilder";
    Map<String, Integer> methodsToHook = new HashMap<String, Integer>();
    methodsToHook.put("command", 2);
    methodsToHook.put("directory", 1);
    methodsToHook.put("environment", 1);
    methodsToHook.put("redirectErrorStream", 1);
    methodsToHook.put("start", 2);
    try {
        hookMethods(null, className, methodsToHook);
        SubstrateMain.log(new StringBuilder("hooking ").append(className).append(" methods sucessful").toString());
    } catch (HookerInitializationException e) {
        SubstrateMain.log(new StringBuilder("hooking ").append(className).append(" methods has failed").toString(), e);
    }
}
Example 19
Project: openjdk-master  File: CommandExecutor.java View source code
/**
         * apply - apply the redirects to the current ProcessBuilder.
         * @param pb current ProcessBuilder
         */
void apply(final ProcessBuilder pb) {
    // Only if there was redirects (saves new structure in ProcessBuilder.)
    if (hasRedirects) {
        // If output and error are the same file then merge.
        final File outputFile = outputRedirect.file();
        final File errorFile = errorRedirect.file();
        if (outputFile != null && outputFile.equals(errorFile)) {
            mergeError = true;
        }
        // Apply redirects.
        pb.redirectInput(inputRedirect);
        pb.redirectOutput(outputRedirect);
        pb.redirectError(errorRedirect);
        pb.redirectErrorStream(mergeError);
    }
}
Example 20
Project: cdt-master  File: FindStdLibPath.java View source code
/**
	 * Find stdc++ library path.
	 * 
	 * @return Stdc++ library path.
	 */
public static String find() {
    ProcessBuilder pb = null;
    //$NON-NLS-1$
    String os = System.getProperty("os.name").toLowerCase();
    if (//$NON-NLS-1$
    os.indexOf("win") >= 0) {
        //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
        pb = new ProcessBuilder("cmd", "/c", WIN_SCRIPT + " " + STD_LIB);
    } else if (//$NON-NLS-1$ //$NON-NLS-2$
    os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0) {
        //$NON-NLS-1$//$NON-NLS-2$
        pb = new ProcessBuilder("bash", "-c", UNIX_SCRIPT);
    } else if (//$NON-NLS-1$
    os.indexOf("mac") >= 0) {
        //$NON-NLS-1$//$NON-NLS-2$
        pb = new ProcessBuilder("bash", "-c", MAC_SCRIPT);
    } else {
        return null;
    }
    try {
        Process p = pb.start();
        String line;
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        line = input.readLine();
        input.close();
        if (line != null) {
            return line;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Example 21
Project: Singularity-master  File: SimpleProcessManager.java View source code
public List<String> runCommand(final List<String> command, final Redirect redirectOutput, final Set<Integer> acceptableExitCodes) throws InterruptedException, ProcessFailedException {
    final ProcessBuilder processBuilder = new ProcessBuilder(command);
    Optional<Integer> exitCode = Optional.absent();
    Optional<OutputReader> reader = Optional.absent();
    String processToString = getCurrentProcessToString();
    try {
        processBuilder.redirectError(Redirect.INHERIT);
        processBuilder.redirectOutput(redirectOutput);
        final Process process = startProcess(processBuilder);
        processToString = getCurrentProcessToString();
        if (redirectOutput == Redirect.PIPE) {
            reader = Optional.of(new OutputReader(process.getInputStream()));
            reader.get().start();
        }
        exitCode = Optional.of(process.waitFor());
        if (reader.isPresent()) {
            reader.get().join();
            if (reader.get().error.isPresent()) {
                throw reader.get().error.get();
            }
        }
    } catch (InterruptedException ie) {
        signalKillToProcessIfActive();
        throw ie;
    } catch (Throwable t) {
        getLog().error("Unexpected exception while running {}", processToString, t);
        signalKillToProcessIfActive();
        throw Throwables.propagate(t);
    } finally {
        processFinished(exitCode);
    }
    if (exitCode.isPresent() && !acceptableExitCodes.contains(exitCode.get())) {
        throw new ProcessFailedException(String.format("Got unacceptable exit code %s while running %s", exitCode, processToString));
    }
    if (!reader.isPresent()) {
        return Collections.emptyList();
    }
    return reader.get().output;
}
Example 22
Project: btrbck-master  File: BtrfsService.java View source code
public void createSubVolume(Path subVolumeDir) {
    String path = subVolumeDir.toAbsolutePath().toString();
    try {
        {
            int exitValue = processBuilder("btrfs", "subvolume", "create", path).start().waitFor();
            if (exitValue != 0) {
                throw new IOException("exit code: " + exitValue);
            }
        }
        // determine the current user
        String userName;
        {
            Process process = new ProcessBuilder("whoami").redirectError(Redirect.INHERIT).start();
            int exitValue = process.waitFor();
            if (exitValue != 0) {
                throw new RuntimeException("whoami exited with " + exitValue);
            }
            userName = Util.readFully(process.getInputStream());
            if (userName.endsWith("\n")) {
                userName = userName.substring(0, userName.length() - "\n".length());
            }
        }
        // change the owner of the subvolume
        {
            int exitValue = processBuilder("chown", userName + ":", path).start().waitFor();
            if (exitValue != 0) {
                throw new RuntimeException("chown exited with " + exitValue);
            }
        }
    } catch (IOExceptionInterruptedException |  e) {
        throw new RuntimeException("Error while creating sub volume in " + path, e);
    }
}
Example 23
Project: gravel-master  File: VMTargetStarter.java View source code
public Process startSecondJVM(Class<?> mainClassToStart) throws IOException {
    String separator = System.getProperty("file.separator");
    String classpath = System.getProperty("java.class.path");
    String path = System.getProperty("java.home") + separator + "bin" + separator + "java";
    ProcessBuilder processBuilder = new ProcessBuilder(path, "-Xdebug", "-Xrunjdwp:transport=dt_socket,address=" + debugPort + ",server=y,suspend=y", "-cp", classpath, mainClassToStart.getCanonicalName());
    processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    final Process process = processBuilder.start();
    Thread closeChildThread = new Thread() {

        public void run() {
            process.destroy();
        }
    };
    Runtime.getRuntime().addShutdownHook(closeChildThread);
    return process;
}
Example 24
Project: bixie-master  File: AbstractIcTest.java View source code
protected File compileJavaFile(File sourceFile) throws IOException {
    final File tempDir = getTempDir();
    final String javac_command = String.format("javac -g %s -d %s", sourceFile.getAbsolutePath(), tempDir.getAbsolutePath());
    ProcessBuilder pb = new ProcessBuilder(javac_command.split(" "));
    pb.redirectOutput(Redirect.INHERIT);
    pb.redirectError(Redirect.INHERIT);
    Process p = pb.start();
    try {
        p.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    }
    return tempDir;
}
Example 25
Project: spring-boot-master  File: AbstractApplicationLauncher.java View source code
private Process startApplication() throws Exception {
    File workingDirectory = getWorkingDirectory();
    File serverPortFile = workingDirectory == null ? new File("target/server.port") : new File(workingDirectory, "target/server.port");
    serverPortFile.delete();
    File archive = this.applicationBuilder.buildApplication();
    List<String> arguments = new ArrayList<>();
    arguments.add(System.getProperty("java.home") + "/bin/java");
    arguments.addAll(getArguments(archive));
    ProcessBuilder processBuilder = new ProcessBuilder(arguments.toArray(new String[arguments.size()]));
    processBuilder.redirectOutput(Redirect.INHERIT);
    processBuilder.redirectError(Redirect.INHERIT);
    if (workingDirectory != null) {
        processBuilder.directory(workingDirectory);
    }
    Process process = processBuilder.start();
    this.httpPort = awaitServerPort(process, serverPortFile);
    return process;
}
Example 26
Project: megam_chef-master  File: SingleShell.java View source code
/**
	 * Processed the command using ProcessBuilder class Print the output's are
	 * wrote in the some file
	 */
public void compute() {
    try {
        boolean stop_flag = false;
        Command prevCom = null;
        for (Iterator<Command> iter = cmd.getOrderedCommands().iterator(); iter.hasNext() && !stop_flag; ) {
            Command com = iter.next();
            List<String> cmdList = new ArrayList<String>();
            cmdList = com.getCommandList();
            logger.debug("#-------------------------------------------------------#");
            logger.debug(cmdList.toString());
            logger.debug("#-------------------------------------------------------#");
            if (prevCom != null && prevCom.composable()) {
                // feed the previous pipe here.
                prevCom.pipeto(null);
                cmdList = com.pipeto(prevCom.appliedPlaceHolder());
            }
            if (cmdList != null) {
                shellProc = new ProcessBuilder(cmdList);
                shellProc.redirectOutput(Redirect.appendTo(com.getRedirectOutputFile()));
                shellProc.redirectError(Redirect.appendTo(com.getRedirectErrorFile()));
                Process p = shellProc.start();
                if (com.composable()) {
                    int subrc = p.waitFor();
                    if (subrc != 0) {
                        stop_flag = true;
                    }
                    prevCom = com;
                }
            } else
                stop_flag = true;
        }
    } catch (IOExceptionInterruptedException | ShellException |  npe) {
        try {
            throw new ShellException(npe);
        } catch (ShellException e) {
            e.printStackTrace();
        }
    }
}
Example 27
Project: community-plugins-master  File: Engine.java View source code
public static XProcess startProcess(final String[] command, final ProcessListener listener, final File folder) {
    final ProcessBuilder pb;
    final Process process;
    try {
        pb = new ProcessBuilder(command);
        pb.directory(folder);
        pb.redirectOutput(Redirect.PIPE);
        pb.redirectInput(Redirect.PIPE);
        process = pb.start();
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
    final AtomicLong lastOutput = new AtomicLong();
    final long startTime = new Date().getTime();
    lastOutput.set(0);
    final OutputStream outputStream = process.getOutputStream();
    final InputStream inputStream = process.getInputStream();
    final InputStream errorStream = process.getErrorStream();
    //BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    //		String line;
    //		try {
    //			line = reader.readLine();
    //			while (line != null && !line.trim().equals("--EOF--")) {
    //	             listener.onOutputLine(line);
    //	             line = reader.readLine();
    //	         }
    //		} catch (IOException e1) {
    //			// TODO Auto-generated catch block
    //			e1.printStackTrace();
    //		}
    final StreamReader streamReader = new StreamReader(inputStream, new StreamListener() {

        @Override
        public void onOutputLine(final String line) {
            //System.out.println("Receiving line");
            lastOutput.set(new Date().getTime());
            listener.onOutputLine(line);
        }

        @Override
        public void onError(final Throwable t) {
            listener.onError(new Exception("Error while reading standard output", t));
        }

        @Override
        public void onClosed() {
        }
    });
    final StreamReader errorStreamReader = new StreamReader(errorStream, new StreamListener() {

        @Override
        public void onOutputLine(final String line) {
            lastOutput.set(new Date().getTime());
            listener.onErrorLine(line);
        }

        @Override
        public void onError(final Throwable t) {
            listener.onError(new Exception("Error while reading error output", t));
        }

        @Override
        public void onClosed() {
        }
    });
    new Thread() {

        @Override
        public void run() {
            errorStreamReader.read();
        }
    }.start();
    new Thread() {

        @Override
        public void run() {
            try {
                streamReader.read();
                final int returnValue = process.waitFor();
                //while (lastOutput.get() == 0 && new Date().getTime() - startTime < 500) {
                //	Thread.sleep(10);
                //}
                //while ( new Date().getTime() - lastOutput.get() < 1000) {
                //	System.out.println("DELAY ... "+(new Date().getTime() - lastOutput.get()));
                //	Thread.sleep(500);
                //}
                //System.out.println("processes finished with "+returnValue);
                listener.onProcessQuit(returnValue);
            } catch (final InterruptedException e) {
                listener.onError(e);
                return;
            }
        }
    }.start();
    return new XProcess() {

        @Override
        public synchronized void sendLine(final String line) {
            final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
            try {
                writer.append(line);
            } catch (final IOException e) {
                listener.onError(e);
            }
        }

        @Override
        public void destory() {
            process.destroy();
            try {
                process.waitFor();
            } catch (final InterruptedException e) {
                throw new RuntimeException(e);
            }
            try {
                errorStreamReader.stop();
                outputStream.close();
                inputStream.close();
                errorStream.close();
            } catch (final IOException e) {
                listener.onError(e);
            }
        }
    };
}
Example 28
Project: infovore-master  File: LocalCmdCluster.java View source code
//
// For better or worse,  this version has a property that other clusters may not have,
// in that it will print the stderr and stdout of the hadoop process to standard out
//
@Override
public void runJob(MavenManagedJar jar, List<String> jarArgs) throws Exception {
    String hadoopBin = findBin("hadoop");
    if (hadoopBin == null) {
        throw new IOException("Hadoop Executable not found");
    }
    String jarName = jar.pathFromLocalMavenRepository(getMavenRepoPath());
    List<String> args = Lists.newArrayList(hadoopBin, "jar", jarName);
    args.addAll(jarArgs);
    ProcessBuilder pb = new ProcessBuilder(args);
    pb.redirectErrorStream(true);
    pb.redirectOutput(Redirect.INHERIT);
    Process p = pb.start();
    int value = p.waitFor();
    if (value != 0) {
        throw ExitCodeException.create(value);
    }
    ;
}
Example 29
Project: filebot-master  File: AcoustIDClient.java View source code
public Map<ChromaprintField, String> fpcalc(File file) throws IOException, InterruptedException {
    Map<ChromaprintField, String> output = new EnumMap<ChromaprintField, String>(ChromaprintField.class);
    ProcessBuilder command = new ProcessBuilder(getChromaprintCommand(), file.getCanonicalPath());
    Process process = command.redirectError(Redirect.INHERIT).start();
    try (Scanner scanner = new Scanner(new InputStreamReader(process.getInputStream(), UTF_8))) {
        while (scanner.hasNextLine()) {
            String[] value = EQUALS.split(scanner.nextLine(), 2);
            if (value.length == 2) {
                try {
                    output.put(ChromaprintField.valueOf(value[0]), value[1]);
                } catch (Exception e) {
                    debug.warning(e::toString);
                }
            }
        }
    }
    return output;
}
Example 30
Project: aorra-master  File: HtmlToPdf.java View source code
private File pdf(File in, String copts) {
    try {
        File destfolder = Files.createTempDir();
        File out = new File(destfolder, FilenameUtils.removeExtension(in.getName()) + ".pdf");
        ProcessBuilder pb = converter(copts, in, out);
        pb.directory(in.getParentFile());
        File log = new File(destfolder, "converter.log");
        pb.redirectErrorStream(true);
        pb.redirectOutput(Redirect.to(log));
        Process p = pb.start();
        // TODO do not wait forever
        int es = p.waitFor();
        if (out.exists()) {
            return out;
        } else {
            String msg = "pdf creation failed with exit status " + es;
            if (log.exists()) {
                throw new RuntimeException(msg + "\n" + FileUtils.readFileToString(log) + "\nfrom " + log.getAbsolutePath());
            } else {
                throw new RuntimeException(msg + " " + destfolder.getAbsolutePath());
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(String.format("failed to create pdf from %s", in.getName()), e);
    }
}
Example 31
Project: jayhorn-master  File: Util.java View source code
/**
	 * Compiles a sourceFile into a temp folder and returns this folder or null
	 * if compilation fails.
	 * 
	 * @param sourceFile the source file to compile
	 * @return the folder that contains the class file(s) or null if compilation
	 *         fails.
	 * @throws IOException
	 */
public static File compileJavaFile(File sourceFile, String classPath) throws IOException {
    final File tempDir = Files.createTempDir();
    final String javac_command = String.format("javac -g -classpath %s %s -d %s", classPath, sourceFile.getAbsolutePath(), tempDir.getAbsolutePath());
    ProcessBuilder pb = new ProcessBuilder(javac_command.split(" "));
    pb.redirectOutput(Redirect.INHERIT);
    pb.redirectError(Redirect.INHERIT);
    Process p = pb.start();
    try {
        p.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    }
    return tempDir;
}
Example 32
Project: FindBug-for-Domino-Designer-master  File: CheckReturnAnnotationDatabase.java View source code
@Override
public void loadAuxiliaryAnnotations() {
    if (IGNORE_BUILTIN_ANNOTATIONS)
        return;
    boolean missingClassWarningsSuppressed = AnalysisContext.currentAnalysisContext().setMissingClassWarningsSuppressed(true);
    addMethodAnnotation("java.util.Iterator", "hasNext", "()Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_LOW);
    addMethodAnnotation("java.io.File", "createNewFile", "()Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.io.File", "delete", "()Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.io.File", "mkdir", "()Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.io.File", "mkdirs", "()Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.io.File", "renameTo", "(Ljava/io/File;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.io.File", "setLastModified", "(J)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.io.File", "setReadOnly", "()Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.io.File", "setWritable", "(ZZ)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.util.Enumeration", "hasMoreElements", "()Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addMethodAnnotation("java.security.MessageDigest", "digest", "([B)[B", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addMethodAnnotation("java.util.concurrent.locks.ReadWriteLock", "readLock", "()Ljava/util/concurrent/locks/Lock;", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_HIGH);
    addMethodAnnotation("java.util.concurrent.locks.ReadWriteLock", "writeLock", "()Ljava/util/concurrent/locks/Lock;", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_HIGH);
    addMethodAnnotation("java.util.concurrent.locks.Condition", "await", "(JLjava/util/concurrent/TimeUnit;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.util.concurrent.CountDownLatch", "await", "(JLjava/util/concurrent/TimeUnit;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addMethodAnnotation("java.util.concurrent.locks.Condition", "awaitUntil", "(Ljava/util/Date;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.util.concurrent.locks.Condition", "awaitNanos", "(J)J", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.util.concurrent.Semaphore", "tryAcquire", "(JLjava/util/concurrent/TimeUnit;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_HIGH);
    addMethodAnnotation("java.util.concurrent.Semaphore", "tryAcquire", "()Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_HIGH);
    addMethodAnnotation("java.util.concurrent.locks.Lock", "tryLock", "(JLjava/util/concurrent/TimeUnit;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_HIGH);
    addMethodAnnotation("java.util.concurrent.locks.Lock", "newCondition", "()Ljava/util/concurrent/locks/Condition;", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_HIGH);
    addMethodAnnotation("java.util.concurrent.locks.Lock", "tryLock", "()Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_HIGH);
    addMethodAnnotation("java.util.concurrent.BlockingQueue", "offer", "(Ljava/lang/Object;JLjava/util/concurrent/TimeUnit;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.util.concurrent.BlockingQueue", "offer", "(Ljava/lang/Object;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.util.concurrent.ConcurrentLinkedQueue", "offer", "(Ljava/lang/Object;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.util.concurrent.DelayQueue", "offer", "(Ljava/lang/Object;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.util.concurrent.LinkedBlockingQueue", "offer", "(Ljava/lang/Object;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_LOW_BAD_PRACTICE);
    addMethodAnnotation("java.util.LinkedList", "offer", "(Ljava/lang/Object;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.util.Queue", "offer", "(Ljava/lang/Object;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_LOW_BAD_PRACTICE);
    addMethodAnnotation("java.util.concurrent.ArrayBlockingQueue", "offer", "(Ljava/lang/Object;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.util.concurrent.SynchronousQueue", "offer", "(Ljava/lang/Object;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM_BAD_PRACTICE);
    addMethodAnnotation("java.util.PriorityQueue", "offer", "(Ljava/lang/Object;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.util.concurrent.PriorityBlockingQueue", "offer", "(Ljava/lang/Object;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addWarningAboutSubmit(ExecutorService.class);
    addWarningAboutSubmit(ThreadPoolExecutor.class);
    addWarningAboutSubmit(ScheduledThreadPoolExecutor.class);
    addWarningAboutSubmit(AbstractExecutorService.class);
    addMethodAnnotation("java.util.concurrent.BlockingQueue", "poll", "(JLjava/util/concurrent/TimeUnit;)Ljava/lang/Object;", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addMethodAnnotation("java.util.Queue", "poll", "()Ljava/lang/Object;", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_LOW);
    addDefaultMethodAnnotation("java.lang.String", CheckReturnValueAnnotation.CHECK_RETURN_VALUE_HIGH);
    addMethodAnnotation("java.lang.String", "getBytes", "(Ljava/lang/String;)[B", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.lang.String", "charAt", "(I)C", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_LOW);
    addMethodAnnotation("java.lang.String", "toString", "()Ljava/lang/String;", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_LOW);
    addMethodAnnotation("java.lang.String", "length", "()I", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_LOW);
    addMethodAnnotation("java.lang.String", "matches", "(Ljava/lang/String;)Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_LOW);
    addMethodAnnotation("java.lang.String", "intern", "()Ljava/lang/String;", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addMethodAnnotation("java.lang.String", "<init>", "([BLjava/lang/String;)V", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.lang.String", "<init>", "(Ljava/lang/String;)V", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_LOW);
    addMethodAnnotation("java.lang.String", "<init>", "()V", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_LOW);
    addDefaultMethodAnnotation("java.math.BigDecimal", CheckReturnValueAnnotation.CHECK_RETURN_VALUE_HIGH);
    addMethodAnnotation("java.math.BigDecimal", "inflate", "()Ljava/math/BigInteger;", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.math.BigDecimal", "precision", "()I", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addMethodAnnotation("java.math.BigDecimal", "toBigIntegerExact", "()Ljava/math/BigInteger;", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.math.BigDecimal", "longValueExact", "()J", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.math.BigDecimal", "intValueExact", "()I", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.math.BigDecimal", "shortValueExact", "()S", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.math.BigDecimal", "byteValueExact", "()B", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.math.BigDecimal", "<init>", "(Ljava/lang/String;)V", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.math.BigDecimal", "intValue", "()I", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.math.BigDecimal", "stripZerosToMatchScale", "(J)Ljava/math/BigDecimal;", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addDefaultMethodAnnotation("java.math.BigInteger", CheckReturnValueAnnotation.CHECK_RETURN_VALUE_HIGH);
    addMethodAnnotation("java.math.BigInteger", "addOne", "([IIII)I", true, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.math.BigInteger", "subN", "([I[II)I", true, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.math.BigInteger", "<init>", "(Ljava/lang/String;)V", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addDefaultMethodAnnotation("java.sql.Connection", CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addDefaultMethodAnnotation("java.net.InetAddress", CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addMethodAnnotation("java.net.InetAddress", "getByName", "(Ljava/lang/String;)Ljava/net/InetAddress;", true, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.net.InetAddress", "getAllByName", "(Ljava/lang/String;)[Ljava/net/InetAddress;", true, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_IGNORE);
    addMethodAnnotation("java.lang.ProcessBuilder", "redirectErrorStream", "()Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addMethodAnnotation("java.lang.ProcessBuilder", "redirectErrorStream", "()Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addMethodAnnotation("java.lang.ProcessBuilder", "redirectErrorStream", "()Z", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addDefaultMethodAnnotation("jsr166z.forkjoin.ParallelArray", CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addDefaultMethodAnnotation("jsr166z.forkjoin.ParallelLongArray", CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addDefaultMethodAnnotation("jsr166z.forkjoin.ParallelDoubleArray", CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addMethodAnnotation(java.sql.Statement.class, "executeQuery", "(Ljava/lang/String;)Ljava/sql/ResultSet;", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    addMethodAnnotation(java.sql.PreparedStatement.class, "executeQuery", "()Ljava/sql/ResultSet;", false, CheckReturnValueAnnotation.CHECK_RETURN_VALUE_MEDIUM);
    AnalysisContext.currentAnalysisContext().setMissingClassWarningsSuppressed(missingClassWarningsSuppressed);
    try {
        throwableClass = Repository.lookupClass("java.lang.Throwable");
    } catch (ClassNotFoundException e) {
        AnalysisContext.reportMissingClass(e);
    }
    try {
        threadClass = Repository.lookupClass("java.lang.Thread");
    } catch (ClassNotFoundException e) {
        AnalysisContext.reportMissingClass(e);
    }
}
Example 33
Project: myria-master  File: PythonWorker.java View source code
/**
   * @throws IOException in case of error.
   */
private void startPythonWorker() throws IOException {
    String pythonWorker = MyriaConstants.PYTHONWORKER;
    ProcessBuilder pb = new ProcessBuilder(MyriaConstants.PYTHONEXEC, "-m", pythonWorker);
    final Map<String, String> env = pb.environment();
    env.put("PYTHONUNBUFFERED", "YES");
    env.put("PYTHON_EGG_CACHE", "/tmp/.python-eggs");
    pb.redirectError(Redirect.INHERIT);
    pb.redirectOutput(Redirect.INHERIT);
    // write the env variables to the path of the starting process
    worker = pb.start();
    OutputStream stdin = worker.getOutputStream();
    OutputStreamWriter out = new OutputStreamWriter(stdin, StandardCharsets.UTF_8);
    out.write(serverSocket.getLocalPort() + "\n");
    out.flush();
    clientSock = serverSocket.accept();
    setupStreams();
}
Example 34
Project: jar2bpl-master  File: SnippetTest.java View source code
protected File compileJavaFile(File sourceFile) throws IOException {
    final File tempDir = getTempDir();
    final String javac_command = String.format("javac -g %s -d %s", sourceFile.getAbsolutePath(), tempDir.getAbsolutePath());
    ProcessBuilder pb = new ProcessBuilder(javac_command.split(" "));
    pb.redirectOutput(Redirect.INHERIT);
    pb.redirectError(Redirect.INHERIT);
    Process p = pb.start();
    try {
        p.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    }
    return tempDir;
}
Example 35
Project: gerrit-master  File: InitSshd.java View source code
private void generateSshHostKeys() throws InterruptedException, IOException {
    if (!exists(site.ssh_key) && (!exists(site.ssh_rsa) || !exists(site.ssh_dsa) || !exists(site.ssh_ed25519) || !exists(site.ssh_ecdsa))) {
        System.err.print("Generating SSH host key ...");
        System.err.flush();
        // Generate the SSH daemon host key using ssh-keygen.
        //
        final String comment = "gerrit-code-review@" + hostname();
        // Workaround for JDK-6518827 - zero-length argument ignored on Win32
        String emptyPassphraseArg = HostPlatform.isWin32() ? "\"\"" : "";
        if (!exists(site.ssh_rsa)) {
            System.err.print(" rsa...");
            System.err.flush();
            new ProcessBuilder("ssh-keygen", "-q", /* quiet */
            "-t", "rsa", "-P", emptyPassphraseArg, "-C", comment, "-f", site.ssh_rsa.toAbsolutePath().toString()).redirectError(Redirect.INHERIT).redirectOutput(Redirect.INHERIT).start().waitFor();
        }
        if (!exists(site.ssh_dsa)) {
            System.err.print(" dsa...");
            System.err.flush();
            new ProcessBuilder("ssh-keygen", "-q", /* quiet */
            "-t", "dsa", "-P", emptyPassphraseArg, "-C", comment, "-f", site.ssh_dsa.toAbsolutePath().toString()).redirectError(Redirect.INHERIT).redirectOutput(Redirect.INHERIT).start().waitFor();
        }
        if (!exists(site.ssh_ed25519)) {
            System.err.print(" ed25519...");
            System.err.flush();
            try {
                new ProcessBuilder("ssh-keygen", "-q", /* quiet */
                "-t", "ed25519", "-P", emptyPassphraseArg, "-C", comment, "-f", site.ssh_ed25519.toAbsolutePath().toString()).redirectError(Redirect.INHERIT).redirectOutput(Redirect.INHERIT).start().waitFor();
            } catch (Exception e) {
                System.err.print(" Failed to generate ed25519 key, continuing...");
                System.err.flush();
            }
        }
        if (!exists(site.ssh_ecdsa)) {
            System.err.print(" ecdsa...");
            System.err.flush();
            try {
                new ProcessBuilder("ssh-keygen", "-q", /* quiet */
                "-t", "ecdsa", "-P", emptyPassphraseArg, "-C", comment, "-f", site.ssh_ecdsa.toAbsolutePath().toString()).redirectError(Redirect.INHERIT).redirectOutput(Redirect.INHERIT).start().waitFor();
            } catch (Exception e) {
                System.err.print(" Failed to generate ecdsa key, continuing...");
                System.err.flush();
            }
        }
        System.err.println(" done");
    }
}
Example 36
Project: cattle-master  File: HaConfigManager.java View source code
protected long dbSize() throws IOException {
    ProcessBuilder pb = DB.get().equals("mysql") ? new ProcessBuilder("mysql", "--skip-column-names", "-s", "-uroot", "-e", "SELECT SUM(data_length)/power(1024,2) AS dbsize_mb FROM information_schema.tables WHERE table_schema='cattle' GROUP BY table_schema;") : new ProcessBuilder("psql", "cattle", "cattle", "-t", "-q", "-c", "SELECT pg_database_size('cattle')/power(1024,2)");
    pb.redirectError(Redirect.INHERIT);
    Process p = pb.start();
    try (InputStream in = p.getInputStream()) {
        return Long.parseLong(IOUtils.toString(in).split("[.]")[0].trim());
    }
}
Example 37
Project: android-sdk-sources-for-api-level-23-master  File: ProcessBuilderTest.java View source code
public void testCommand() {
    ProcessBuilder pb = new ProcessBuilder("command");
    assertEquals(1, pb.command().size());
    assertEquals("command", pb.command().get(0));
    // Regression for HARMONY-2675
    pb = new ProcessBuilder("AAA");
    pb.command("BBB", "CCC");
    List<String> list = pb.command();
    list.add("DDD");
    String[] command = new String[3];
    list.toArray(command);
    assertTrue(Arrays.equals(new String[] { "BBB", "CCC", "DDD" }, command));
}
Example 38
Project: AutoGrader-master  File: Test.java View source code
public static boolean compile(String[] compile) {
    ProcessBuilder probuilder = new ProcessBuilder(compile);
    probuilder.directory(new File(npath));
    File beforeoutput = new File(path + "compile.txt");
    probuilder.redirectErrorStream(true);
    probuilder.redirectOutput(Redirect.appendTo(beforeoutput));
    System.out.println("Compiling..");
    try {
        flag = true;
        Process proccess = probuilder.start();
        proccess.waitFor();
        BufferedReader compileReader = new BufferedReader(new FileReader(path + "compile.txt"));
        String error = null;
        if ((error = compileReader.readLine()) == null) {
            System.out.println("Compiled successfully!");
        } else {
            flag = false;
            do {
                System.out.println(error);
            } while ((error = compileReader.readLine()) != null);
        }
        compileReader.close();
        beforeoutput.delete();
        new File(path + compile[1]).delete();
    } catch (IOException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InterruptedException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }
    return flag;
}
Example 39
Project: sling-master  File: LauncherCallable.java View source code
private ProcessDescription start(final File jar) throws Exception {
    final ProcessDescription cfg = new ProcessDescription(this.configuration.getId(), this.configuration.getFolder());
    final ProcessBuilder builder = new ProcessBuilder();
    final List<String> args = new ArrayList<String>();
    args.add("java");
    add(args, this.configuration.getVmOpts());
    add(args, this.configuration.getVmDebugOpts(this.environment.getDebug()));
    args.add("-cp");
    args.add("bin");
    args.add(Main.class.getName());
    // first three arguments: jar, listener port, verbose
    args.add(jar.getPath());
    args.add(String.valueOf(cfg.getControlListener().getPort()));
    args.add("true");
    // from here on launchpad properties
    add(args, this.configuration.getOpts());
    final String contextPath = this.configuration.getContextPath();
    if (contextPath != null && contextPath.length() > 0 && !contextPath.equals("/")) {
        args.add("-r");
        args.add(contextPath);
    }
    if (this.configuration.getPort() != null) {
        args.add("-p");
        args.add(this.configuration.getPort());
    }
    if (this.configuration.getControlPort() != null) {
        args.add("-j");
        args.add(this.configuration.getControlPort());
    }
    if (this.configuration.getRunmode() != null && this.configuration.getRunmode().length() > 0) {
        args.add("-Dsling.run.modes=" + this.configuration.getRunmode());
    }
    if (!this.environment.isShutdownOnExit()) {
        args.add("start");
    }
    builder.command(args.toArray(new String[args.size()]));
    builder.directory(this.configuration.getFolder());
    builder.redirectErrorStream(true);
    logger.info("Starting Launchpad " + this.configuration.getId() + "...");
    String stdOutFile = this.configuration.getStdOutFile();
    if (StringUtils.isNotBlank(stdOutFile)) {
        File absoluteStdOutFile = new File(builder.directory(), stdOutFile);
        // make sure to create the parent directories (if they do not exist yet)
        absoluteStdOutFile.getParentFile().mkdirs();
        builder.redirectOutput(absoluteStdOutFile);
        logger.info("Redirecting stdout and stderr to " + absoluteStdOutFile);
    } else {
        builder.redirectOutput(Redirect.INHERIT);
    }
    logger.debug("Launchpad cmd: " + builder.command());
    logger.debug("Launchpad dir: " + builder.directory());
    try {
        cfg.setProcess(builder.start());
    } catch (final IOException e) {
        if (cfg.getProcess() != null) {
            cfg.getProcess().destroy();
            cfg.setProcess(null);
        }
        throw new Exception("Could not start the Launchpad", e);
    }
    return cfg;
}
Example 40
Project: incubator-systemml-master  File: Utility.java View source code
/**
	 * This function will run command specified abd redirect stdout/stderr per instruction.
	 *
	 * @param
	 * @param
	 * @return
	 */
public static int runCommand(String[] command, String strCurDir, String strOutputFile, String strErrorFile, String strMessage) throws IOException {
    if (strMessage != null && strMessage.trim().length() > 0)
        debugPrint(Constants.DEBUG_INFO, strMessage, strOutputFile);
    debugPrint(Constants.DEBUG_CODE, "Running command: '" + String.join(" ", command) + "'", strOutputFile);
    ProcessBuilder processBuilder = new ProcessBuilder(command);
    // Set current working directory for command to run.
    if (strCurDir != null && strCurDir.trim().length() > 0)
        processBuilder.directory(new File(strCurDir));
    // Start the process
    Process process = processBuilder.start();
    // Read the output from runtime and redirect to file/stdout
    InputStreamReader insIn = new InputStreamReader(process.getInputStream());
    BufferedReader bufIn = new BufferedReader(insIn);
    boolean bWriteOutToFile = false;
    PrintWriter writerOut = null;
    try {
        if (strOutputFile != null && strOutputFile.trim().length() > 0)
            bWriteOutToFile = true;
        if (bWriteOutToFile)
            writerOut = new PrintWriter(new BufferedWriter(new FileWriter(strOutputFile, true)));
        // Read standard output after running the command.
        String strLine;
        while ((strLine = bufIn.readLine()) != null) {
            if (bWriteOutToFile)
                writerOut.println(strLine);
            else
                debugPrint(Constants.DEBUG_INFO2, "StdOut: " + strLine);
        }
    } catch (IOException ioe) {
        debugPrint(Constants.DEBUG_ERROR, "Exception occured while reading from process output: " + ioe, strOutputFile);
    }
    if (bWriteOutToFile && writerOut != null)
        writerOut.close();
    // Read the output from runtime and redirect to file/stdout
    InputStreamReader insErr = new InputStreamReader(process.getErrorStream());
    BufferedReader bufErr = new BufferedReader(insErr);
    boolean bWriteErrToFile = false;
    PrintWriter writerErr = null;
    try {
        if (strErrorFile != null && strErrorFile.trim().length() > 0)
            bWriteErrToFile = true;
        if (bWriteErrToFile)
            writerErr = new PrintWriter(new BufferedWriter(new FileWriter(strErrorFile, true)));
        // Read standard output after running the command.
        String strLine;
        while ((strLine = bufErr.readLine()) != null) {
            if (bWriteErrToFile)
                writerErr.println(strLine);
            else
                debugPrint(Constants.DEBUG_INFO2, "StdErr: " + strLine);
        }
    } catch (IOException ioe) {
        debugPrint(Constants.DEBUG_ERROR, "Exception occured while reading from process error stream: " + ioe, strOutputFile);
    }
    if (bWriteErrToFile && writerErr != null)
        writerErr.close();
    // Wait for program to exit.
    int exitValue = -1;
    try {
        exitValue = process.waitFor();
    } catch (InterruptedException ie) {
        debugPrint(Constants.DEBUG_ERROR, "Program interrunpted: " + ie);
    }
    debugPrint(Constants.DEBUG_CODE, "Program '" + String.join(" ", command) + "' exited with exit status " + exitValue, strOutputFile);
    return exitValue;
}
Example 41
Project: giulius-selenium-tests-master  File: FfmpegVideoRecorder.java View source code
@Override
public void start() {
    String display = settings.getString("DISPLAY");
    if (display == null) {
        Logger.getLogger(FfmpegVideoRecorder.class.getName()).log(Level.SEVERE, null, new Error("ENV DISPLAY VARIABLE NOT SET"));
        return;
    }
    log("Starting ffmpeg");
    String filename = settings.getString("video");
    if (filename == null) {
        filename = settings.getString("testMethodQname", "screencast");
        String base = filename;
        if (!"screencast".equals(filename)) {
            filename = filename.replace('.', '-');
        }
        File f = new File("target");
        if (f.exists() && f.isDirectory()) {
            filename = "target" + File.separator + filename;
        }
        filename += '_' + dateString() + ".mp4";
    }
    int threads = settings.getInt("video.threads", 2);
    System.setProperty("video.file", filename);
    String cmdline = "ffmpeg -y -v 1 -r 15 -f x11grab -s 1280x1024 -i " + display + " -vcodec libx264 -threads " + threads + " -q:v 2 -r 30 " + filename;
    log("Will run ffmpeg with command-line: '" + cmdline + "'");
    log("Recording video to " + filename);
    String urlBase = settings.getString("base.video.url");
    if (urlBase != null) {
        if (!urlBase.endsWith("/")) {
            urlBase += "/";
        }
        urlBase += filename;
        String build = settings.getString("BUILD_NUMBER", "lastSuccessfulBuild");
        urlBase = urlBase.replace("_BUILD_", build);
        log("Video available from " + urlBase);
    }
    ProcessBuilder pb = new ProcessBuilder().inheritIO().command(cmdline.split("\\s"));
    pb.redirectOutput(Redirect.PIPE);
    pb.redirectError(Redirect.PIPE);
    try {
        synchronized (this) {
            process = pb.start();
        }
        log("Started ffmpeg");
    } catch (IOException ex) {
        Logger.getLogger(FfmpegVideoRecorder.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Example 42
Project: moditect-master  File: AddModuleInfoTest.java View source code
@Test
public void foo() throws Exception {
    prepareTestJar();
    String javaHome = System.getProperty("java.home");
    String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
    ProcessBuilder builder = new ProcessBuilder(javaBin, "--module-path", GENERATED_TEST_RESOURCES + File.separator + "example.jar", "--module", "com.example").redirectOutput(Redirect.INHERIT);
    Process process = builder.start();
    process.waitFor();
    if (process.exitValue() == 0) {
        throw new AssertionError();
    }
    new AddModuleInfo("module com.example {}", "com.example.HelloWorld", Paths.get("target", "generated-test-resources", "example.jar"), Paths.get("target", "generated-test-modules"), false).run();
    builder = new ProcessBuilder(javaBin, "--module-path", GENERATED_TEST_MODULES + File.separator + "example.jar", "--module", "com.example");
    process = builder.start();
    process.waitFor();
    if (process.exitValue() != 0) {
        throw new AssertionError();
    }
}
Example 43
Project: framesoc-master  File: ExternalProgramWrapper.java View source code
/**
	 * Execute the external program, passing the command stdout and stderr to
	 * the given processor.
	 * 
	 * The external program is destroyed if the progress monitor is stopped.
	 * 
	 * @param monitor
	 *            progress monitor
	 * @param processor
	 *            line processor
	 * @return the execution status
	 */
public IStatus execute(IProgressMonitor monitor, ILineProcessor processor) {
    logger.debug("Executing: {}", fCommand);
    try {
        ProcessBuilder pb = new ProcessBuilder(fCommand);
        pb.redirectErrorStream(true);
        pb.redirectOutput(Redirect.PIPE);
        Process p = pb.start();
        BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";
        while ((line = bri.readLine()) != null && !monitor.isCanceled()) {
            processor.process(line);
        }
        bri.close();
        p.destroy();
    } catch (IOException e) {
        System.err.println(e.getMessage());
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Example 44
Project: openmrs-core-master  File: SimpleXStreamSerializerTest.java View source code
/**
	 * @throws SerializationException
	 * @see SimpleXStreamSerializer#deserialize(String,Class)
	 */
@Test
public void deserialize_shouldNotDeserializeProxies() throws SerializationException {
    String serialized = "<dynamic-proxy>" + "<interface>org.openmrs.OpenmrsObject</interface>" + "<handler class=\"java.beans.EventHandler\">" + "<target class=\"java.lang.ProcessBuilder\">" + "<command>" + "<string>someApp</string>" + "</command></target>" + "<action>start</action>" + "</handler>" + "</dynamic-proxy>";
    expectedException.expect(SerializationException.class);
    new SimpleXStreamSerializer().deserialize(serialized, OpenmrsObject.class);
}
Example 45
Project: cloudstore-master  File: LocalServerProcessLauncher.java View source code
public boolean start() throws IOException {
    // Check the configuration 'localServerProcess.enabled'.
    if (!LsConfig.isLocalServerProcessEnabled())
        return false;
    // LocalServer is not started inside the separate VM process. Hence, we don't launch the VM at all.
    if (!LsConfig.isLocalServerEnabled())
        return false;
    final File javaExecutableFile = getJavaExecutableFile();
    if (javaExecutableFile == null)
        return false;
    final File thisJarFile = getThisJarFile();
    if (thisJarFile == null)
        return false;
    final List<String> command = new ArrayList<>();
    command.add(javaExecutableFile.getPath());
    populateJvmArguments(command);
    populateConfigSystemProperties(command);
    command.add("-jar");
    command.add(thisJarFile.getPath());
    logger.info("start: command={}", command);
    final ProcessBuilder pb = new ProcessBuilder(command);
    final File processRedirectInputFile = getProcessRedirectInputFile();
    final File processRedirectOutputFile = getProcessRedirectOutputFile();
    // 0-byte-file
    processRedirectInputFile.createNewFile();
    pb.redirectInput(processRedirectInputFile.getIoFile());
    pb.redirectOutput(processRedirectOutputFile.getIoFile());
    pb.redirectError(processRedirectOutputFile.getIoFile());
    final Process process = pb.start();
    if (process == null) {
        logger.warn("start: process=null");
        return false;
    }
    waitUntilServerOnline();
    return true;
}
Example 46
Project: dkpro-core-master  File: SfstAnnotator.java View source code
@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
    CAS cas = aJCas.getCas();
    modelProvider.configure(cas);
    featuresParser.configure(cas);
    String modelEncoding = (String) modelProvider.getResourceMetaData().get("model.encoding");
    if (modelEncoding == null) {
        throw new AnalysisEngineProcessException(new Throwable("Model should contain encoding metadata"));
    }
    File model = modelProvider.getResource();
    File executable;
    try {
        executable = runtimeProvider.getFile("fst-infl2");
    } catch (IOException e) {
        throw new AnalysisEngineProcessException(e);
    }
    ProcessBuilder pb = new ProcessBuilder(executable.getAbsolutePath(), "-s", "-q", model.getAbsolutePath());
    pb.redirectError(Redirect.INHERIT);
    StringBuffer lastOut = new StringBuffer();
    String lastIn = null;
    boolean success = false;
    Process proc = null;
    try {
        proc = pb.start();
        PrintWriter out = new PrintWriter(new OutputStreamWriter(proc.getOutputStream(), modelEncoding));
        BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), modelEncoding));
        for (Sentence sentence : select(aJCas, Sentence.class)) {
            List<Token> tokens = selectCovered(Token.class, sentence);
            // Skip empty sentences
            if (tokens.isEmpty()) {
                continue;
            }
            // Send full sentence
            for (Token token : tokens) {
                lastOut.append(token.getCoveredText()).append(' ');
                out.printf("%s%n", token.getCoveredText());
                out.printf("%s%n", FLUSH_TOKEN);
            }
            out.flush();
            // Read sentence tags
            tokenLoop: for (Token token : tokens) {
                boolean skip = false;
                analysisLoop: while ((lastIn = in.readLine()) != null) {
                    // Analysis line
                    if (lastIn.startsWith(">")) {
                        // Echo line, ignore.
                        continue analysisLoop;
                    }
                    if (lastIn.contains(FLUSH_TOKEN)) {
                        // End of analysis
                        continue tokenLoop;
                    }
                    if (lastIn.startsWith("no result for")) {
                        // No analysis for this token
                        MorphologicalFeatures morph = new MorphologicalFeatures(aJCas, token.getBegin(), token.getEnd());
                        morph.setValue("");
                        morph.addToIndexes();
                        if (token.getMorph() == null) {
                            token.setMorph(morph);
                        }
                        // the flush marker.
                        continue analysisLoop;
                    }
                    // Analysis line
                    if (!skip) {
                        MorphologicalFeatures morph = featuresParser.parse(aJCas, token, lastIn);
                        if (token.getMorph() == null) {
                            token.setMorph(morph);
                        }
                    }
                    switch(mode) {
                        case FIRST:
                            // Go to next token after reading first analysis
                            skip = true;
                            break;
                        case ALL:
                            // We record all analyses
                            break;
                    }
                }
            }
            lastOut.setLength(0);
        }
        success = true;
    } catch (IOException e) {
        throw new AnalysisEngineProcessException(e);
    } finally {
        if (!success) {
            getLogger().error("Sent before error: [" + lastOut + "]");
            getLogger().error("Last response before error: [" + lastIn + "]");
        }
        if (proc != null) {
            proc.destroy();
        }
    }
}
Example 47
Project: javataint-master  File: Bootstrap.java View source code
public static void main(String[] args) {
    String rt14Jar = null, rt15Jar = null, installPath = null, outputJar = "jt-bootlib.jar";
    for (int i = 0; i < args.length; i++) {
        if ("-r14".equals(args[i]) && i != args.length - 1)
            rt14Jar = args[++i];
        else if ("-r15".equals(args[i]) && i != args.length - 1)
            rt15Jar = args[++i];
        else if ("-j".equals(args[i]) && i != args.length - 1)
            outputJar = args[++i];
        else if ("-i".equals(args[i]) && i != args.length - 1)
            installPath = args[++i];
        else if ("-d".equals(args[i]))
            debug = true;
        else
            usage();
    }
    if ((rt14Jar != null && rt15Jar == null) || (rt14Jar == null && rt15Jar != null))
        usage();
    if (VmInfo.version() == VmInfo.VERSION_UNKNOWN || VmInfo.vendor() == VmInfo.VENDOR_UNKNOWN) {
        System.err.println("Unknown java configuration:");
        System.err.println("vendor: " + System.getProperty("java.vendor"));
        System.err.println("vm name: " + System.getProperty("java.vm.name"));
        System.err.println("version: " + System.getProperty("java.specification.version"));
        System.err.println("Please report this error to JavaTaint");
        System.exit(-1);
    }
    try {
        addInstrumentation("java.lang.String", StringAdapter.builder());
        addInstrumentation("java.lang.StringBuffer", StringBufferAdapter.builder());
        addInstrumentation("java.lang.ClassLoader", ClassLoaderAdapter.builder());
        addInstrumentation("java.io.OutputStream", XssAdapter.builder("java/io/OutputStream"));
        addInstrumentation("java.io.PrintWriter", XssAdapter.builder("java/io/PrintWriter"));
        addInstrumentation("java.lang.Runtime", RuntimeAdapter.builder());
        addInstrumentation("java.lang.Thread", ThreadAdapter.builder());
        addInstrumentation("java.sql.Connection", ConnectionAdapter.builder());
        addAnalysis("java.io.File", FileAdapter.CloneOptimizer.builder());
        addInstrumentation("java.io.File", FileAdapter.builder());
        if (VmInfo.vendor() == VmInfo.VENDOR_BEA)
            addInstrumentation("jrockit.vm.StringMaker", StringMakerAdapter.builder());
        if (VmInfo.version() >= VmInfo.VERSION1_5) {
            addInstrumentation("java.lang.StringBuilder", StringBuilderAdapter.builder());
            addInstrumentation("java.lang.ProcessBuilder", ProcessBuilderAdapter.builder());
            writeJarFile(rt15Jar, outputJar, installPath);
        } else {
            writeJarFile(rt14Jar, outputJar, installPath);
        }
    } catch (Exception e) {
        System.err.println("Bootstrap error: " + e);
        e.printStackTrace();
        System.exit(-1);
    }
}
Example 48
Project: lucene-solr-master  File: TestIndexWriterOnJRECrash.java View source code
/** fork ourselves in a new jvm. sets -Dtests.crashmode=true */
@SuppressForbidden(reason = "ProcessBuilder requires java.io.File for CWD")
public void forkTest() throws Exception {
    List<String> cmd = new ArrayList<>();
    cmd.add(Paths.get(System.getProperty("java.home"), "bin", "java").toString());
    cmd.add("-Xmx512m");
    cmd.add("-Dtests.crashmode=true");
    // passing NIGHTLY to this test makes it run for much longer, easier to catch it in the act...
    cmd.add("-Dtests.nightly=true");
    cmd.add("-DtempDir=" + tempDir);
    cmd.add("-Dtests.seed=" + SeedUtils.formatSeed(random().nextLong()));
    cmd.add("-ea");
    cmd.add("-cp");
    cmd.add(System.getProperty("java.class.path"));
    cmd.add("org.junit.runner.JUnitCore");
    cmd.add(getClass().getName());
    ProcessBuilder pb = new ProcessBuilder(cmd).directory(tempDir.toFile()).redirectInput(Redirect.INHERIT).redirectErrorStream(true);
    Process p = pb.start();
    // We pump everything to stderr.
    PrintStream childOut = System.err;
    Thread stdoutPumper = ThreadPumper.start(p.getInputStream(), childOut);
    if (VERBOSE)
        childOut.println(">>> Begin subprocess output");
    p.waitFor();
    stdoutPumper.join();
    if (VERBOSE)
        childOut.println("<<< End subprocess output");
}
Example 49
Project: google-java-format-master  File: MainTest.java View source code
@Test
public void testMain() throws Exception {
    Process process = new ProcessBuilder(ImmutableList.of(Paths.get(System.getProperty("java.home")).resolve("bin/java").toString(), "-cp", System.getProperty("java.class.path"), Main.class.getName())).redirectError(Redirect.PIPE).redirectOutput(Redirect.PIPE).start();
    process.waitFor();
    String err = new String(ByteStreams.toByteArray(process.getErrorStream()), UTF_8);
    assertThat(err).contains("Usage: google-java-format");
    assertThat(process.exitValue()).isEqualTo(0);
}
Example 50
Project: XHAIL-master  File: Dialler.java View source code
public Map.Entry<Values, Collection<Collection<String>>> execute(int iter) {
    if (iter < 0)
        throw new IllegalArgumentException("Illegal 'iter' argument in Dialler.execute(int): " + iter);
    calls += 1;
    try {
        solvable.save(iter, Files.newOutputStream(source));
        try {
            if (debug)
                Logger.message(String.format("*** Info  (%s): calling '%s'", Logger.SIGNATURE, String.join(" ", this.gringo)));
            Process gringo = new //
            ProcessBuilder(//
            this.gringo).redirectError(Redirect.to(errors.toFile())).redirectOutput(Redirect.to(middle.toFile())).start();
            gringo.waitFor();
            handle(Files.newInputStream(errors));
            try {
                if (debug)
                    Logger.message(String.format("*** Info  (%s): calling '%s'", Logger.SIGNATURE, String.join(" ", this.clasp)));
                Process clasp = new ProcessBuilder(this.clasp).redirectOutput(Redirect.to(target.toFile())).start();
                clasp.waitFor();
                try {
                    return Acquirer.from(Files.newInputStream(target)).parse();
                } catch (IOException e) {
                    if (!output)
                        Logger.error("cannot read from 'clasp' process");
                }
            } catch (IOException e) {
                if (!output)
                    Logger.error("cannot launch 'clasp' process");
            } catch (InterruptedException e) {
                if (!output)
                    Logger.error("'clasp' process was interrupted");
            }
        } catch (IOException e) {
            if (!output)
                Logger.error("cannot launch 'gringo' process");
        } catch (InterruptedException e) {
            if (!output)
                Logger.error("'gringo' process was interrupted");
        }
    } catch (IOException e) {
        if (!output)
            Logger.error("cannot write to 'gringo' process");
    }
    return new SimpleEntry<Values, Collection<Collection<String>>>(null, Collections.emptySet());
}
Example 51
Project: TarsosTranscoder-master  File: FFMPEGExecutor.java View source code
public AudioInputStream pipe(Attributes attributes) throws EncoderException {
    String pipeEnvironment;
    String pipeArgument;
    File pipeLogFile;
    int pipeBuffer;
    if (System.getProperty("os.name").indexOf("indows") > 0) {
        pipeEnvironment = "cmd.exe";
        pipeArgument = "/C";
    } else {
        pipeEnvironment = "/bin/bash";
        pipeArgument = "-c";
    }
    pipeLogFile = new File("decoder_log.txt");
    //buffer 1/4 second of audio.
    pipeBuffer = attributes.getSamplingRate() / 4;
    AudioFormat audioFormat = Encoder.getTargetAudioFormat(attributes);
    String command = toString();
    ProcessBuilder pb = new ProcessBuilder(pipeEnvironment, pipeArgument, command);
    pb.redirectError(Redirect.appendTo(pipeLogFile));
    LOG.fine("Starting piped decoding process");
    final Process process;
    try {
        process = pb.start();
    } catch (IOException e1) {
        throw new EncoderException("Problem starting piped sub process: " + e1.getMessage());
    }
    InputStream stdOut = new BufferedInputStream(process.getInputStream(), pipeBuffer);
    //read and ignore the 46 byte wav header, only pipe the pcm samples to the audioinputstream
    byte[] header = new byte[46];
    double sleepSeconds = 0;
    //seconds
    double timeoutLimit = 20;
    try {
        while (stdOut.available() < header.length) {
            try {
                Thread.sleep(100);
                sleepSeconds += 0.1;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (sleepSeconds > timeoutLimit) {
                throw new Error("Could not read from pipe within " + timeoutLimit + " seconds: timeout!");
            }
        }
        int bytesRead = stdOut.read(header);
        if (bytesRead != header.length) {
            throw new EncoderException("Could not read complete WAV-header from pipe. This could result in mis-aligned frames!");
        }
    } catch (IOException e1) {
        throw new EncoderException("Problem reading from piped sub process: " + e1.getMessage());
    }
    final AudioInputStream audioStream = new AudioInputStream(stdOut, audioFormat, AudioSystem.NOT_SPECIFIED);
    //This thread waits for the end of the subprocess.
    new Thread(new Runnable() {

        public void run() {
            try {
                process.waitFor();
                LOG.fine("Finished piped decoding process");
            } catch (InterruptedException e) {
                LOG.severe("Interrupted while waiting for sub process exit.");
                e.printStackTrace();
            }
        }
    }, "Decoding Pipe Reader").start();
    return audioStream;
}
Example 52
Project: jCAE-master  File: TriMultPoly.java View source code
private void initExe() {
    if (initDone)
        return;
    initDone = true;
    if (executable == null)
        return;
    ProcessBuilder pb = new ProcessBuilder(executable, "-d");
    try {
        pb.redirectOutput(Redirect.INHERIT);
        process = pb.start();
        stdin = Channels.newChannel(process.getOutputStream());
        stderr = Channels.newChannel(process.getErrorStream());
    } catch (IOException ex) {
        LOGGER.log(Level.SEVERE, "Cannot run " + executable, ex);
    }
}
Example 53
Project: writer2latex-master  File: ExternalApps.java View source code
/** Execute an external application
     *  @param sAppName the name of the application to execute
     *  @param sCommand subcommand/option to pass to the command
     *  @param sFileName the file name to use
     *  @param workDir the working directory to use
     *  @param env map of environment variables to set (or null if no variables needs to be set)
     *  @param bWaitFor true if the method should wait for the execution to finish
     *  @return error code 
     */
public int execute(String sAppName, String sCommand, String sFileName, File workDir, Map<String, String> env, boolean bWaitFor) {
    // Assemble the command
    String[] sApp = getApplication(sAppName);
    if (sApp == null) {
        return 1;
    }
    try {
        Vector<String> command = new Vector<String>();
        command.add(sApp[0]);
        String[] sArguments = sApp[1].split(" ");
        for (String s : sArguments) {
            command.add(s.replace("%c", sCommand).replace("%s", sFileName));
        }
        ProcessBuilder pb = new ProcessBuilder(command);
        pb.directory(workDir);
        if (env != null) {
            pb.environment().putAll(env);
        }
        Process proc = pb.start();
        // Gobble the error stream of the application
        StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
        // Gobble the output stream of the application
        StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
        // Kick them off
        errorGobbler.start();
        outputGobbler.start();
        // Any error?
        return bWaitFor ? proc.waitFor() : 0;
    } catch (InterruptedException e) {
        return 1;
    } catch (IOException e) {
        return 1;
    }
}
Example 54
Project: xmvn-master  File: JavadocMojo.java View source code
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        if (StringUtils.isEmpty(encoding))
            encoding = "UTF-8";
        if (StringUtils.isEmpty(docencoding))
            docencoding = "UTF-8";
        Set<Path> sourcePaths = //
        Stream.concat(//
        reactorProjects.stream(), //
        reactorProjects.stream().map( p -> p.getExecutionProject())).filter(//
         project -> project != null).filter(//
         project -> !project.getPackaging().equals("pom")).filter(//
         project -> project.getArtifact().getArtifactHandler().getLanguage().equals("java")).filter(//
         project -> project.getCompileSourceRoots() != null).map(//
         project -> project.getCompileSourceRoots().stream().filter( compileRoot -> compileRoot != null).map( compileRoot -> Paths.get(compileRoot)).map( sourcePath -> sourcePath.isAbsolute() ? sourcePath : project.getBasedir().toPath().resolve(sourcePath).toAbsolutePath()).filter(//
         sourcePath -> Files.isDirectory(sourcePath))).flatMap(//
         x -> x).collect(Collectors.toSet());
        Set<Path> files = new LinkedHashSet<>();
        for (Path sourcePath : sourcePaths) findJavaSources(files, sourcePath);
        if (files.isEmpty()) {
            logger.info("Skipping Javadoc generation: no Java sources found");
            return;
        }
        Path outputDir = getOutputDir();
        if (!Files.isDirectory(outputDir))
            Files.createDirectories(outputDir);
        outputDir = outputDir.toRealPath();
        List<String> opts = new ArrayList<>();
        opts.add("-private");
        opts.add("-use");
        opts.add("-version");
        opts.add("-Xdoclint:none");
        opts.add("-classpath");
        opts.add(quoted(StringUtils.join(getClasspath().iterator(), ":")));
        opts.add("-encoding");
        opts.add(quoted(encoding));
        opts.add("-sourcepath");
        opts.add(quoted(StringUtils.join(sourcePaths.iterator(), ":")));
        opts.add("-charset");
        opts.add(quoted(docencoding));
        opts.add("-d");
        opts.add(quoted(outputDir));
        opts.add("-docencoding");
        opts.add(quoted(docencoding));
        opts.add("-doctitle");
        opts.add(quoted("Javadoc for package XXX"));
        for (Path file : files) opts.add(quoted(file));
        Files.write(outputDir.resolve("args"), opts, StandardOpenOption.CREATE);
        Path javadocExecutable = //
        Paths.get(System.getenv("JAVA_HOME")).resolve(//
        "bin").resolve(//
        "javadoc").toRealPath();
        ProcessBuilder pb = new ProcessBuilder(javadocExecutable.toString(), "@args");
        pb.directory(outputDir.toFile());
        pb.redirectOutput(Redirect.INHERIT);
        pb.redirectError(Redirect.INHERIT);
        Process process = pb.start();
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            throw new MojoExecutionException("Javadoc failed with exit code " + exitCode);
        }
    } catch (IOExceptionInterruptedException |  e) {
        throw new MojoExecutionException("Unable to execute javadoc command: " + e.getMessage(), e);
    }
}
Example 55
Project: hybridbpm-master  File: FormEditor.java View source code
private void compileTheme() {
    try {
        binder.commit();
        ServletContext context = VaadinServlet.getCurrent().getServletContext();
        String fullPath = context.getRealPath("/VAADIN/themes/dashboard");
        String customScssFileName = fullPath + "/custom.scss";
        SassCompiler.writeFile(customScssFileName, module.getModel());
        ProcessBuilder processBuilder = new ProcessBuilder("java", "-cp", "../../../WEB-INF/lib/*", "com.vaadin.sass.SassCompiler", "styles.scss", "styles.css");
        processBuilder.directory(new File(fullPath));
        File error = new File(fullPath, "custom.scss.log");
        processBuilder.redirectErrorStream(true);
        processBuilder.redirectError(Redirect.PIPE);
        Process process = processBuilder.start();
        process.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        StringBuilder builder = new StringBuilder();
        String line = reader.readLine();
        while (line != null) {
            builder.append(line);
            line = reader.readLine();
        }
        reader.close();
        if (!builder.toString().trim().isEmpty()) {
            throw new Exception(builder.toString());
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, ex.getMessage(), ex);
        Notification.show("Error", ex.getMessage(), Notification.Type.ERROR_MESSAGE);
    }
}
Example 56
Project: arquillian-daemon-master  File: ManagedDaemonDeployableContainer.java View source code
/**
     * Starts the process, then forwards control to {@link DaemonDeployableContainerBase#start()} to connect.
     *
     * @see org.jboss.arquillian.daemon.container.common.DaemonDeployableContainerBase#start()
     */
@Override
public void start() throws LifecycleException {
    // Build the launch command
    final File javaHome = new File(SecurityActions.getSystemProperty(SYSPROP_KEY_JAVA_HOME));
    final List<String> command = new ArrayList<String>(10);
    command.add(javaHome.getAbsolutePath() + "/bin/java");
    command.add("-jar");
    command.add(serverjarFile.getAbsolutePath());
    final InetSocketAddress remoteAddress = this.getRemoteAddress();
    command.add(remoteAddress.getHostString());
    command.add(Integer.toString(remoteAddress.getPort()));
    // Launch the process
    final ProcessBuilder processBuilder = new ProcessBuilder(command);
    processBuilder.redirectErrorStream(true);
    processBuilder.redirectOutput(Redirect.INHERIT);
    final Process process;
    try {
        process = processBuilder.start();
        this.remoteProcess = process;
    } catch (final IOException e) {
        throw new LifecycleException("Could not start container", e);
    }
    // Add a shutdown hook for when this current process terminates to kill the one we've launched
    final Runnable shutdownServerRunnable = new Runnable() {

        @Override
        public void run() {
            if (process != null) {
                process.destroy();
                try {
                    process.waitFor();
                } catch (final InterruptedException e) {
                    Thread.interrupted();
                    throw new RuntimeException("Interrupted while awaiting server daemon process termination", e);
                }
            }
        }
    };
    shutdownHookThread = new Thread(shutdownServerRunnable);
    Runtime.getRuntime().addShutdownHook(shutdownHookThread);
    // Call the super implementation (to handle connect)
    super.start();
}
Example 57
Project: h2o-3-master  File: H2OBuildVersion.java View source code
private String calcBranch() {
    try {
        Process p = new ProcessBuilder("git", "branch").start();
        p.waitFor();
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = br.readLine();
        while (line != null) {
            if (!line.startsWith("*")) {
                line = br.readLine();
                continue;
            }
            String branch = line.substring(2);
            return branch;
        }
        return "(unknown)";
    } catch (Exception e) {
        return "(unknown)";
    }
}
Example 58
Project: MassivePixelEnvironment-master  File: Process.java View source code
public void shutDown() throws IOException {
    // kill all previously launched process'
    java.lang.Process kp;
    java.lang.ProcessBuilder pb;
    try {
        pb = new ProcessBuilder("pkill", "-9", "-f", "agentlib");
        String path = "/home/vislab/Processing/sketchbook/MPEPeasy/log";
        if (debug_) {
            //				BufferedWriter pw = new BufferedWriter(new FileWriter(path + config_.getRank()));
            File log = new File(path);
            pb.redirectErrorStream(true);
            pb.redirectOutput(Redirect.appendTo(log));
        //				pw.append(config_.getRank() + ": inside shutdown");
        //				pw.close();
        }
        kp = pb.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Example 59
Project: GdxStudio-master  File: ToolBar.java View source code
void run() {
    if (!Style.btnMap.get("stop").isEnabled()) {
        Style.btnMap.get("stop").setEnabled(true);
        Style.btnMap.get("go").setEnabled(false);
    }
    if (!new File(Content.getProject() + new File(Content.getProject()).getName() + ".jar").exists())
        Export.createJar();
    pb = new ProcessBuilder("java", "-jar", Content.getProject() + new File(Content.getProject()).getName() + ".jar");
    pb.redirectOutput(Redirect.INHERIT);
    pb.redirectError(Redirect.INHERIT);
    pb.redirectInput(Redirect.INHERIT);
    try {
        runningProcess = pb.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Example 60
Project: ceylon-common-master  File: DocBuilder.java View source code
private String invokeScript(ScriptToolModel<?> model, String arg) {
    ProcessBuilder processBuilder;
    if (OSUtil.isWindows()) {
        processBuilder = new ProcessBuilder("cmd.exe", "/C", model.getScriptName(), arg);
    } else {
        processBuilder = new ProcessBuilder(model.getScriptName(), arg);
    }
    CeylonTool.setupScriptEnvironment(processBuilder, model.getScriptName());
    processBuilder.redirectError(Redirect.INHERIT);
    try {
        Process process = processBuilder.start();
        // no stdin to the tool
        process.getOutputStream().close();
        InputStream stream = process.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        StringBuffer strbuf = new StringBuffer();
        String line;
        while ((line = reader.readLine()) != null) {
            strbuf.append(line + "\n");
        }
        reader.close();
        int exit = process.waitFor();
        if (exit != 0)
            return "";
        return strbuf.toString();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "";
}
Example 61
Project: rakam-master  File: TestingEnvironment.java View source code
private int startKinesis() throws Exception {
    Path mainDir = new File(getProperty("user.dir"), ".test/kinesalite").toPath();
    String nodePath = mainDir.resolve("node/node").toFile().getAbsolutePath();
    String kinesalitePath = mainDir.resolve("node_modules/.bin/kinesalite").toFile().getAbsolutePath();
    int kinesisPort = randomPort();
    kinesisProcess = new ProcessBuilder(of(nodePath, kinesalitePath, "--port", Integer.toString(kinesisPort))).redirectErrorStream(true).redirectOutput(INHERIT).start();
    return kinesisPort;
}
Example 62
Project: jline2-master  File: TerminalLineSettings.java View source code
private String exec(final String... cmd) throws IOException, InterruptedException {
    checkNotNull(cmd);
    Log.trace("Running: ", cmd);
    Process p = null;
    if (useRedirect) {
        try {
            p = inheritInput(new ProcessBuilder(cmd)).start();
        } catch (Throwable t) {
            useRedirect = false;
        }
    }
    if (p == null) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < cmd.length; i++) {
            if (i > 0) {
                sb.append(' ');
            }
            sb.append(cmd[i]);
        }
        sb.append(" < ");
        sb.append(ttyDevice);
        p = new ProcessBuilder(shCommand, "-c", sb.toString()).start();
    }
    String result = waitAndCapture(p);
    Log.trace("Result: ", result);
    return result;
}
Example 63
Project: AP2DX-master  File: CascadingParametersAndLocalFieldsTest.java View source code
// Tests using the java.lang.Process and java.lang.ProcessBuilder classes //////////////////////////////////////////
@Test
public void cascadeOnJREClasses() throws Exception {
    new NonStrictExpectations() {

        @Cascading
        ProcessBuilder pb;

        {
            ProcessBuilder sameBuilder = pb.directory((File) any);
            assert pb != sameBuilder;
            Process process = sameBuilder.start();
            process.getOutputStream().write(5);
            process.exitValue();
            result = 1;
        }
    };
    Process process = new ProcessBuilder("test").directory(new File("myDir")).start();
    process.getOutputStream().write(5);
    process.getOutputStream().flush();
    assert process.exitValue() == 1;
}
Example 64
Project: jmockit-master  File: CascadingParametersTest.java View source code
// Tests using the java.lang.Process and java.lang.ProcessBuilder classes //////////////////////////////////////////
@Test
public void cascadeOnJREClasses(@Mocked final ProcessBuilder pb) throws Exception {
    new Expectations() {

        {
            ProcessBuilder sameBuilder = pb.directory((File) any);
            assertSame(sameBuilder, pb);
            Process process = sameBuilder.start();
            process.getOutputStream().write(5);
            process.exitValue();
            result = 1;
        }
    };
    Process process = new ProcessBuilder("test").directory(new File("myDir")).start();
    process.getOutputStream().write(5);
    process.getOutputStream().flush();
    assertEquals(1, process.exitValue());
}
Example 65
Project: gMix-master  File: TestNode.java View source code
@Override
public boolean execute(String vpid, String classpath, String classname, Map<String, String> args, Map<String, String> vmArgs, Map<String, String> environmentVariables) throws RemoteException {
    String installDir = "";
    try {
        if (managedProcesses.containsKey(vpid))
            throw new InvalidParameterException("VPID already taken on this Testnode!");
        args = replacePlaceholders(args);
        vmArgs = replacePlaceholders(vmArgs);
        Comparator<String> comparator = new Comparator<String>() {

            @Override
            public int compare(String arg0, String arg1) {
                switch(arg0) {
                    case "-noGUI":
                        return -1;
                    case "-TOOL":
                        if (arg1.equals("-noGUI"))
                            return 1;
                        else
                            return -1;
                    case "-CONFIGFILE":
                        if (arg1.equals("-OVERWRITE"))
                            return -1;
                        else
                            return 1;
                    default:
                        return 1;
                }
            }
        };
        Map<String, String> orderedArgs = new TreeMap<String, String>(comparator);
        orderedArgs.putAll(args);
        installDir = testbedInstallDir;
        String logfilePath = testbedSensorLogDir + "/process_" + vpid + ".log";
        String argString = generateArgString(orderedArgs);
        String vmArgString = generateArgString(vmArgs);
        // just for display in the coordinator
        String programCall = "java " + vmArgString + " -cp \"" + installDir + "/" + classpath + "\" \"" + classname + "\" '" + argString + "'";
        List<String> processCommand = new ArrayList<String>();
        if (environmentVariables == null) {
            environmentVariables = new HashMap<String, String>();
        }
        processCommand.add("java");
        if (!vmArgString.isEmpty())
            processCommand.add(vmArgString);
        processCommand.add(((classname.isEmpty()) ? "-jar" : "-cp"));
        processCommand.add(classpath);
        if (!classname.isEmpty())
            processCommand.add(classname);
        // add args to command
        for (Map.Entry<String, String> entry : orderedArgs.entrySet()) {
            String value = entry.getValue();
            processCommand.add(entry.getKey() + ((value.isEmpty()) ? "" : "=" + value));
        }
        // initialize the ProcessBuilder
        ProcessBuilder builder = new ProcessBuilder(processCommand);
        builder.directory(new File(installDir));
        builder.environment().putAll(environmentVariables);
        // create log file if none exists yet
        File logFile = new File(logfilePath);
        synchronized (this) {
            if (!logFile.exists())
                logFile.createNewFile();
        }
        // Redirect the output to the log file
        builder.redirectErrorStream(true);
        builder.redirectOutput(Redirect.appendTo(logFile));
        logger.info("Create process with the following command: " + builder.command());
        // create and store the Process for later termination
        Process process = builder.start();
        if (process == null)
            throw new Exception("Process start failed!");
        managedProcesses.put(vpid, process);
        managedProcessInfos.put(vpid, new ProcessInfo(vpid, programCall));
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    }
    if (ProcessUtility.isRunning(managedProcesses.get(vpid)))
        logger.info("Process successfully started.");
    else
        logger.info("Process start failed or process already exited!");
    return true;
}
Example 66
Project: beakerx-master  File: CppEvaluator.java View source code
@Override
public void run() {
    theOutput.setOutputHandler();
    InternalVariable.setValue(theOutput);
    try {
        // Parse code to find beaker_main and type
        CPP14Lexer lexer = new CPP14Lexer(new ANTLRInputStream(theCode));
        // Get a list of matched tokens
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        // Pass the tokens to the parser
        CPP14Parser parser = new CPP14Parser(tokens);
        // Parse code
        ParserRuleContext t = parser.translationunit();
        ParseTreeWalker walker = new ParseTreeWalker();
        Extractor extractor = new Extractor();
        walker.walk(extractor, t);
        String cellType = extractor.returnType;
        int beakerMainLastToken = extractor.beakerMainLastToken;
        cellType = cellType.replaceAll(">>", "> >");
        String processedCode = theCode;
        // If beaker_main was found
        if (!cellType.equals("none")) {
            int beakerMainEnd = tokens.get(beakerMainLastToken).getStopIndex();
            StringBuilder builder = new StringBuilder(theCode);
            builder.insert(beakerMainEnd + 1, createMainCaller(cellType));
            // builder.insert(0, "extern Beaker beaker;\n");
            builder.insert(0, "#include <beaker.hpp>\n");
            processedCode = builder.toString();
        }
        // Create .cpp file
        String tmpDir = tempCppFiles.getPath();
        Path filePath = Paths.get(tmpDir + "/" + theCellId + ".cpp");
        Files.write(filePath, processedCode.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
        // Prepare to compile
        String inputFile = tmpDir + "/" + theCellId + ".cpp";
        String outputFile = tmpDir + "/lib" + theCellId + ".so";
        ArrayList<String> clangCommand = new ArrayList<>(compileCommand);
        clangCommand.add("-o");
        clangCommand.add(outputFile);
        clangCommand.add(inputFile);
        clangCommand.addAll(userFlags);
        ProcessBuilder pb;
        if (System.getenv("BEAKER_CPP_DEBUG") != null) {
            logger.info("Compiling with:");
            StringBuilder builder = new StringBuilder();
            for (String s : clangCommand) {
                builder.append(s + " ");
            }
            logger.info(builder.toString());
        }
        // Compile
        pb = new ProcessBuilder(clangCommand);
        pb.directory(new File(System.getProperty("user.dir")));
        pb.redirectInput(Redirect.PIPE);
        pb.redirectOutput(Redirect.PIPE);
        pb.redirectError(Redirect.PIPE);
        Process p = pb.start();
        CellGobblerManager.getInstance().startCellGobbler(p.getInputStream(), "stderr", theOutput);
        CellGobblerManager.getInstance().startCellGobbler(p.getErrorStream(), "stderr", theOutput);
        if ((p.waitFor()) == 0) {
            loadedCells.add(theCellId);
        } else {
            theOutput.error("Compilation failed");
            theOutput.finished(null);
        }
        Object ret;
        // Execute if type is recognized
        if (!cellType.equals("none")) {
            List<String> runCommand = new ArrayList<>();
            runCommand.add(tempCppFiles.getPath() + "/cpp");
            runCommand.add(EXECUTE);
            runCommand.add(sessionId);
            runCommand.add(theCellId);
            runCommand.add(cellType);
            runCommand.add(tempCppFiles.getPath());
            for (String cell : loadedCells) {
                if (!cell.equals(theCellId)) {
                    runCommand.add(cell);
                }
            }
            pb = new ProcessBuilder(runCommand);
            pb.directory(new File(System.getProperty("user.dir")));
            pb.redirectInput(Redirect.PIPE);
            pb.redirectOutput(Redirect.PIPE);
            pb.redirectError(Redirect.PIPE);
            cellProc = pb.start();
            CellGobblerManager.getInstance().startCellGobbler(cellProc.getInputStream(), "stdout", theOutput);
            CellGobblerManager.getInstance().startCellGobbler(cellProc.getErrorStream(), "stderr", theOutput);
            if ((cellProc.waitFor()) == 0) {
                try {
                    InputStream file = new FileInputStream(tmpDir + "/" + theCellId + ".result");
                    InputStream buffer = new BufferedInputStream(file);
                    ObjectInputStream input = new ObjectInputStream(buffer);
                    ret = input.readObject();
                    theOutput.finished(ret);
                } catch (EOFException ex) {
                    logger.info("EOFException!");
                    theOutput.error("Failed to read serialized cell output");
                } catch (IOException ex) {
                    logger.info("IOException!");
                    theOutput.error("Failed to read serialized cell output");
                }
            } else {
                theOutput.error("Execution failed");
            }
        } else
            theOutput.finished(null);
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException)
            e = ((InvocationTargetException) e).getTargetException();
        if ((e instanceof InterruptedException) || (e instanceof ThreadDeath)) {
            theOutput.error("... cancelled!");
        } else {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            theOutput.error(sw.toString());
        }
    } finally {
        if (theOutput != null) {
            theOutput.executeCodeCallback();
        }
    }
    theOutput.clrOutputHandler();
}
Example 67
Project: XRobot-master  File: LeJOSEV3Util.java View source code
public static ProcessBuilder createProcessBuilder(List<String> args2) throws IOException {
    int len = args2.size();
    ArrayList<String> args3;
    if (len <= 1 || !isWindows()) {
        args3 = new ArrayList<String>(len);
        args3.addAll(args2);
    } else {
        args3 = new ArrayList<String>(2);
        Iterator<String> it = args2.iterator();
        args3.add(it.next());
        // Both java.lang.Runtime.exec(String[]) as well as in java.lang.ProcessBuilder
        // don't escape the arguments that are passed to the program. Also, they fail to
        // handle the empty string correctly. Hence, we manually escape all arguments.
        // If the second element of the command list starts and ends with a quote,
        // then ProcessBuilder won't add more quotes and the string will be passed
        // to the invoked program without any further processing (not documented, but
        // the implementation shows that this is the case).
        StringBuilder sb = new StringBuilder();
        while (it.hasNext()) {
            sb.append(' ');
            // the escaping must add quotes around the argument in order to
            // satisfy the requirements described above.
            escapeWindowsArg(it.next(), sb);
        }
        args3.add(sb.substring(1));
    }
    return new ProcessBuilder(args3);
}
Example 68
Project: HiTune-master  File: TorqueInfoProcessor.java View source code
private void getHodJobInfo() throws IOException {
    StringBuffer sb = new StringBuffer();
    sb.append(torqueBinDir).append("/qstat -a");
    String[] getQueueInfoCommand = new String[3];
    getQueueInfoCommand[0] = "ssh";
    getQueueInfoCommand[1] = torqueServer;
    getQueueInfoCommand[2] = sb.toString();
    String command = getQueueInfoCommand[0] + " " + getQueueInfoCommand[1] + " " + getQueueInfoCommand[2];
    ProcessBuilder pb = new ProcessBuilder(getQueueInfoCommand);
    Process p = pb.start();
    Timer timeout = new Timer();
    TorqueTimerTask torqueTimerTask = new TorqueTimerTask(p, command);
    timeout.schedule(torqueTimerTask, TorqueTimerTask.timeoutInterval * 1000);
    BufferedReader result = new BufferedReader(new InputStreamReader(p.getInputStream()));
    ErStreamHandler errorHandler = new ErStreamHandler(p.getErrorStream(), command, true);
    errorHandler.start();
    String line = null;
    boolean start = false;
    TreeSet<String> jobsInTorque = new TreeSet<String>();
    while ((line = result.readLine()) != null) {
        if (line.startsWith("---")) {
            start = true;
            continue;
        }
        if (start) {
            String[] items = line.split("\\s+");
            if (items.length >= 10) {
                String hodIdLong = items[0];
                String hodId = hodIdLong.split("[.]")[0];
                String userId = items[1];
                String numOfMachine = items[5];
                String status = items[9];
                jobsInTorque.add(hodId);
                if (!currentHodJobs.containsKey(hodId)) {
                    TreeMap<String, String> aJobData = new TreeMap<String, String>();
                    aJobData.put("userId", userId);
                    aJobData.put("numOfMachine", numOfMachine);
                    aJobData.put("traceCheckCount", "0");
                    aJobData.put("process", "0");
                    aJobData.put("status", status);
                    currentHodJobs.put(hodId, aJobData);
                } else {
                    TreeMap<String, String> aJobData = currentHodJobs.get(hodId);
                    aJobData.put("status", status);
                    currentHodJobs.put(hodId, aJobData);
                }
            // if..else
            }
        }
    }
    try {
        errorHandler.join();
    } catch (InterruptedException ie) {
        log.error(ie.getMessage());
    }
    timeout.cancel();
    Set<String> currentHodJobIds = currentHodJobs.keySet();
    Iterator<String> currentHodJobIdsIt = currentHodJobIds.iterator();
    TreeSet<String> finishedHodIds = new TreeSet<String>();
    while (currentHodJobIdsIt.hasNext()) {
        String hodId = currentHodJobIdsIt.next();
        if (!jobsInTorque.contains(hodId)) {
            TreeMap<String, String> aJobData = currentHodJobs.get(hodId);
            String process = aJobData.get("process");
            if (process.equals("0") || process.equals("1")) {
                aJobData.put("status", "C");
            } else {
                finishedHodIds.add(hodId);
            }
        }
    }
    // while
    Iterator<String> finishedHodIdsIt = finishedHodIds.iterator();
    while (finishedHodIdsIt.hasNext()) {
        String hodId = finishedHodIdsIt.next();
        currentHodJobs.remove(hodId);
    }
}
Example 69
Project: rootcloakplus-master  File: Main.java View source code
// Note to self: in the future, instead of doing any of this, change it to hook start(), and then have it check using command() if it is a bad arg
// Otherwise the app doign the detection could detect it!!!
static void initForProcessBuilder() {
    Class _class;
    Method method;
    Constructor constructor;
    try {
        _class = Class.forName("java.lang.ProcessBuilder");
        constructor = _class.getConstructor(List.class);
        MS.hookMethod(_class, constructor, new MethodAlteration<Object, Object>() {

            public Object invoked(Object _this, Object... args) throws Throwable {
                final String testName = Common.getProcessName();
                if (testName == null || !isBlacklisted(testName)) {
                    //Log.d(Common.TAG, "hookMethod, and testName: " + testName);
                    return invoke(_this, args);
                }
                Log.i(Common.TAG, "15 Blacklisted app: " + testName);
                List<String> execList = (List<String>) args[0];
                if ((execList != null) && (execList.size() >= 1)) {
                    String outputString = "";
                    for (String temp : execList) {
                        outputString += " " + temp;
                    }
                    Log.d(Common.TAG, "List: " + outputString);
                    ArrayList<String> finalArgs = new ArrayList<String>();
                    if (execList.get(0) == "ps") {
                        args[0] = buildGrepListSingle(execList, true);
                    } else if (execList.get(0) == "ls") {
                        args[0] = buildGrepListSingle(execList, true);
                    } else if (execList.get(0) == "pm") {
                        if (execList.size() >= 3 && execList.get(1).equalsIgnoreCase("list") && execList.get(2).equalsIgnoreCase("packages")) {
                            args[0] = buildGrepListSingle(execList, true);
                        } else if (execList.size() >= 3 && (execList.get(1).equalsIgnoreCase("dump") || execList.get(1).equalsIgnoreCase("path"))) {
                            // If getting dumping or getting the path, don't let it work if it contains any of the keywords
                            if (stringContainsFromCollection(execList.get(2), Common.DEFAULT_KEYWORD_ENTRIES)) {
                                List<String> tempList = new ArrayList<String>(Arrays.asList(new String[] { execList.get(0), execList.get(1), "FAKE_PACKAGE" }));
                                args[0] = tempList;
                            }
                        }
                    } else if (execList.get(0) == "sh") {
                        if (execList.size() >= 3 && execList.get(1) != null && execList.get(1).equalsIgnoreCase("-c")) {
                            if (execList.get(2) != null && (execList.get(2) == "ps" || execList.get(2) == "pm" || execList.get(2) == "")) {
                                args[0] = buildGrepListSingle(execList, false);
                            } else if (execList.get(2) != null && execList.get(2) == "su") {
                                List<String> tempList = new ArrayList<String>(Arrays.asList(new String[] { "sh", "-c", "suz" }));
                                args[0] = tempList;
                            } else {
                                args[0] = buildGrepListSingle(execList, false);
                            }
                        } else {
                            List<String> tempList = new ArrayList<String>(Arrays.asList(new String[] { "FAKECOMMAND", "FAKE" }));
                            args[0] = tempList;
                        }
                    }
                }
                execList = (List<String>) args[0];
                if ((execList != null) && (execList.size() >= 1)) {
                    String outputString = "";
                    for (String temp : execList) {
                        outputString += " " + temp;
                    }
                    Log.d(Common.TAG, "New List: " + outputString);
                }
                return invoke(_this, args);
            }
        });
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    // For ProcessBuilder command
    try {
        _class = Class.forName("java.lang.ProcessBuilder");
        method = _class.getMethod("command", List.class);
        MS.hookMethod(_class, method, new MethodAlteration<Object, Object>() {

            public Object invoked(Object _this, Object... args) throws Throwable {
                final String testName = Common.getProcessName();
                if (testName == null || !isBlacklisted(testName)) {
                    //Log.d(Common.TAG, "hookMethod, and testName: " + testName);
                    return invoke(_this, args);
                }
                Log.i(Common.TAG, "16 Blacklisted app: " + testName);
                List<String> execList = (List<String>) args[0];
                if ((execList != null) && (execList.size() >= 1)) {
                    ArrayList<String> finalArgs = new ArrayList<String>();
                    if (execList.get(0) == "ps") {
                        args[0] = buildGrepListSingle(execList, true);
                    } else if (execList.get(0) == "ls") {
                        args[0] = buildGrepListSingle(execList, true);
                    } else if (execList.get(0) == "pm") {
                        if (execList.size() >= 3 && execList.get(1).equalsIgnoreCase("list") && execList.get(2).equalsIgnoreCase("packages")) {
                            args[0] = buildGrepListSingle(execList, true);
                        } else if (execList.size() >= 3 && (execList.get(1).equalsIgnoreCase("dump") || execList.get(1).equalsIgnoreCase("path"))) {
                            // If getting dumping or getting the path, don't let it work if it contains any of the keywords
                            if (stringContainsFromCollection(execList.get(2), Common.DEFAULT_KEYWORD_ENTRIES)) {
                                List<String> tempList = new ArrayList<String>(Arrays.asList(new String[] { execList.get(0), execList.get(1), "FAKE_PACKAGE" }));
                                args[0] = tempList;
                            }
                        }
                    } else if (execList.get(0) == "sh") {
                        if (execList.size() >= 3 && execList.get(1) != null && execList.get(1).equalsIgnoreCase("-c")) {
                            if (execList.get(2) != null && (execList.get(2) == "ps" || execList.get(2) == "pm" || execList.get(2) == "")) {
                                args[0] = buildGrepListSingle(execList, false);
                            } else if (execList.get(2) != null && execList.get(2) == "su") {
                                List<String> tempList = new ArrayList<String>(Arrays.asList(new String[] { "sh", "-c", "suz" }));
                                args[0] = tempList;
                            } else {
                                args[0] = buildGrepListSingle(execList, false);
                            }
                        } else {
                            List<String> tempList = new ArrayList<String>(Arrays.asList(new String[] { "FAKECOMMAND", "FAKE" }));
                            args[0] = tempList;
                        }
                    }
                }
                return invoke(_this, args);
            }
        });
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}
Example 70
Project: nifi-master  File: ExecuteStreamCommand.java View source code
@Override
public void onTrigger(ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile inputFlowFile = session.get();
    if (null == inputFlowFile) {
        return;
    }
    final ArrayList<String> args = new ArrayList<>();
    final boolean putToAttribute = context.getProperty(PUT_OUTPUT_IN_ATTRIBUTE).isSet();
    final Integer attributeSize = context.getProperty(PUT_ATTRIBUTE_MAX_LENGTH).asInteger();
    final String attributeName = context.getProperty(PUT_OUTPUT_IN_ATTRIBUTE).getValue();
    final String executeCommand = context.getProperty(EXECUTION_COMMAND).evaluateAttributeExpressions(inputFlowFile).getValue();
    args.add(executeCommand);
    final String commandArguments = context.getProperty(EXECUTION_ARGUMENTS).evaluateAttributeExpressions(inputFlowFile).getValue();
    final boolean ignoreStdin = Boolean.parseBoolean(context.getProperty(IGNORE_STDIN).getValue());
    if (!StringUtils.isBlank(commandArguments)) {
        for (String arg : ArgumentUtils.splitArgs(commandArguments, context.getProperty(ARG_DELIMITER).getValue().charAt(0))) {
            args.add(arg);
        }
    }
    final String workingDir = context.getProperty(WORKING_DIR).evaluateAttributeExpressions(inputFlowFile).getValue();
    final ProcessBuilder builder = new ProcessBuilder();
    logger.debug("Executing and waiting for command {} with arguments {}", new Object[] { executeCommand, commandArguments });
    File dir = null;
    if (!StringUtils.isBlank(workingDir)) {
        dir = new File(workingDir);
        if (!dir.exists() && !dir.mkdirs()) {
            logger.warn("Failed to create working directory {}, using current working directory {}", new Object[] { workingDir, System.getProperty("user.dir") });
        }
    }
    final Map<String, String> environment = new HashMap<>();
    for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
        if (entry.getKey().isDynamic()) {
            environment.put(entry.getKey().getName(), entry.getValue());
        }
    }
    builder.environment().putAll(environment);
    builder.command(args);
    builder.directory(dir);
    builder.redirectInput(Redirect.PIPE);
    builder.redirectOutput(Redirect.PIPE);
    final Process process;
    try {
        process = builder.start();
    } catch (IOException e) {
        logger.error("Could not create external process to run command", e);
        throw new ProcessException(e);
    }
    try (final OutputStream pos = process.getOutputStream();
        final InputStream pis = process.getInputStream();
        final InputStream pes = process.getErrorStream();
        final BufferedInputStream bis = new BufferedInputStream(pis);
        final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pes))) {
        int exitCode = -1;
        final BufferedOutputStream bos = new BufferedOutputStream(pos);
        FlowFile outputFlowFile = putToAttribute ? inputFlowFile : session.create(inputFlowFile);
        ProcessStreamWriterCallback callback = new ProcessStreamWriterCallback(ignoreStdin, bos, bis, logger, attributeName, session, outputFlowFile, process, putToAttribute, attributeSize);
        session.read(inputFlowFile, callback);
        outputFlowFile = callback.outputFlowFile;
        if (putToAttribute) {
            outputFlowFile = session.putAttribute(outputFlowFile, attributeName, new String(callback.outputBuffer, 0, callback.size));
        }
        exitCode = callback.exitCode;
        logger.debug("Execution complete for command: {}.  Exited with code: {}", new Object[] { executeCommand, exitCode });
        Map<String, String> attributes = new HashMap<>();
        final StringBuilder strBldr = new StringBuilder();
        try {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                strBldr.append(line).append("\n");
            }
        } catch (IOException e) {
            strBldr.append("Unknown...could not read Process's Std Error");
        }
        int length = strBldr.length() > 4000 ? 4000 : strBldr.length();
        attributes.put("execution.error", strBldr.substring(0, length));
        final Relationship outputFlowFileRelationship = putToAttribute ? ORIGINAL_RELATIONSHIP : OUTPUT_STREAM_RELATIONSHIP;
        if (exitCode == 0) {
            logger.info("Transferring flow file {} to {}", new Object[] { outputFlowFile, outputFlowFileRelationship.getName() });
        } else {
            logger.error("Transferring flow file {} to {}. Executable command {} ended in an error: {}", new Object[] { outputFlowFile, outputFlowFileRelationship.getName(), executeCommand, strBldr.toString() });
        }
        attributes.put("execution.status", Integer.toString(exitCode));
        attributes.put("execution.command", executeCommand);
        attributes.put("execution.command.args", commandArguments);
        outputFlowFile = session.putAllAttributes(outputFlowFile, attributes);
        // This transfer will transfer the FlowFile that received the stream out put to it's destined relationship.
        // In the event the stream is put to the an attribute of the original, it will be transferred here.
        session.transfer(outputFlowFile, outputFlowFileRelationship);
        if (!putToAttribute) {
            logger.info("Transferring flow file {} to original", new Object[] { inputFlowFile });
            inputFlowFile = session.putAllAttributes(inputFlowFile, attributes);
            session.transfer(inputFlowFile, ORIGINAL_RELATIONSHIP);
        }
    } catch (final IOException ex) {
        logger.warn("Problem terminating Process {}", new Object[] { process }, ex);
    } finally {
        // last ditch effort to clean up that process.
        process.destroy();
    }
}
Example 71
Project: autopsy-master  File: PhotoRecCarverFileIngestModule.java View source code
/**
     * @inheritDoc
     */
@Override
public IngestModule.ProcessResult process(AbstractFile file) {
    // Skip everything except unallocated space files.
    if (file.getType() != TskData.TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS) {
        return IngestModule.ProcessResult.OK;
    }
    // Safely get a reference to the totalsForIngestJobs object
    IngestJobTotals totals = getTotalsForIngestJobs(jobId);
    Path tempFilePath = null;
    try {
        // Verify initialization succeeded.
        if (null == this.executableFile) {
            // NON-NLS
            logger.log(Level.SEVERE, "PhotoRec carver called after failed start up");
            return IngestModule.ProcessResult.ERROR;
        }
        // Check that we have roughly enough disk space left to complete the operation
        // Some network drives always return -1 for free disk space. 
        // In this case, expect enough space and move on.
        long freeDiskSpace = IngestServices.getInstance().getFreeDiskSpace();
        if ((freeDiskSpace != IngestMonitor.DISK_FREE_SPACE_UNKNOWN) && ((file.getSize() * 1.2) > freeDiskSpace)) {
            // NON-NLS
            logger.log(// NON-NLS
            Level.SEVERE, // NON-NLS
            "PhotoRec error processing {0} with {1} Not enough space on primary disk to save unallocated space.", // NON-NLS
            new Object[] { file.getName(), PhotoRecCarverIngestModuleFactory.getModuleName() });
            MessageNotifyUtil.Notify.error(NbBundle.getMessage(this.getClass(), "PhotoRecIngestModule.UnableToCarve", file.getName()), NbBundle.getMessage(this.getClass(), "PhotoRecIngestModule.NotEnoughDiskSpace"));
            return IngestModule.ProcessResult.ERROR;
        }
        if (this.context.fileIngestIsCancelled() == true) {
            // if it was cancelled by the user, result is OK
            // NON-NLS
            logger.log(Level.INFO, "PhotoRec cancelled by user");
            MessageNotifyUtil.Notify.info(PhotoRecCarverIngestModuleFactory.getModuleName(), NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.cancelledByUser"));
            return IngestModule.ProcessResult.OK;
        }
        // Write the file to disk.
        long writestart = System.currentTimeMillis();
        WorkingPaths paths = PhotoRecCarverFileIngestModule.pathsByJob.get(this.jobId);
        tempFilePath = Paths.get(paths.getTempDirPath().toString(), file.getName());
        ContentUtils.writeToFile(file, tempFilePath.toFile(), context::fileIngestIsCancelled);
        if (this.context.fileIngestIsCancelled() == true) {
            // if it was cancelled by the user, result is OK
            // NON-NLS
            logger.log(Level.INFO, "PhotoRec cancelled by user");
            MessageNotifyUtil.Notify.info(PhotoRecCarverIngestModuleFactory.getModuleName(), NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.cancelledByUser"));
            return IngestModule.ProcessResult.OK;
        }
        // Create a subdirectory for this file.
        Path outputDirPath = Paths.get(paths.getOutputDirPath().toString(), file.getName());
        Files.createDirectory(outputDirPath);
        //NON-NLS
        File log = new File(Paths.get(outputDirPath.toString(), LOG_FILE).toString());
        // Scan the file with Unallocated Carver.
        ProcessBuilder processAndSettings = new ProcessBuilder("\"" + executableFile + "\"", // NON-NLS
        "/d", "\"" + outputDirPath.toAbsolutePath() + File.separator + PHOTOREC_RESULTS_BASE + "\"", // NON-NLS
        "/cmd", "\"" + tempFilePath.toFile() + "\"", // NON-NLS
        "search");
        // Add environment variable to force PhotoRec to run with the same permissions Autopsy uses
        //NON-NLS
        processAndSettings.environment().put("__COMPAT_LAYER", "RunAsInvoker");
        processAndSettings.redirectErrorStream(true);
        processAndSettings.redirectOutput(Redirect.appendTo(log));
        FileIngestModuleProcessTerminator terminator = new FileIngestModuleProcessTerminator(this.context, true);
        int exitValue = ExecUtil.execute(processAndSettings, terminator);
        if (this.context.fileIngestIsCancelled() == true) {
            // if it was cancelled by the user, result is OK
            cleanup(outputDirPath, tempFilePath);
            // NON-NLS
            logger.log(Level.INFO, "PhotoRec cancelled by user");
            MessageNotifyUtil.Notify.info(PhotoRecCarverIngestModuleFactory.getModuleName(), NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.cancelledByUser"));
            return IngestModule.ProcessResult.OK;
        } else if (terminator.getTerminationCode() == ProcTerminationCode.TIME_OUT) {
            cleanup(outputDirPath, tempFilePath);
            // NON-NLS
            String msg = NbBundle.getMessage(this.getClass(), "PhotoRecIngestModule.processTerminated") + file.getName();
            // NON-NLS                
            MessageNotifyUtil.Notify.error(NbBundle.getMessage(this.getClass(), "PhotoRecIngestModule.moduleError"), msg);
            logger.log(Level.SEVERE, msg);
            return IngestModule.ProcessResult.ERROR;
        } else if (0 != exitValue) {
            // if it failed or was cancelled by timeout, result is ERROR
            cleanup(outputDirPath, tempFilePath);
            totals.totalItemsWithErrors.incrementAndGet();
            // NON-NLS
            logger.log(// NON-NLS
            Level.SEVERE, // NON-NLS
            "PhotoRec carver returned error exit value = {0} when scanning {1}", // NON-NLS
            new Object[] { exitValue, file.getName() });
            MessageNotifyUtil.Notify.error(PhotoRecCarverIngestModuleFactory.getModuleName(), // NON-NLS
            NbBundle.getMessage(// NON-NLS
            PhotoRecCarverFileIngestModule.class, // NON-NLS
            "PhotoRecIngestModule.error.exitValue", new Object[] { exitValue, file.getName() }));
            return IngestModule.ProcessResult.ERROR;
        }
        // Move carver log file to avoid placement into Autopsy results. PhotoRec appends ".1" to the folder name.
        //NON-NLS
        java.io.File oldAuditFile = new java.io.File(Paths.get(outputDirPath.toString(), PHOTOREC_RESULTS_EXTENDED, PHOTOREC_REPORT).toString());
        //NON-NLS
        java.io.File newAuditFile = new java.io.File(Paths.get(outputDirPath.toString(), PHOTOREC_REPORT).toString());
        oldAuditFile.renameTo(newAuditFile);
        if (this.context.fileIngestIsCancelled() == true) {
            // if it was cancelled by the user, result is OK
            // NON-NLS
            logger.log(Level.INFO, "PhotoRec cancelled by user");
            MessageNotifyUtil.Notify.info(PhotoRecCarverIngestModuleFactory.getModuleName(), NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.cancelledByUser"));
            return IngestModule.ProcessResult.OK;
        }
        Path pathToRemove = Paths.get(outputDirPath.toAbsolutePath().toString());
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(pathToRemove)) {
            for (Path entry : stream) {
                if (Files.isDirectory(entry)) {
                    FileUtil.deleteDir(new File(entry.toString()));
                }
            }
        }
        long writedelta = (System.currentTimeMillis() - writestart);
        totals.totalWritetime.addAndGet(writedelta);
        // Now that we've cleaned up the folders and data files, parse the xml output file to add carved items into the database
        long calcstart = System.currentTimeMillis();
        PhotoRecCarverOutputParser parser = new PhotoRecCarverOutputParser(outputDirPath);
        if (this.context.fileIngestIsCancelled() == true) {
            // if it was cancelled by the user, result is OK
            // NON-NLS
            logger.log(Level.INFO, "PhotoRec cancelled by user");
            MessageNotifyUtil.Notify.info(PhotoRecCarverIngestModuleFactory.getModuleName(), NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.cancelledByUser"));
            return IngestModule.ProcessResult.OK;
        }
        List<LayoutFile> carvedItems = parser.parse(newAuditFile, file, context);
        long calcdelta = (System.currentTimeMillis() - calcstart);
        totals.totalParsetime.addAndGet(calcdelta);
        if (carvedItems != null && !carvedItems.isEmpty()) {
            // if there were any results from carving, add the unallocated carving event to the reports list.
            totals.totalItemsRecovered.addAndGet(carvedItems.size());
            context.addFilesToJob(new ArrayList<>(carvedItems));
            // fire an event to update the tree
            services.fireModuleContentEvent(new ModuleContentEvent(carvedItems.get(0)));
        }
    } catch (IOException ex) {
        totals.totalItemsWithErrors.incrementAndGet();
        logger.log(Level.SEVERE, "Error processing " + file.getName() + " with PhotoRec carver", ex);
        MessageNotifyUtil.Notify.error(PhotoRecCarverIngestModuleFactory.getModuleName(), NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.error.msg", file.getName()));
        return IngestModule.ProcessResult.ERROR;
    } finally {
        if (null != tempFilePath && Files.exists(tempFilePath)) {
            // Get rid of the unallocated space file.
            tempFilePath.toFile().delete();
        }
    }
    return IngestModule.ProcessResult.OK;
}
Example 72
Project: jcloudscale-master  File: LocalVM.java View source code
protected void launchHost(String size) {
    this.instanceSize = size;
    if (!(this.config instanceof LocalCloudPlatformConfiguration))
        throw new JCloudScaleException("Failed to launch local virtual VM: preconfigured configuration is of the wrong type:" + (this.config == null ? "NULL" : this.config.getClass().getName()) + "instead of LocalCloudPlatformConfiguration");
    LocalCloudPlatformConfiguration config = (LocalCloudPlatformConfiguration) this.config;
    File workingDir = new File(config.getStartupDirectory());
    try {
        //For even higher platform independency, we can use ant to start new jvm.
        String javaPath = config.getJavaPath();
        ProcessBuilder pb = new ProcessBuilder(/*JAVA executable*/
        javaPath, //should go as separate args.
        CLASSPATH_COMMAND, //should go as separate args.
        config.getClasspath(), /*Server startup class*/
        config.getServerStartupClass());
        // adding memory limit parameter
        if (config.getJavaHeapSizeMB() > 0)
            pb.command().add(1, HEAP_SIZE_COMMAND + config.getJavaHeapSizeMB() + "m");
        // adding server mode parameter
        if (config.isServerMode())
            pb.command().add(1, SERVER_MODE_COMMAND);
        // add custom JVM parameters as defined by the user
        for (String arg : config.getCustomJVMArgs()) {
            pb.command().add(1, arg);
        }
        pb.directory(workingDir);
        // redirecting output to the same destination as of our process
        //(so that server will write output to client's console/error stream.)
        pb.redirectOutput(Redirect.INHERIT);
        pb.redirectError(Redirect.INHERIT);
        jvmProcess = pb.start();
        try {
            Thread.sleep(400);
            int exitCode = jvmProcess.exitValue();
            String message = "JVM failed to launch. Exit code " + exitCode;
            throw new ScalingException(message);
        } catch (IllegalThreadStateExceptionInterruptedException |  e1) {
        }
    } catch (IOException e) {
        throw new ScalingException("Could not start local VM. Error message was: " + e.getMessage());
    }
}
Example 73
Project: ceylon-compiler-master  File: MiscTests.java View source code
public void launchCeylon(String[] args) throws IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder(args);
    pb.redirectInput(Redirect.INHERIT);
    pb.redirectOutput(Redirect.INHERIT);
    pb.redirectError(Redirect.INHERIT);
    Process p = pb.start();
    p.waitFor();
    if (p.exitValue() > 0) {
        Assert.fail("Ceylon script execution failed");
    }
}
Example 74
Project: arduinoConnect-master  File: ArduinoConnect.java View source code
/**
   * getSerialPortName Returns the serial port name where the arduino with the serial id specified by
   *   the arduino_serial_id parameter is connected. If the arduino is not it returns null.
   * @param arduino_serial_id Holds the unique serial id of the arduino that we want to connect to
   * @return Name of port to which the arduino identified on the hash map is connected
   */
public static String getSerialPortName(String arduino_serial_id) {
    //assuming OSX
    String getUsbArgs[] = new String[2];
    getUsbArgs[0] = "system_profiler";
    getUsbArgs[1] = "SPUSBDataType";
    Matcher idMatch;
    try {
        Process process = new ProcessBuilder(getUsbArgs).start();
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        String currSerial = null;
        String currLocation = null;
        boolean foundArduino = false;
        int serialNumPosition, locationPosition;
        while ((line = br.readLine()) != null) {
            serialNumPosition = line.indexOf(SERIAL_NUMBER);
            if (serialNumPosition >= 0) {
                currSerial = line.substring(serialNumPosition + SERIAL_NUMBER.length());
                if (!currSerial.equals(arduino_serial_id)) {
                    currSerial = null;
                }
            } else if (currSerial != null && (line.indexOf("Arduino") >= 0 || line.indexOf("FT232R") >= 0 || line.indexOf("Vendor ID: 0x20a0") >= 0)) {
                //Vendor ID: 0x20a0 is freetronics
                foundArduino = true;
            } else if (foundArduino && currSerial != null) {
                locationPosition = line.indexOf(LOCATION);
                if (locationPosition >= 0) {
                    currLocation = line.substring(locationPosition + LOCATION.length());
                    idMatch = REGEX_PAT.matcher(currLocation);
                    if (idMatch.groupCount() > 0 && idMatch.find()) {
                        return "/dev/tty.usbmodem" + idMatch.group(1) + "1";
                    }
                    currSerial = null;
                    currLocation = null;
                    foundArduino = false;
                }
            }
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    return null;
}
Example 75
Project: eclipselink.runtime-master  File: DefaultSemanticValidatorTest2_0.java View source code
@Test
public final void test_ConstructorExpression_UndefinedConstructor_3() throws Exception {
    String jpqlQuery = "SELECT NEW java.lang.ProcessBuilder(e.name, e.name, e.name) FROM Employee e";
    int startPosition = "SELECT NEW ".length();
    int endPosition = "SELECT NEW java.lang.ProcessBuilder".length();
    List<JPQLQueryProblem> problems = validate(jpqlQuery);
    // Note: Unless there is a way to discover vararg, we'll assume it's not possible to match
    testHasProblem(problems, ConstructorExpression_UndefinedConstructor, startPosition, endPosition);
}
Example 76
Project: rootcloak-master  File: RootCloak.java View source code
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
    XposedBridge.log("Hooked ProcessBuilder");
    if (param.args[0] != null) {
        String[] cmdArray = (String[]) param.args[0];
        if (debugPref) {
            String tempString = "ProcessBuilder Command:";
            for (String temp : cmdArray) {
                tempString = tempString + " " + temp;
            }
            XposedBridge.log(tempString);
        }
        if (stringEndsWithFromSet(cmdArray[0], commandSet)) {
            cmdArray[0] = FAKE_COMMAND;
            param.args[0] = cmdArray;
        }
        if (debugPref) {
            String tempString = "New ProcessBuilder Command:";
            for (String temp : (String[]) param.args[0]) {
                tempString = tempString + " " + temp;
            }
            XposedBridge.log(tempString);
        }
    }
}
Example 77
Project: robovm-master  File: AppLauncher.java View source code
private File getXcodePath() throws Exception {
    if (xcodePath != null) {
        return new File(xcodePath);
    }
    File tmpFile = File.createTempFile(this.getClass().getSimpleName(), ".tmp");
    try {
        int ret = new ProcessBuilder("xcode-select", "-print-path").redirectErrorStream(true).redirectOutput(Redirect.to(tmpFile)).start().waitFor();
        if (ret != 0) {
            throw new IOException("xcode-select failed with error code: " + ret);
        }
        return new File(new String(Files.readAllBytes(tmpFile.toPath()), "UTF-8").trim());
    } finally {
        tmpFile.delete();
    }
}
Example 78
Project: sharenav-master  File: BundleShareNav.java View source code
private static void pack(Configuration c) throws ZipException, IOException {
    File n = null;
    if (config.getMapName().equals("") || !config.mapzip) {
        n = new File(c.getBundleFileName() + (config.sourceIsApk ? ".apk" : ".jar"));
        rewriteManifestFile(c, true);
    } else {
        n = new File(c.getMapFileName());
        rewriteManifestFile(c, false);
        renameCopying(c);
    }
    BufferedOutputStream fo = new BufferedOutputStream(new FileOutputStream(n));
    ZipOutputStream zf = new ZipOutputStream(fo);
    zf.setLevel(9);
    if (compressed == false) {
        zf.setMethod(ZipOutputStream.STORED);
    }
    File src = new File(c.getTempDir());
    if (src.isDirectory() == false) {
        throw new Error("TempDir is not a directory");
    }
    packDir(zf, src, "");
    String bundleName = n.getAbsolutePath();
    String jarSigner = config.getJarsignerPath();
    // resolve Windows environment variables case-insensitive
    java.util.Map<String, String> env = System.getenv();
    for (String envName : env.keySet()) {
        jarSigner = jarSigner.replaceAll("(?i)%" + envName + "%", Matcher.quoteReplacement(env.get(envName)));
    }
    zf.close();
    if (config.sourceIsApk && config.signApk && !config.mapzip) {
        Process signer = null;
        // FIXME add "-storepass" handling with a password field on GUI
        String command[] = { jarSigner, "-verbose", "-verbose", "-verbose", "-digestalg", "SHA1", "-sigalg", "MD5withRSA", bundleName, "sharenav" };
        String passString = config.getSignApkPassword();
        if (!passString.equals("")) {
            command[2] = "-storepass";
            command[3] = passString;
        }
        try {
            String jarsignerOutputLine = null;
            System.out.println("Signing with external program " + command[0] + " (set jarsignerPath=<jarsigner-path-or-commandname> in .properties to change)");
            ProcessBuilder pBuilder = new ProcessBuilder(command);
            pBuilder.redirectErrorStream(true);
            signer = pBuilder.start();
            //signer = Runtime.getRuntime().exec(command);
            // Runtime.getRuntime().exec(jarSigner + " -verbose -digestalg SHA1 -sigalg MD5withRSA " + bundleName + " sharenav");
            //DataInputStream jarsignerOutputDataStream = new InputStream(signer.getInputStream());
            BufferedReader jarsignerOutput = new BufferedReader(new InputStreamReader(signer.getInputStream()));
            // if jarsigner asks for a password, this makes it stop
            // asking and show the query/error output
            signer.getOutputStream().flush();
            signer.getOutputStream().close();
            while ((jarsignerOutputLine = jarsignerOutput.readLine()) != null) {
                System.out.println(jarsignerOutputLine);
            }
        } catch (IOException ioe) {
            System.out.println("Error: IO exception " + ioe);
            showSigningMessage(bundleName);
        }
        if (signer != null) {
            try {
                signer.waitFor();
                int exitStatus = signer.exitValue();
                if (exitStatus != 0) {
                    System.out.println("ERROR: jarsigner exited with exit status " + exitStatus + ", signing failed");
                    showSigningMessage(bundleName);
                }
            } catch (InterruptedException ie) {
                System.out.println("Error: interrupted execution " + ie);
                showSigningMessage(bundleName);
            }
        }
    }
    if (config.getMapName().equals("") && !config.sourceIsApk) {
        writeJADfile(c, n.length());
    }
    Calendar endTime = Calendar.getInstance();
    if (config.verbose >= 0) {
        System.out.println(n.getName() + " created successfully with " + (n.length() / 1024 / 1024) + " MiB in " + getDuration(endTime.getTimeInMillis() - startTime.getTimeInMillis()));
    }
}
Example 79
Project: protobuf-el-master  File: ExecMojo.java View source code
private int createProcess(final List<String> command, final Redirect errorRedirect, final Redirect outRedirect, final File directory, final Map<String, String> environment, final boolean redirectErrorStream) {
    final ProcessBuilder processBuilder = new ProcessBuilder(command).redirectErrorStream(redirectErrorStream).redirectError(errorRedirect).redirectOutput(outRedirect).directory(directory);
    if ((environment != null) && !environment.isEmpty()) {
        processBuilder.environment().putAll(environment);
    }
    Process process;
    try {
        process = processBuilder.start();
        return process.waitFor();
    } catch (final IOException e) {
        getLog().error(e);
    } catch (final InterruptedException e) {
        getLog().error(e);
    }
    return -1;
}
Example 80
Project: spring-data-gemfire-master  File: ProcessConfiguration.java View source code
public static ProcessConfiguration create(ProcessBuilder processBuilder) {
    Assert.notNull(processBuilder, "The ProcessBuilder used to configure and start the Process must not be null");
    return new ProcessConfiguration(processBuilder.command(), processBuilder.directory(), processBuilder.environment(), processBuilder.redirectErrorStream());
}
Example 81
Project: emacs-android-app-master  File: PathReceiver.java View source code
private void chmod(String... args) throws IOException {
    String[] cmdline = new String[args.length + 1];
    cmdline[0] = "/system/bin/chmod";
    System.arraycopy(args, 0, cmdline, 1, args.length);
    new ProcessBuilder(cmdline).start();
}
Example 82
Project: spawn-master  File: Subprocesses.java View source code
public SubprocessBuilder redirectStdout(final ProcessBuilder.Redirect redirect) {
    processBuilder.redirectInput(redirect);
    return this;
}
Example 83
Project: IBAdataAnalysis-master  File: SRIM.java View source code
public void runSRModule() {
    //path SRModule
    setPaths();
    try {
        ProcessBuilder pb = new ProcessBuilder();
        pb.directory(new File(IJapp));
        pb.command(path_SRModule);
        pb.start();
    } catch (Exception e) {
        IJ.error(e.toString());
    }
}