1

I have a jar file that I do not have the source for (It's relatively old). I know that when it runs, it acts as a server (incoming and outgoing connections), and spits out a large amount of console output. What I want to do is have another java application kick it off using an exec, and redirect the stream within itself so that it can watch for certain keywords or timestamps within the output of the jar. The intent of the program is to keep track of certain events and provide a larger picture of when events happen, and "learn" how to react when they do happen in the future.

I'm guessing, from the reading I've done thus far, the tricky part is to get the data in real-time and not after the stream has been closed.

How would I go about doing this?

Sakamoto Kazuma
  • 2,573
  • 7
  • 34
  • 75
  • Any reason you want to exec it instead of simply provide extra classes and then invoke the jar directly in the existing JVM? You can redirect standard out easily. Also note that if this is logging output, it might be very simple to reconfigure the logging framework. – Thorbjørn Ravn Andersen Feb 06 '15 at 22:10
  • @Thorbjørn Ravn Andersen I didn't know this was possible. Any tips on how to do this? – Sakamoto Kazuma Feb 06 '15 at 22:22
  • Put the jar on your classpath. Find out where the old main() method is. Write a new main() which does what you need and invokes the old main(). Also see http://stackoverflow.com/questions/2851234/system-out-to-a-file-in-java – Thorbjørn Ravn Andersen Feb 07 '15 at 01:00

1 Answers1

1

Extract your jar with a zip program. Look at the main class in the manifest

Create a new class

public class MyLogger {
    public static void main (String args[]) throws Exception {
         System.setOut(new PrintStream("out.txt"));
         System.setErr(new PrintStream("err.txt"));
         OriginalClassName.main(args);
    }
 }

Now simply build your class ( set the Javan class path to be your original .jar that you don't have the code for ) then once complete create a new .jar file with the jar tool and set your class to be the main one in the manifest

Now when you run you can use a tool like tail ( with the -f parameter ) to print the contents of file to a console while the file is being written in realtime. Tail is available on Linux and other Unix-platforms. For Windows install e.g. cygwin.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
Damian Nikodem
  • 1,324
  • 10
  • 26