0

I need to access the console on the node through java how to make this ?

public class Comando {

public static void main(String[] args) {
    String comando = "C:\\Program Files\\nodejs\\node.exe";  
    try {  
       Process process = Runtime.getRuntime().exec(comando);   
       OutputStream stdin = process.getOutputStream ();
       InputStream stderr = process.getErrorStream ();
       InputStream stdout = process.getInputStream ();


       stdin.write("1+2".getBytes());
       stdin.flush();
    //   System.out.print(stdout.read());
       stdin.close();
       System.out.print(stdout.read());

       //BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
       //BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

      // writer.close();

    } catch (IOException e) {  
        e.printStackTrace();  
    }  
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Maybe this help you http://docs.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html – Leonardo Pugliese Sep 02 '13 at 14:21
  • Read (and implement) *all* the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to `exec` and build the `Process` using a `ProcessBuilder`. Also break a `String arg` into `String[] args` to account for arguments which themselves contain spaces. – Andrew Thompson Sep 02 '13 at 14:31
  • when I implement System.out.print(stdout.read()); the program returns me-1 – user2740119 Sep 02 '13 at 17:50

2 Answers2

0

I expect that node.exe requires text, not binary as you are using it.

This means that using PrintWriter to write lines of text and BufferedReader to read lines of text would make more sense.

Java has a built in Javascript interpreter. I assume you cannot use that for some reason.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;

public class Comando {

    public static void main(String[] args) {

        String comando = "C:\\Program Files\\nodejs\\node.exe";
        try {

            ProcessBuilder builder = new ProcessBuilder(comando);
            Process process = builder.start();
            OutputStream stdin = process.getOutputStream();
            InputStream stderr = process.getErrorStream();
            InputStream stdout = process.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    stdout));
            PrintWriter writer = new PrintWriter(stdin);

            writer.write("1+2");
            writer.flush();
            stdin.close();
            System.out.print(reader.read());// return -1

            // writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

It continues at me return -1, when I try read your output