Java program ends when logging out from x-session - java

When I run a Java program with a GUI (e.g. a simple JFrame) and then ending the X session (logout), my Java program is terminated. Java programs that don't use a GUI keep running.
There is nothing wrong with that, but I'd like to know how the program is terminated. I don't get messages about uncaught exceptions or other issues. The exit code is 1. If the prog was terminated by a signal, the exit code would by 128+signal (afaik).
Can someone shed some light on this issue. Can I somehow prevent program termination or control the exit code for that case?
[Edit]
I'm aware of the defaultCloseOperation. I'm using EXIT_ON_CLOSE. But this would lead to a call to System.exit(0).
I also added a WindowListener to monitor which of its functions is called when my program ends. When I finish the program by clicking on the window-X, the function windowClosing() is called. When I logout and finish the X-session so that my program is terminated, no WindowListener function is called. My program just exits with exit code 1.

When I run a Java program with a GUI (e.g. a simple JFrame)
I don't know how X-session works, but when using a JFrame you need to be aware of the frames
setDefaultCloseOperation(...);
method. When EXIT_ON_CLOSE is used, the JVM will be exited right away.
When DISPOSE_ON_CLOSE, the JVM will be exited if it is the last frame open and there are no other Threads running to prevent the JVM from closing.

Related

Command line programs on windows randomly freeze. Ctrl-C reactivates them

I am writing a simple program in Java, and sometimes it randomly freezes and does not respond. when I try to end it with Ctrl-C instead of quitting, the program springs back to life and starts working fine again. I am not posting my code because I have noticed this behavior with other command line programs on Windows, so it does not appear to be anything specific to my code. The program will eventually be running 24/7 on a headless server, so you can see why it would be a serious issue if it just stopped working every now and then. Thanks in advance. Any help is appreciated.
This sounds a bit like perhaps a selection issue: If you make a selection in the console window, it freezes console output. The next time an application attempts to flush to the console it will stall until the selection is cleared. Pressing ctrl-c will copy the selection and clear it, allowing the flush to complete and the application to continue to run. Any keypress in the console window should clear the selection though, and it sounds like only ctrl-c is working for you.
If that's not what the issue is, your next best bet the next time you see this would be to open up a native debugger (e.g. Windbg) or a java debugger and attach to the process you're running in the console process to see what is doing the waiting. It's likely that something you're calling is triggering a spurious getch / readline / etc. A debugger should make the source of the stall obvious. If you need help deciphering the stack once you have one, I might be able to help. Just paste it into this thread.
Ben

Can Matlab deploytool compile files with javax.swing elements?

Suppose I have a set of codes to display a JFrame, a JPanel, and a JLabel. This works fine if I run it as a script file. It just shows a tiny window with a label that says "A label" exactly like you would expect:
frame = javax.swing.JFrame('Test');
panel = javax.swing.JPanel();
label = javax.swing.JLabel('A label');
panel.add(label);
frame.add(panel);
frame.setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
The problem comes when I compile this as an exe file with the deploytool. It will compile and I can run the program, but the frame will show up for about 3 seconds or so then disappear. If I run from inside Matlab with !main.exe, there is no error message when the window disappears (I don't want to say it crashes because there is no error message). Neither is there one if I run the executable from the Windows command prompt (same results -- shows for a few seconds and then crashes).
Any ideas what is going on here? I can compile other files just fine. Is the problem because I included the javax.swing elements?
Many thanks for your help.
UPDATE
This feels like a really cheap hack, but having a while loop that pauses Matlab as long as the JFrame is open does the trick. So now the question is, is there a better way to do this?
The problem is probably that your main M-code function finishes executing, and since there are no figures up, Matlab decides to exit. In a Java Swing program, what would happen is that things would keep going until all the Swing windows are closed or you explicitly terminate the program. Since this is a Matlab program, the layer that's "in control" is the Matlab handle graphics layer, so you need to either have the main function executing or a figure up. (In interactive Matlab, it'll keep running as long as you have the IDE up, but there's no IDE in a compiled Matlab program, so when its work is done, it exits.)
The "Right Thing" to do from MathWorks' perspective is to probably buy the Matlab Builder JA toolbox, build the Matlab part of your program in to a Java library, include that in a main program which you write in Java. That way the Java layer is "in control" of the main execution sequence, and the "stay running as long as there are Java windows open" logic you're expecting will be in effect.
If you want to hack this to make it work in your current program structure, your invisible figure window might be a good one. Though you will need to make it Visible to have it work; invisible figures don't count for keeping a Matlab GUI running. You may be able to hide it from the user by changing its position to move it entirely off the user's screen.
Then you need to terminate the program somehow. Some part of your code is going to know when the program should end. That sounds like it's the Java part of your code. From there, you could just call java.lang.System.exit(). If you need to do Matlab-layer stuff, you could exit from M-code by communicate the "it's time to exit" back to your Matlab code, which could then call exit() or close that figure. You could do this by setting a public class variable in one of your Java classes, and have a Matlab timer object that checked that variable every 500 milliseconds or so.
If the condition that ends the program is that all your Java Swing windows get closed, that's a little tougher. Because the Matlab figure window itself is a Java AWT or Swing window, so as long as that's open you're not getting down to zero windows. What you could do is have that Matlab timer, instead of looking for a class variable, check the list of open Java windows and see if the Matlab figure is the only one left, and if so, close it or exit explicitly.

How can i detect what code base a function is running in with no references to classes?

I am making an application which works with a game that runs in an applet (i didn't make the game, i am just making things for it, in this case, the launcher), but the applet is not coded very neatly so it has calls to System.exit(int).
I need the launcher to stay open on the event of the game crashing or the user clicking the quit game button and attempting to call System.exit(int) so the only way i can do this is by making a SecurityManager that functions like System.exit(int) go to check with it to see if it's allowed to exit the Java VM, if it's not, it will throw an exeption.
The second part of the solution for keeping the launcher open is by creating an UncaughtExceptionHandler so it can ignore the exception thrown by trying to exit; By implementing this, i can also have the launcher notify the user of a bug in the launcher when it's not the exiting exception (if it's something else, that's not supposed to be there so it'll result in a bug report).
Now, the actual problem that this question is about is how can i make the SecurityManager aware of if the game or my launcher that is trying to call System.exit(int)? I still need my launcher to be able to exit when it wants to, the game is just what i want to restrict.
Someone suggested using ProtectionDomain to detect what code base is running the code but i don't understand how this is implementable. You don't have references to classes in the SecurityManager.

Java Process Not Terminating

I have a Java application launched via Webstart. About half the time, this program will not die. This program listens on a socket and when it receives a particular command, should close down. When it receives this command, it prints out what it normally should to the java console (that it received the shutdown command), the Java console closes, all the windows close, but the java process continues to run.
I have tried switching System.exit(0) to Runtime.getRuntime().halt(0) with no success - the same problem happens (my initial thought was a shutdown hook was affecting it).
Furthermore, when I have VisualVM connected to the Java process, it also indicates that it loses the connection when halt or exit are called, and I cannot reconnect, but the java process continues to exist.
Any ideas on how I could solve this? I've tries wrapping the program in try { } and exit/halt in finally { } to ensure nothing was holding it up, tried halting instead of exiting, etc...
My only thought is Java could be retaining a system resource (file handle to socket or something) or be stuck in a system call so the system won't let it quit - but the Java console closes, which to me seems like an indication the VM is trying to shutdown...
Any thoughts or ideas are appreciated!

How can I stop my application?

I have the main thread from which I start a window using invokeLater. I run my application from command line. So, when application is running I see the window and my command line is "blocked" by the application.
I can stop the application either by closing the window (as a result the command line is unblocked) or by typing Ctrl-C in the command line (as a result the window disappear).
I wanted to be able to stop the application by clicking on a button in the window of the application. I used setVisible(false) for that. But in this way I can achieve the goal only partially. My window really disappear but the command line is still blocked. So, the software is still running.
Well, I assume it's because some other threads are still running. But how can I easily close all these threads (like I do by closing the window of the application manually).
System.exit(0);
If it's a JFrame you're showing, you can tell it to exit the app when the frame is closed - the default is to just hide the frame:
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
This will exit the app if the user closes the window (top right [x] button often) , in addition you could have a Quit button whose event handler closes the window using myFrame.dispose();
You must finish all threads in order to stop your application. Just hiding the GUI will not finish the AWT-Thread. Have a look at the API of the GUI classes you use like the dispose-methods.
Try Window.dispose()

Categories

Resources