Mike Bailey
Mike Bailey

Reputation: 12817

Catching an exception from another running Java application

I've run into the issue where I have a program (not written by me, by someone else) I want to run 24/7, but sometimes it crashes. Normally, this wouldn't be an issue because I can simply create a process watcher that checks if it crashed, and then restarts it if necessary.

But, this particular program sometimes throws an exception and outputs it into the graphical interface that's integrated into it. In this instance, the program doesn't crash at all. The interface stays up, but the actual server functionality is unavailable.

Is there any way I can intercept this information from this process?

Upvotes: 6

Views: 520

Answers (5)

Jordan Bentley
Jordan Bentley

Reputation: 1309

You want to use the Java Virtual Machine Tools Interface. I can't give you the code to catch your exception, but this is where to look. You'll have to do some detective work to find the class that throws the exception, or at least to find some indicator that it has been thrown.

Edit: You can also try calling the vendor to see if they know of a way. You can also look to see if it is writing the exception to a log file, which you could then watch.

Upvotes: 4

Jordan Bentley
Jordan Bentley

Reputation: 1309

I assume you have no access to the source code, so if it is outputting to the GUI the answer is no. Even if you could attach to the running process you would need to intercept the exception, but it is caught and sent to the GUI, not thrown from the application.

In theory, you could screen scrape the application. I don't know of any specific tools for doing this, but they may be out there.

Edit: I may have been wrong above, check out a post here where they get the stack from a running thread. You probably won't be able to capture the exception this way, but if you're lucky the stack trace will look very different when the program is operating normally compared to when an exception has been thrown.

Edit 2: I submitted a second, more accurate answer. See below.

Upvotes: 2

Windle
Windle

Reputation: 1392

This may or may not work, but if when the application displays it's error and the server stops working does the memory usage drop? If so you could probably just add some logic to your process monitor to call the windows command tasklist to see if the memory usage drops below some threshold. You'll have to check how much memory the program normally uses and how much it uses after the error though.

Since you said the server functionality stops working, another option could be to write a simple program that basically just pings the server how ever often you want to make sure it is still up. If not, kill the process and restart it.

Upvotes: 2

Alex Gitelman
Alex Gitelman

Reputation: 24722

Without ability to rebuild the app you are generally out of luck unless you do some extensive hacking. Here is one option I can think of.

Most likely the application replaces System.out and/or System.err with its own stream implementation. If that's the case you can try to locate the class for this stream and replace it with your own wrapper with the same name. You may rename original class using jarjar. In the wapper you can provide console output to detect the exception.

Upvotes: 1

Miserable Variable
Miserable Variable

Reputation: 28752

Is the other program Java? Look at AspectJ, you may be able to hack something using it if you have control on the program startup.

Upvotes: 1

Related Questions