Capturing an exception from another running Java application

I ran into a problem when I have a program (not written by me by someone else). I want to run 24/7, but sometimes it works. This is usually not a problem, because I can simply create a process observer that checks if it crashed and then restarts it if necessary.

But this particular program sometimes throws an exception and displays it in the graphical interface built into it. In this case, the program does not crash at all. The interface remains enabled, but the actual server functionality is not available.

Is there any way to intercept this information from this process?

+6
source share
5 answers

You want to use the Java virtual machine interface . I can’t give you the code to catch your exception, but this is where to look. You will need to do some detective work to find the class that throws the exception, or at least find some kind of indicator that it threw.

Edit: You can also ask the supplier to find out if they know about it. You can also see if this exception writes to a log file that you might observe.

+4
source

I assume that you do not have access to the source code, so if it outputs to the graphical interface, the answer is no. Even if you can connect to the running process, you will need to catch the exception, but it is caught and sent to the graphical interface, and not selected from the application.

In theory, you can screen an application. I do not know any special tools for this, but they can be there.

Edit: Perhaps I was mistaken above, view the message here where they get the stack from the working thread. You probably won’t be able to catch the exception, but if you are lucky, the stack trace will look very different if the program works fine compared to when the exception was thrown.

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

+2
source

This may or may not work, but if the application displays an error and the server stops working, is there a reduction in memory usage? If so, you could simply add logic to the process monitor to invoke the windows tasklist to find out if memory usage falls below a certain threshold. You will need to check how much memory the program usually uses and how much it uses after an error.

Since you said that the server’s functionality stops working, another option would be to write a simple program that basically just pings the server, how often do you want to make sure that it still works. If not, start the process and restart it.

+2
source

Is another java program? Look at AspectJ , you can hack something if you have control over the launch of the program.

+1
source

Without the ability to rebuild the application, you are usually unlucky if you are not doing some kind of extensive hack. Here is one of the options that I can think of.

Most likely, the application replaces System.out and / or System.err with its own stream implementation. If this is the case, you can try to find a class for this thread and replace it with your own shell with the same name. You can rename the original class with jarjar. In wapper, you can provide console output for detecting exceptions.

+1
source

Source: https://habr.com/ru/post/897474/


All Articles