1

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);
   }
}                                            
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Anthony Byron
  • 65
  • 1
  • 3
  • 8

3 Answers3

3

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

lucrussell
  • 5,032
  • 2
  • 33
  • 39
Smagin
  • 31
  • 2
1
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 When Runtime.exec() won't.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

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

Taymon
  • 24,950
  • 9
  • 62
  • 84