I was confused by a program exiting silently without entering main
, which turned out to be because a System.loadLibrary
call in a static block threw UnsatisfiedLinkError
. To narrow the problem down, this is a construct that makes the program fail silently:
public class T {
static void bad() {
throw new RuntimeException("Terror error!");
}
static {
System.out.println("in static");
bad();
}
public static void main(String[] args) {
System.out.println("in main");
}
}
When this is run, the process exits silently after printing only “in static”, with exit code 1. At least that is what happens with my current Java environment, “OpenJDK 64-Bit Server VM Homebrew (build 22.0.2, mixed mode, sharing)”.
Of course this can be fixed with a try-catch block around the bad
call, but what if there isn’t such a block because it was unclear to the programmer it was needed? (Someone could have done this sort of thing in a library outside of my control, for instance.) Is there any way to hook in to handle an Error
or RuntimeException
thrown from a static block, to at least get a message that shows where the problem occurred?
As noted in comments, this is simply a bug in JDK22. Thanks!