Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't open java application when ran as executable jar
    text
    copied!<p><br/> I had a strange problem today... I'm going to make a simplified example since it "worth a thousands words" :D</p> <pre><code>public class Application() { public static void main(String[] args) { try { A a = new A(); // this may throw exceptions // (which will cause an ExceptionInInitializerError) } catch (Throwable t) { JOptionPane.showMessageDialog(null, "Oooops!"); System.exit(1); } } } </code></pre> <p>Since it's a stand-alone application with a Swing GUI, my goal is to give a message to the user in case of any problems (in this case at startup)... the code above <em>works</em> in Eclipse IDE but when I export the project as executable jar by double-clicking on it, well, it just won't open.</p> <p>So I try to execute it in cmd with <code>java -jar application.jar</code> and it prints in the shell that there was an <code>ExceptionInInitializerError</code>.</p> <p>Why the error was not caught?<br /> It doesn't work even if I specify <code>catch (ExceptionInInitializerError e)</code>.</p> <p><strong>EDIT:</strong> After more indepth debugging, I found out that this problem only happens when two particular exceptions occur and the latter occurs in the catch block of the former. I corrected the bug by changing the order of some checks that I do on startup.</p> <p>The problem btw should never happen since it was originated by a volountary mistake of the JDBC driver class name to load in a static block.</p> <p>Well, at least it made me clearly understand why constructors and static initialization blocks should not throw exceptions: it makes debugging almost impossible in the case in which the class that throws the exception is used by many classes, since it may become very hard to find out when the class is loaded.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload