0

I am using a really old Java version and i am missing a lot of classes. The system runs on an embedded platform. There is a class to execute system commands, but the output from the command is discarded. Is there anyway to cache or get this output another way?

There is another java application that is not coded by us that we interact with. This application starts a test and output the results in the shell. We are not able to edit the source code of that application.

Any suggestions?

trexake
  • 1
  • 2

4 Answers4

0

If the external app will run inside the same process, you can try to redirect System.out (using System.setOut()) before the call to the external application.

Vlad
  • 10,602
  • 2
  • 36
  • 38
  • This sounds interesting. What do you suggest me to use instead? – trexake Nov 24 '14 at 13:45
  • I found this: http://stackoverflow.com/questions/8708342/redirect-console-output-to-string-in-java However, i dont have the method setOut() And i cant change system.out since its a final variable. – trexake Nov 24 '14 at 13:50
  • A `ByteArrayOutputStream`-backed `PrintStream` was my first choice too. What version of Java are you using? System.setOut() is there since 1.1. BTW, this only helps if your other app starts in the same process, not in a new one. – Vlad Nov 24 '14 at 13:53
  • Using java with these options: target 1.1, source 1.3. This is some special java for this system. – trexake Nov 24 '14 at 13:55
0

You can check ProcessBuilder to run system commands.



       ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
       Map env = pb.environment();
       env.put("VAR1", "myValue");
       env.remove("OTHERVAR");
       env.put("VAR2", env.get("VAR1") + "suffix");
       pb.directory(new File("myDir"));
       File log = new File("log");
       pb.redirectErrorStream(true);
       pb.redirectOutput(Redirect.appendTo(log));
       Process p = pb.start();

javaCurious
  • 140
  • 2
  • 14
  • The ProcessBuilder and Map classes does not exists. It's a really shitty OS. – trexake Nov 24 '14 at 13:46
  • ProcessBuilder class is in java since 1.5. If you want to use older than this another alternative is to use, java.lang.Runtime class. And how come Map is not there? java.util.Map is there since JDK 1.2. What version of Java are you using? – javaCurious Nov 24 '14 at 15:42
  • Using java with these options: target 1.1, source 1.3. However, this is a special JVM for this specific system. Crappy like hell – trexake Nov 28 '14 at 09:28
0

Depending on how desperate you are, you can manipulate the standard output of the launched process by adding a "shim" class. Basically, create your own class with a main method which redirects stdout to your desired location (like some known file).

public class HackMain {
  public static void main(String[] args) {
    // ... use reflection to hack System.out ...

    // invoke "real" main
    RealMainClass.main(args);
  }
}

Then add a jar with this class to the invoked process command line and call it instead like java -cp <other_jars>:<hack_jar> HackMain [<args> ...].

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
0

If you're using a shell wrapper and you have access to the file system you can try to redirect the output of your command to a file and read it from there:

Ish.execute(fancyCommand + " > myfile.tmp");
InputStream is = null;
    StringBuilder sb = new StringBuilder();
    try {
        is = new BufferedInputStream(new FileInputStream("/path/to/myfile.tmp"));
        int c;
        while ((c = is.read()) != -1){
            sb.append((char)c);
        }
    } catch (IOException ioex) {
        ioex.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ignore) {
            }
        }
    }
Vlad
  • 10,602
  • 2
  • 36
  • 38