List of usage examples for java.lang Process getInputStream
public abstract InputStream getInputStream();
From source file:dk.netarkivet.common.utils.ProcessUtils.java
/** Runs an external process that takes no input, discarding its output. * * @param environment An environment to run the process in (may be null) * @param programAndArgs The program and its arguments. * @return The return code of the process. *//* w w w .ja v a2s .com*/ public static int runProcess(String[] environment, String... programAndArgs) { try { log.debug("Running external program: " + StringUtils.conjoin(" ", programAndArgs) + " with environment " + StringUtils.conjoin(" ", environment)); Process p = Runtime.getRuntime().exec(programAndArgs, environment); discardProcessOutput(p.getInputStream()); discardProcessOutput(p.getErrorStream()); while (true) { try { return p.waitFor(); } catch (InterruptedException e) { // Ignoring interruptions, we just want to try waiting // again. } } } catch (IOException e) { throw new IOFailure("Failure while running " + Arrays.toString(programAndArgs), e); } }
From source file:ezbake.amino.cli.Main.java
/** * A funny hack in order to get the proper terminal width so that we can word wrap properly * @return The number of characters wide the terminal is currently. *//*from w w w .j a v a 2s . c o m*/ public static int getTerminalWidth() { try { Process p = Runtime.getRuntime().exec(new String[] { "sh", "-c", "tput cols 2> /dev/tty" }); p.waitFor(); return Integer.parseInt(new BufferedReader(new InputStreamReader(p.getInputStream())).readLine()); } catch (Exception e) { logger.error("Error getting terminal width", e); return 80; } }
From source file:Main.java
public static int getSuVersionCode() { Process process = null; String inLine = null;//from www . ja v a2s.c om try { process = Runtime.getRuntime().exec("sh"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); BufferedReader is = new BufferedReader( new InputStreamReader(new DataInputStream(process.getInputStream())), 64); os.writeBytes("su -v\n"); // We have to hold up the thread to make sure that we're ready to read // the stream, using increments of 5ms makes it return as quick as // possible, and limiting to 1000ms makes sure that it doesn't hang for // too long if there's a problem. for (int i = 0; i < 400; i++) { if (is.ready()) { break; } try { Thread.sleep(5); } catch (InterruptedException e) { Log.w(TAG, "Sleep timer got interrupted..."); } } if (is.ready()) { inLine = is.readLine(); if (inLine != null && Integer.parseInt(inLine.substring(0, 1)) > 2) { inLine = null; os.writeBytes("su -V\n"); inLine = is.readLine(); if (inLine != null) { return Integer.parseInt(inLine); } } else { return 0; } } else { os.writeBytes("exit\n"); } } catch (IOException e) { Log.e(TAG, "Problems reading current version.", e); return 0; } finally { if (process != null) { process.destroy(); } } return 0; }
From source file:net.pms.util.ProcessUtil.java
public static boolean kill(Integer pid, int signal) { boolean killed = false; LOGGER.warn("Sending kill -" + signal + " to the Unix process: " + pid); try {//from w w w .j av a 2s. co m ProcessBuilder processBuilder = new ProcessBuilder("kill", "-" + signal, Integer.toString(pid)); processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); // consume the error and output process streams StreamGobbler.consume(process.getInputStream(), true); int exit = waitFor(process); if (exit == 0) { killed = true; LOGGER.debug("Successfully sent kill -" + signal + " to the Unix process: " + pid); } } catch (IOException e) { LOGGER.error("Error calling: kill -" + signal + " " + pid, e); } return killed; }
From source file:de.knowwe.visualization.util.Utils.java
private static boolean isFileClosedUnix(File file) { try {// w w w .j a v a 2 s .c o m Process plsof = new ProcessBuilder(new String[] { "lsof", "|", "grep", file.getAbsolutePath() }) .start(); BufferedReader reader = new BufferedReader(new InputStreamReader(plsof.getInputStream())); String line; while ((line = reader.readLine()) != null) { if (line.contains(file.getAbsolutePath())) { reader.close(); plsof.destroy(); return false; } } reader.close(); plsof.destroy(); } catch (Exception ignore) { } return true; }
From source file:energy.usef.environment.tool.security.VaultService.java
/** * Executes a class's static main method with the current java executable and classpath in a separate process. * /*from w ww .ja va2s.co m*/ * @param klass the class to call the static main method for * @param params the parameters to provide * @return the exit code of the process * @throws IOException * @throws InterruptedException */ public static int exec(@SuppressWarnings("rawtypes") Class klass, List<String> params) throws IOException, InterruptedException { String javaHome = System.getProperty("java.home"); String javaBin = javaHome + File.separator + "bin" + File.separator + "java"; String classpath = System.getProperty("java.class.path"); String className = klass.getCanonicalName(); // construct the command line List<String> command = new ArrayList<String>(); command.add(javaBin); command.add("-cp"); command.add(classpath); command.add(className); command.addAll(params); LOGGER.debug("executing class '{}' with params '{}' in classpath '{}' with java binary '{}'", className, params.toString(), classpath, javaBin); // build and start the Vault's process ProcessBuilder builder = new ProcessBuilder(command); Process process = builder.start(); process.waitFor(); // get the input and error streams of the process and log them InputStream in = process.getInputStream(); InputStream en = process.getErrorStream(); InputStreamReader is = new InputStreamReader(in); InputStreamReader es = new InputStreamReader(en); BufferedReader br = new BufferedReader(is); BufferedReader be = new BufferedReader(es); String read = br.readLine(); while (read != null) { LOGGER.debug(read); read = br.readLine(); } read = be.readLine(); while (read != null) { LOGGER.debug(read); read = be.readLine(); } br.close(); is.close(); in.close(); return process.exitValue(); }
From source file:deincraftlauncher.start.StartMinecraft.java
public static void startMC(Modpack pack) { try {/* w ww.j a v a 2 s. com*/ System.out.println("trying to start minecraft"); GameInfos infos = new GameInfos(pack.getName(), new File(pack.getPath()), new GameVersion(pack.getMCVersion(), pack.getGameType()), new GameTweak[] { GameTweak.FORGE }); System.out.println("GameInfos done"); System.out.println("Zugangsdaten: " + settings.getUsername() + settings.getPassword()); Authenticator authenticator = new Authenticator(Authenticator.MOJANG_AUTH_URL, AuthPoints.NORMAL_AUTH_POINTS); AuthResponse rep = authenticator.authenticate(AuthAgent.MINECRAFT, settings.getUsername(), settings.getPassword(), ""); AuthInfos authInfos = new AuthInfos(rep.getSelectedProfile().getName(), rep.getAccessToken(), rep.getSelectedProfile().getId()); System.out.println("authinfos done"); //AuthInfos authInfos = new AuthInfos(settings.getUsername(), MCAuthentication.getToken(settings.getUsername(), settings.getPassword()), MCAuthentication.getUUID(settings.getUsername(), settings.getPassword())); ExternalLaunchProfile profile = MinecraftLauncher.createExternalProfile(infos, getFolder(pack), authInfos); List<String> vmArgs = profile.getVmArgs(); vmArgs.add(String.valueOf("-Xms" + settings.getRAM() + "m")); //System.out.println("vm args: " + vmArgs); profile.setVmArgs(vmArgs); ExternalLauncher launcher = new MCLauncher(profile); System.out.println("profile and launcher done " + launcher.getProfile()); Process launch = launcher.launch(); BufferedReader stdout = new BufferedReader(new InputStreamReader(launch.getInputStream())); String line; while ((line = stdout.readLine()) != null) { System.out.println(line); } Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Platform.runLater(() -> { System.out.println("minecraft started, changing start state"); pack.setStartState(PackViewHandler.StartState.Loading); pack.setStartText("gestartet"); }); } }, 8000); checkAlive(launch, pack); } catch (LaunchException | AuthenticationException | IOException ex) { Logger.getLogger(StartMinecraft.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.searchbox.framework.web.SystemController.java
/** * Utility function to execute a function *//*from w w w . j a v a 2 s. c o m*/ private static String execute(String cmd) { DataInputStream in = null; Process process = null; try { process = Runtime.getRuntime().exec(cmd); in = new DataInputStream(process.getInputStream()); // use default charset from locale here, because the command invoked // also uses the default locale: return IOUtils.toString(new InputStreamReader(in, Charset.defaultCharset())); } catch (Exception ex) { // ignore - log.warn("Error executing command", ex); return "(error executing: " + cmd + ")"; } finally { if (process != null) { IOUtils.closeQuietly(process.getOutputStream()); IOUtils.closeQuietly(process.getInputStream()); IOUtils.closeQuietly(process.getErrorStream()); } } }
From source file:com.unresyst.DealRecommender.java
private static String runCommand(String... commands) throws IOException, InterruptedException { // generate a script file containg the command to run final File scriptFile = new File("/tmp/runcommand.sh"); PrintWriter w = new PrintWriter(scriptFile); w.println("#!/bin/sh"); for (String command : commands) { w.println(command);//from www .j a va 2 s . c o m } w.close(); // make the script executable //System.out.println("absolute path: " + scriptFile.getAbsolutePath()); Process p = Runtime.getRuntime().exec("chmod +x " + scriptFile.getAbsolutePath()); p.waitFor(); // execute the script p = Runtime.getRuntime().exec(scriptFile.getAbsolutePath()); p.waitFor(); BufferedReader stdin = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stderr = new BufferedReader(new InputStreamReader(p.getErrorStream())); String toReturn = ""; String line = ""; while ((line = stdin.readLine()) != null) { toReturn += line + "\n"; } while ((line = stderr.readLine()) != null) { toReturn += "err: " + line + "\n"; } scriptFile.delete(); return toReturn; }
From source file:jp.co.tis.gsp.tools.dba.util.ProcessUtil.java
public static void exec(Map<String, String> environment, String... args) throws IOException, InterruptedException { Process process = null; InputStream stdout = null;//from w ww. j a v a 2 s . c om BufferedReader br = null; try { ProcessBuilder pb = new ProcessBuilder(args); pb.redirectErrorStream(true); if (environment != null) { pb.environment().putAll(environment); } process = pb.start(); stdout = process.getInputStream(); br = new BufferedReader(new InputStreamReader(stdout)); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { throw e; } finally { IOUtils.closeQuietly(br); IOUtils.closeQuietly(stdout); if (process != null) { process.destroy(); } } }