9

I created a Java desktop-application (using Swing) and am now trying to make it work by starting it from the net using JNLP. The application works fine when I start it from the terminal, but as soon as I launch it from JNLP, it does not close. I have to manually kill the process every time.

I read that there might be a problem if my JFrame uses DISPOSE_ON_CLOSE as the default close-operation, but it doesn't. It uses DO_NOTHING_ON_CLOSE (implicitly). Also, I'm explicitly calling System.exit(0) after releasing all my objects:

f = new JFrame("Pacman");
f.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        // Terminate the Game-loop:
        GameLoop.INSTANCE.stopLoop();
        // Close the application:
        System.exit(0);
    }
});

I guess that there might be an exception thrown when I close the application, but I can't find a way to get the console-output (e.g. the Stack-Trace) of a running application started with JNLP. Here's what I tried:

  • Start javaws with the debugging parameters and connect with jconsole (works but I can't find any exception- or console-ouput).
  • Start javaws with the debugging parameters and attach IntelliJ debugger to it (also works but does not give me any output)

So, how can I start the application with JNLP and get the output (written to the default out- and error-streams), as if I would do with a normal desktop application?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • 1
    I'd approach it as `try { GameLoop.INSTANCE.stopLoop(); } catch(Exception e) { e.printStackTrace(); /* maybe pop a JOptionPane for debugging */ } // Close the application: System.exit(0);` – Andrew Thompson Jun 05 '12 at 15:31
  • Could you post the code of `GameLoop.INSTANCE.stopLoop()`? From the name of this method, I think, that there might be a `wait()` or `Threa#join()` called which waits infinitely for some reason. Debugging this method is also a good idea. – npe Jun 05 '12 at 19:14
  • @npe I'm not using the `Thread`-class, but the executer-framework. So, no `join()`- or `wait()`-calls. You might find the whole class (and the whole project) [here](https://bitbucket.org/LukasKnuth/pacman/src/70041773fdb8/src/org/ita23/pacman/game/GameLoop.java) – Lukas Knuth Jun 05 '12 at 19:37
  • @AndrewThompson Is their really no other way of doing this (sanely)? If the application hangs, I don't think it's going to spawn a `JOptionPane` in the same context. – Lukas Knuth Jun 05 '12 at 19:37
  • 1
    *"Is their really no other way of doing this"* I never meant to imply it was the only way. It is just a 'quick-n-dirty' way to pause execution before shutting down. You can dump the stack trace elements to the option pane. – Andrew Thompson Jun 05 '12 at 19:42
  • Duplicated of https://stackoverflow.com/questions/686061/how-can-i-debug-applications-under-java-web-start-jnlp – Manuel Romeiro Jan 28 '21 at 19:42

3 Answers3

17

Solution #1 - Enable Java Console, and look for exceptions.

You can do it via Java Control Panel. Switch to Advanced tab, and in the Java Console make sure Show console is selected.

Then, run your application and monitor the console for exceptions. Fix the exception.

Solution #2 - Debug your running application (properly).

Start the Web Start app like this (for Java 1.6 and newer):

javaws -verbose -J-Xdebug -J-Xnoagent -J-Xrunjdwp:transport=dt_socket,server=n,suspend=y,address=8123 http://myserver.com/path/to/myapp.jnlp

If using earlier java versions (1.4.2, 1.5) set the environment variable, like this:

set JAVAWS_VM_ARGS="-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=n,suspend=y,address=8123"

and run the app via:

javaws http://myserver.com/path/to/myapp.jnlp

When the app runs:

  1. Attach a debugger (Eclipse will do - use Run => Debug Configurations => Remote Java Application, and in Connection Properties panel enter the port passed in the parameters to javaws (in this case: 8123).
  2. Set a breakpoint inside your windowClosing method.
  3. Try to close your application - Eclipse should break the execution on your breakpoint
  4. Step into the GameLoop.INSTANCE.stopLoop() method to see where/when it hangs.

Don't expect to see a solutions in the console, just step through the code with a debugger - if the application hangs, it will show you where.

npe
  • 15,395
  • 1
  • 56
  • 55
  • Nice, I now have the exception. Thanks! – Lukas Knuth Jun 06 '12 at 16:10
  • Is the Xnoagent option really needed for newer Java Versions? – keiki May 08 '14 at 08:26
  • After launching javaws with option suggested in Solution#2, with java 7 my application launches but while starting debugger from eclipse it give error that could not connect to VM. I have specified the correct port no (8123) which was used for launching javaws. One observation was that javaws was not showing in task manager as a process which surprised me as the application was launching. – user1630693 May 26 '15 at 13:11
2

There are times when even the console doesn't show anything, for example when there is a problem with the TLS/SSL handshake (i.e. a close_notify or handshake_failure). In these cases you need to do the following:

  1. Enable the Java logs and tracing in the Java Control Panel > Advanced. Java Control Panel: logs and tracing

  2. Enable parameters for debugging Java & launching the JNLP, there are two ways you can do it:

    2.a. Download the JNLP file and execute it from command line (the SET command is not required in this particular case).

    set JAVA_TOOL_OPTIONS=-Djavax.net.debug=all
    javaws -wait jnlp.jnlp
    

    2.b. Add arguments (i.e. -Djavax.net.debug=all) for the JVM in the Java Control Panel > Java > View (this is not required in this particular), and launch the JNLP file from browser:

    Java Control Panel: jvm arguments

  3. The logs and traces are located in the log directory from the Java Deployment Home from where I paste these locations:

    a. Windows XP: %HOME%\Application Data\Sun\Java\Deployment

    b. Windows 7/Vista: %APPDATA%\..\LocalLow\Sun\Java\Deployment

    c. Linux/Solaris: %HOME%/.java/deployment

lmiguelmh
  • 3,074
  • 1
  • 37
  • 53
0

This answer is an alternative to npe answer to enable the remote debug (Windows).

  • Go to Control Panel;
  • Click on Java, to open Java Control Panel;
  • Inside of Java Control Panel, go to Java tab, and click "View";
  • This will open a window with installed java versions. On runtime parameters put "-Xdebug -Xrunjdwp:transport=dt_socket,address=8123,server=y,suspend=n" (if you want to debug when application is starting, change to "suspend" to "y", that will make the application stop until an editor connect remotely);

Enabling remote debug

Afer that, configure you editor to debug remotely to the configured port (localhost:8123 in this case).

Manuel Romeiro
  • 1,002
  • 12
  • 14