External java program: Handle SIGTERM - java

I have java program (with a gui) which is running on a host. On runtime a user can add some data records. The program just works with them.
Later the system maybe shuts down or the program is just closed by SIGTERM.
Unfortunately the porgram only seems to store the data records if it is closed manually by the user with the "X".
Now i have the problem that i maybe lose some data if the program is closed by SIGTERM. Unfortunately there is no contact email address to ask the author for the change.
I tried to decompile it with "jd gui" which seemed to work more or less. I thought then i just could add the signal handler and invoke the "window close" method.
But "jd gui" created code like this:
/* 110:269 */ if ((??? = Thread.currentThread().getContextClassLoader()) != null) {
/* 111:270 */ return ???;
And i also get many other errors if i try to compile the source.
I think this is because the jar file is obfuscated by some tool. Unfortunately the decompiled code doesn't work and i am not able to recompile the file with the JFrame.
Long story short:
Is it possible to write a "wrapper program" which just handles SIGTERM and then invokes the JFrame "close method" (or how it is called) of the main frame (which is the only JFrame)?
I just do not know how to invoke such a method of an external running java program.
Thank you very much.
Best regards
Kevin

Create your own class with a main method which installs a shutdown hook to save before closing. then invoke the original programs main method. add this to the jar and use this class to launch the program.

Try to deobfuscate the application. Have a look here (2009) - Tool to deobfuscate Java codes.
Maybe it is possible to resolve the source so that you are able to diggin deeper into
the source and compile it.
Otherwise it is quite complicated to hook into a Swing based ui event queue from outside a wrapper application.
Maybe you can decompile the main class and replace its main method with your own and
add a shutdownhook see: Useful example of a shutdown hook in Java? which calls the onClose (deobfuscated) method.
Any way, an interesting and challening question!

Related

Want to know the exact comment

how to run a jar file in background in windows cmd
this below jar file cmd i want to run in background
java -Djavax.net.ssl.trustStore=cacerts_appedo_agent-Djavax.net.ssl.trustStorePassword=changeit -jar appedo_tomcat_agent_2.0.063.jar
You can't do this in a platform-independent manner.
In Unix/Linux, you would call the fork() system call, which duplicates your process. With Java, that means duplicating the entire JVM. Then, you'd have to figure out whether it's the parent process or the child process, which you can determine from the process ID that the fork() call gives you. If it's the parent process, you exit. If it's the child process, you have to close standard input, standard output and standard error.
In Windows, there appears to be the FreeConsole function, but I know next to nothing about Windows programming.
So conceivably, you could write a JNA library that figures out on which platform you are, and invokes the appropriate calls to achieve this. But it's probably not the best idea to get rid of the console window when starting a Java application.
Use javaw, although you won't get any console output either, which is inconvenient.

Java/C++ background program to get keypress to run exe

Right, my title probably made no sense. However I'll explain it now.
I have a program coded in C++ that I wish to allow a keycombo to be pressed to run the program. My program does not run all the time it only runs when clicked then closes when the operation is complete. It simply does it's function then closes itself. Now here is the question.
Would it be simpler to attempt to edit my C++ program to run all the time via a thread in a class that then called the other class to run that did the function then stopped and listened for keypress again.
Or
Create a Java program that runs all the time and listens for a keyEvent and upon doing so runs the other programs exe. Then bundle the exe and jar in to one exe that is installed and run so they have access to each other.
If I did it this way is it even possible as far as I know key events need to have focus to be able to be detected. Meaning that I would need to be running the program as my main window to detect it? Or is that not true.
I'd looked at the java route as my Java is better than my C++. Is this do-able and if so what approach would be the better one?
Hope this is explained well enough let me know otherwise.

Launching an external .exe and reading in real-time what has been written in Console [duplicate]

This question already has answers here:
Run external program from Java, read output, allow interruption
(5 answers)
Closed 9 years ago.
I have been trying to work on this issue I had by searching thoroughly so as to find out what to do. However, none of the results I've found (at least until now) had suited my requests.
The fact is, I've got an executable JAR I've done. This jar starts an .EXE.
Now, the thing is, the EXE will keep on running the whole time, and I want to get whatever has been written in the console so as to write it on a JTextBox as soon as that is read.
Would you mind giving me an example of that? I would like to do it on my own, but my head doesn't seem to find out how.
Thank you very much.
EDIT: what I'm trying to do is a GUI for a gaming server
EDIT 2: for those saying its duplicate... wish it was... tried what the others explained but didn't work, so that's the reason I asked here..
EDIT 3: as I have been looking forward to find what the problem was, I will tell you that what I've done does not have any errors. However, I guess, it may be caused to the fact that the server (written in C++/C) may not output in a 'normal' way. May that be the reason? I hope so. Otherwise, I might be doing something really wrong.
Please notice I use InputStream in order to be able to read.. but well.
Basically, you need to start by running the process in some kind of background thread so there is no risk that it will block the Event Dispatching Thread.
Then you need to read the processes InputStream. As the input is read, you need to push these updates to the UI in such away so as not to violate the single thread rules of Swing. That is, you should ensure that all updates are made within the context of the Event Dispatching Thread.
Check out Concurrency in Swing for more details.
In this, I would recommend using something like SwingWorker. It allows you to monitor the process from a background thread, but has easy to use functionality to sync the updates back to the EDT.
Take a look at Printing a Java InputStream from a Process for an example

connect to the internet using java

Whenever i want to connect to internet,i double click a connection icon(i created it earlier where username and password(for broadband) are stored) and click connect.The icon is in the network places(Windows XP)
May i know how to launch this connection from java or any other language? (I am asking this because my Internet Service Provider doesn't charge anything between 2 AM and 8AM :-) )
Creating a system task to run the program. You shouldn't need Java to execute a program on windows.
To use Scheduled Tasks in XP: http://support.microsoft.com/kb/308569
This is going to be unnecessarily difficult with Java, I believe. You'd have to write some native code to do the job for you, at which point you may as well write your whole program in C# or C++ anyway.
But, since you asked for a Java approach, you might want to look at the Robot class. It lets you move the mouse to a specific location on the screen, click, and otherwise automate the manual actions that you are doing. It's a very fragile solution.
Alternatively, if you can figure out what command the network connection shortcut is invoking, you can directly invoke it from Java using Runtime.exec.
(I don't really see why Java is good for this task, though.)

How do I get my Java application to shutdown nicely in windows?

I have a Java application which I want to shutdown 'nicely' when the user selects Start->Shutdown. I've tried using JVM shutdown listeners via Runtime.addShutdownHook(...) but this doesn't work as I can't use any UI elements from it.
I've also tried using the exit handler on my main application UI window but it has no way to pause or halt shutdown as far as I can tell. How can I handle shutdown nicely?
The previously mentioned JNI approach will likely work.
You can use JNA which is basically a wrapper around JNI to make it easier to use. An added bonus is that it (in my opinion at least) generally is faster and more maintainable than raw JNI. You can find JNA at https://jna.dev.java.net/
If you're just starting the application in the start menu because you're trying to make it behave like a service in windows, you can use the java service wrapper which is found here:
http://wrapper.tanukisoftware.org/doc/english/download.jsp
As far as I know you need to start using JNI to set up a message handler for the Windows WM_QUERYENDSESSION message.
To do this (if you're new to Windows programming like me) you'll need to create a new class of window with a new message handling function (as described here) and handle the WM_QUERYENDSESSION from the message handler.
NB: You'll need to use the JNIEnv::GetJavaVM(...) and then JavaVM::AttachCurrentThread(...) on the message handling thread before you can call any Java methods from your native message handling code.

Categories