I need to read a byte[] produced by another java program I wrote. As known, main in java can only return a void. Does this mean I need to write a program with a static method with the return type byte[] ? If yes, how can I then run this method in my program and save it's return value in my programm? this is what I have in my program:
ProcessBuilder pb = new ProcessBuilder("java.exe","viewer.java","pathToViewer.java");
Process process = pb.start();
InputStream is = process.getInputStream();
byte[] bytes = IOUtils.toByteArray(is);
What is the right way?
No, you can't communicate return values across JVM instances. You must devise and implement a specific protocol that will pass the data. One simple option you have is writing to System.out
in the subprocess, which will push the data into your is
, so your bytes
will end up containing what the subprocess wrote.