processbuilder

How to execute batch script which takes multiple inputs by using Java Process Builder?

☆樱花仙子☆ 提交于 2019-12-05 17:25:59
I have a batch script which takes input as user name and age and prints both the inputs. I want to write a java program to execute that script and pass the inputs. I have written the java program using ProcessBuilder. I am also passing the username and age in the process OutputStream but only user name is getting printed and age is missing. my script(test.bat file) : @echo off echo executing test.bat set /p name=Enter Name: set /p age=Enter Age : echo Hi %name%, you are %age% years old. my java program : private static void executeInteractiveCommand(String cmd,String ... args){ try{ List

Java run async processes

。_饼干妹妹 提交于 2019-12-05 10:54:27
I am trying to run an async process and I do not want the program to wait until the end of these processes executions. I found this question how to run shell script asynchronously from within Java program but it doesn't have the answer that I am looking for. What I am doing is I am simply running bash processes and after I run it, I do not want the Java program to wait until it's finished. This is what I have done: public void runCommandLine(String directory) throws IOException { Thread commandLineThread = new Thread(() -> { try { ProcessBuilder processBuilder = new ProcessBuilder( "/bin/bash"

ProcessBuilder redirected to standard output

 ̄綄美尐妖づ 提交于 2019-12-05 09:53:34
问题 I would like to redirect a java process output towards the standard output of the parent java process. Using the ProcessBuilder class as follows: public static void main(String[] args) { ProcessBuilder processBuilder = new ProcessBuilder("cmd"); processBuilder.directory(new File("C:")); processBuilder.redirectErrorStream(true); // redirect error stream to output stream processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT); } I would have expected that the outputs of "cmd", which are

java.io.IOException: error=11

青春壹個敷衍的年華 提交于 2019-12-05 04:52:13
I am experiencing a weird problem with the Java ProcessBuilder . The code is shown below (in a slightly simplified form) public class Whatever implements Runnable { public void run(){ //someIdentifier is a randomly generated string String in = someIdentifier + "input.txt"; String out = someIdentifier + "output.txt"; ProcessBuilder builder = new ProcessBuilder("./whateveer.sh", in, out); try { Process process = builder.start(); process.waitFor(); } catch (IOException e) { log.error("Could not launch process. Command: " + builder.command(), e); } catch (InterruptedException ex) { log.error(ex);

How to run NPM Command in Java using Process Builder

梦想的初衷 提交于 2019-12-04 15:13:02
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.*; public class TestUnZip { public static void main(String[] args) throws IOException, InterruptedException{ String destFolder="E:\\TestScript"; /* * Location where the Nodejs Project is Present */ System.out.println(destFolder); String cmdPrompt="cmd"; String path="/c"; String

running scripts through processbuilder

≯℡__Kan透↙ 提交于 2019-12-04 09:13:07
I'm trying to run Python, Ruby, C, C++, and Java scripts from a java program, and Processbuilder was suggested to me as a good way to run the scripts. From what I understand, Processbuilder mostly runs native files (.exe on windows, etc.). However, I have heard a few things about running scripts (nonnative) files using Processbuilder. Unfortunately, everything I find on the subject is incredibly vague. If someone could clarify a way to run nonnative scripts such as Python, Ruby, etc. I would be most grateful! You can check the ProcessBuilder documentation over at Sunoracle , but basically, you

Java - start another class' main in a different process

a 夏天 提交于 2019-12-04 09:08:33
I need a clean way to start many instances of a Java program with a GUI, and I want to do it programmatically. The "program" i want to run is just a .class file (a compiled .java file with a main method), it should show a GUI and run independently from the others (as its own process). I also need to pass that program some arguments. Check EDIT5 for the complete working solution code. Here's the class that's supposed to start the many processes package startothermain; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class Starter { public

Can't run program with ProcessBuilder, runs fine from command line

≡放荡痞女 提交于 2019-12-04 05:21:31
问题 On linux (debian), I can run this command: /usr/lib/jvm/jdk1.7.0_21/bin/java -jar ~/myjar.jar ".*" I am trying to run it from a Java program instead with: ProcessBuilder pb = new ProcessBuilder(java, "-jar", "~/myjar.jar", "\".*\""); System.out.println(pb.command()); prints the following, as expected: [/usr/lib/jvm/jdk1.7.0_21/bin/java, -jar, ~/myjar.jar, ".*"] However I don't get the same output from the program (it runs but the ouput looks as if the ".*" argument is not properly taken into

How to set PATH environment variable in ProcessBuilder java in windows

回眸只為那壹抹淺笑 提交于 2019-12-04 01:07:36
问题 I am trying to set the PATH environment variable for the process builder in java, I tried the following: ProcessBuilder pb = new ProcessBuilder(command); Map<String, String> mp = pb.environment(); mp.put("Path", "myPath"); pb.start(); But the following did not work, the process builder picked the default system path. I came across this question and this trick his not helping me in my current project. What should I do to get around this? 回答1: Path is used in a new proccess. It doesn't used to

ProcessBuilder redirected to standard output

纵然是瞬间 提交于 2019-12-03 22:28:54
I would like to redirect a java process output towards the standard output of the parent java process. Using the ProcessBuilder class as follows: public static void main(String[] args) { ProcessBuilder processBuilder = new ProcessBuilder("cmd"); processBuilder.directory(new File("C:")); processBuilder.redirectErrorStream(true); // redirect error stream to output stream processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT); } I would have expected that the outputs of "cmd", which are like: Microsoft Windows [version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. Tous droits