/** * Executes a program. This is a convenience method mainly to be able to catch * the output of programs as shell scripts can do by wrapping calls in $(...). * * @param workingDirectory the directory in which to execute the program * @param err the {@link PrintStream} to print the program's error stream to; * if null is passed, the error goes straight to Nirvana (not the * band, though). * @param out the {@link PrintStream} to print the program's output to; if * null is passed, the output will be accumulated into a * {@link String} and returned upon exit. * @param args the command-line to execute, split into components * @return the output of the program if {@code out} is null, otherwise an * empty {@link String} * @throws RuntimeException if interrupted or the program failed to execute * successfully. */ public static String exec(final File workingDirectory, final PrintStream err, final PrintStream out, final String... args) { return exec(workingDirectory, null, err, out, args); }
/** * Executes a program. This is a convenience method mainly to be able to catch * the output of programs as shell scripts can do by wrapping calls in $(...). * * @param workingDirectory the directory in which to execute the program * @param err the {@link PrintStream} to print the program's error stream to; * if null is passed, the error goes straight to Nirvana (not the * band, though). * @param out the {@link PrintStream} to print the program's output to; if * null is passed, the output will be accumulated into a * {@link String} and returned upon exit. * @param args the command-line to execute, split into components * @return the output of the program if {@code out} is null, otherwise an * empty {@link String} * @throws RuntimeException if interrupted or the program failed to execute * successfully. */ public static String exec(final File workingDirectory, final PrintStream err, final PrintStream out, final String... args) { return exec(workingDirectory, null, err, out, args); }
@Override public void run() { try { ProcessUtils.exec(null, null, null, "sleep", "" + seconds); } catch (Throwable t) { result = t; } }
/** * Make a hex-dump. * * This calls the {@code hex-dump} program. * * @param inputFile * the file to hex-dump * @param outputFile * the output file (can be the same as the {@code inputFile} * @throws FileNotFoundException * @throws IOException */ protected void hexdump(File inputFile, File outputFile) throws FileNotFoundException, IOException { final String result = ProcessUtils.exec(null, out, null, "hexdump", "-C", inputFile.getAbsolutePath()); copy(new ByteArrayInputStream(result.getBytes()), new FileOutputStream(outputFile), true, true); }
@Override public void run() { try { ProcessUtils.exec(null, diff.getPrintStream(), diff.getPrintStream(), "git", "show"); } catch (RuntimeException e) { if (!(e.getCause() instanceof InterruptedException)) e.printStackTrace(); } } };
protected void execute(final String[] args, final File dir, final boolean verbose) throws IOException { if (verbose) { String output = "Executing:"; for (int i = 0; i < args.length; i++) output += " '" + args[i] + "'"; err.println(output); } /* stupid, stupid Windows... */ if (System.getProperty("os.name").startsWith("Windows")) { for (int i = 0; i < args.length; i++) args[i] = quoteArg(args[i]); // stupid, stupid, stupid Windows taking all my time!!! if (args[0].startsWith("../")) { args[0] = new File(dir, args[0]).getAbsolutePath(); } } ProcessUtils.exec(dir, err, out, args); }
/** * Run {@code javap} on a class file. * * @param jarFile * the <i>.jar</i> file containing the class * @param className * the name of the class * @return the output file * @throws IOException */ protected File javap(final File jarFile, final String className) throws IOException { File file = File.createTempFile("javap-", ""); try { final String result = ProcessUtils.exec(null, out, null, "javap", "-classpath", jarFile.getAbsolutePath(), "-c", className); copy(new ByteArrayInputStream(result.getBytes()), new FileOutputStream(file), true, true); } catch (RuntimeException e) { if (e.getCause() != null && e.getCause() instanceof InterruptedException) throw e; e.printStackTrace(); out.println("class " + className + " misses dependencies"); } return file; }
/** * Run {@code javap} on a class file. * * @param file * the <i>.class</i> file (and the output file) * @throws IOException */ protected void javap(final File file) throws IOException { final byte[] buffer = UpdaterUtil.readStreamAsBytes(new FileInputStream(file)); final ByteCodeAnalyzer analyzer = new ByteCodeAnalyzer(buffer, ByteCodeAnalyzer.Mode.CONSTANTS); final String path = analyzer.getPathForClass(); file.delete(); File classFile = new File(file, path); classFile.getParentFile().mkdirs(); copy(new ByteArrayInputStream(buffer), new FileOutputStream(classFile), true, true); final String result = ProcessUtils.exec(null, out, null, "javap", "-classpath", file.getAbsolutePath(), "-c", path.replace('/', '.')); while (!classFile.equals(file)) { classFile.delete(); classFile = classFile.getParentFile(); } copy(new ByteArrayInputStream(result.getBytes()), new FileOutputStream(file), true, true); }
final String suffix = path.substring(0, path.lastIndexOf('/') + 1) + sourceFile; try { path = ProcessUtils.exec(gitWorkingDirectory, null, null, "git", "ls-files", "*/" + suffix); if (path.length() <= suffix.length()) continue; if (path.endsWith("\n")) path = path.substring(0, path.length() - 1);
ProcessUtils.exec(null, ijLog, ijLog, System.getProperty("ij.executable"), "--jar", System.getProperty("ij.dir") + "/retro/retrotranslator-transformer-1.2.9.jar",
ProcessUtils.exec(null, out, out, "git", "diff", "--color=" + (color ? "always" : "auto"), "--no-index",
@Test public void testStdin() { assumePOSIX(); final String value = "Hello, World!\n"; final InputStream input = new ByteArrayInputStream(value.getBytes()); final String result = ProcessUtils.exec(null, input, null, null, "cat"); assertEquals(value, result); }