563

Run processbuilder and get in and output

Question:

I'm trying to get my program to launch Enchanter to SSH into my server, but can't seem to figure out how to get in and output to go to stdin and stdout, or anywhere for that matter. I just get a blank output window in Netbeans. How to I get the Jar to run, and get input/output?

public class openShell { public void openShell() throws IOException { String line; Scanner scan = new Scanner(System.in); ProcessBuilder builder = new ProcessBuilder ("C:\\Program Files\\Java\\lib\\enchanter-beanshell-0.6.jar", "myscript.bsh"); builder.redirectErrorStream(true); Process process = builder.start(); OutputStream stdin = process.getOutputStream (); InputStream stderr = process.getErrorStream (); InputStream stdout = process.getInputStream (); BufferedReader reader = new BufferedReader (new InputStreamReader(stdout)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin)); while (scan.hasNext()) { String input = scan.nextLine(); if (input.trim().equals("exit")) { // Putting 'exit' amongst the echo --EOF--s below doesn't work. writer.write("exit\n"); } else { writer.write("((" + input + ") && echo --EOF--) || echo --EOF--\n"); } writer.flush(); line = reader.readLine(); while (line != null && ! line.trim().equals("--EOF--")) { System.out.println ("Stdout: " + line); line = reader.readLine(); } if (line == null) { break; } } } } private void LaunchButtonActionPerformed(ActionEvent evt) { //openShell open = new openShell(); //RUNS BUT NO IN OR OUTPUT //BELOW CODE IS FOR TESTING, JUST TRYING TO GET PROCESSBUILDER TO CONNECT // TO MY JAR try { ProcessBuilder builder = new ProcessBuilder( "Java -jar C:\\Program Files\\Java\\lib\\enchanter-beanshell-0.6.jar"); builder.redirectErrorStream(true); Process process = builder.start(); } catch (IOException ex) { Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex); } }

Answer1:

The method ProcessBuilder.inheritIO will redirect your command streams in your stdin, stdout and stderr. This applies to Java 7.

Answer2:

String[] args = { "java", "-jar", "C:\\Program Files\\Java\\lib\\enchanter-beanshell-0.6.jar" }; ProcessBuilder builder = new ProcessBuilder(args);

Start with breaking up the arguments as above. Then implement all the recommendations of <a href="http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html" rel="nofollow">When Runtime.exec() won't</a>.

Answer3:

The methods Process.getInputStream and Process.getOutputStream will get you streams that you can then read from and write to.

Recommend

  • PHP code (mysqli) queries are not working after specific code
  • Calling a shell script from java hangs
  • How to compare two different JSON response structure in java?
  • How to hardcode the value in JMeter
  • Is there anything like Javascript Eval in C# or Java [duplicate]
  • Powershell 2: Easy way to direct every bit of output to a file?
  • Can't get JSON data from Rails API
  • PHP - Kill proc_open() process on different file?
  • Illegal state exception while calling signout through GoogleApiClient
  • When try to execute a cgi script, get the error: “[WinError 193] %1 is not a valid Win32 application
  • Subviews for custom UIView with Nib (.xib) don't load?
  • Reading a file located in a jar as java.io.File object
  • Set text in TextView in custom dialog
  • Retrofit 2 error: NetworkOnMainThreadException
  • RxJava,Retrofit Error :Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
  • How to add plugin dependencies to pom.xml
  • Can I customize a Jackson ObjectMapper by adding a module?
  • Trying to get all column names in a table
  • How to set the navigation bar to the top in Table View?
  • UIPickerView without IB?
  • integrate POCO library in android ndk
  • Cassandra NoClassDefFoundError: com/google/common/util/concurrent/AsyncFunction
  • SqlCommand back up Database
  • “mvn clean generate-source” could not resolve dependencies
  • In Java, how can I construct a File from a resource?
  • Calculating ratio of reciprocated ties for each node in igraph
  • Android Studio Can't Find tools.jar
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • Differences in dis-assembled C code of GCC and Borland?
  • Mysterious problem with floating point in LISP - time axis generation
  • Algorithm for a smudge tool?
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • sending mail using smtp is too slow
  • Checking variable from a different class in C#
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?