netbeans: Uncompilable code exception with Compile on Save
Apache NetBeans version
Apache NetBeans 16
What happened
Certain projects cause a runtime exception when run or debugged in the IDE, when the ‘compile on save’ option is selected in the project properties. One trigger is using the instanceof pattern matching, e.g.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() instanceof JButton button) {
JOptionPane.showMessageDialog(this, String.format("Pressed '%s'", button.getText()));
}
}
The exception is “Exception in thread “AWT-EventQueue-0” java.lang.RuntimeException: Uncompilable code” - see below for more details. The problem does not occur if compile on save is not selected, or when running the build JAR direct from the command line. Clean and build project does not help. Clearing the NB cache does not help.
How to reproduce
I’ve made a minimal project on github which reproduces always for me: https://github.com/pedro-w/UncompilableCodeDemo
- Clone this project and open in the IDE
- Run and press the button on the form that appears - should be OK
- Close app and select ‘Compile on Save’ in the project properties
- Re-run and press the button - the exception should appear
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable code
at demo.NewJFrame.jButton1ActionPerformed(NewJFrame.java:47)
at demo.NewJFrame$1.actionPerformed(NewJFrame.java:39)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
…
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
BUILD SUCCESSFUL (total time: 4 seconds)
Did this work correctly in an earlier version?
I believe it worked correctly on NetBeans 15
Operating System
Windows 11 Pro 22H2 (x64)
JDK
openjdk version “17.0.3” 2022-04-19 OpenJDK Runtime Environment Temurin-17.0.3+7 (build 17.0.3+7) OpenJDK 64-Bit Server VM Temurin-17.0.3+7 (build 17.0.3+7, mixed mode, sharing)
Apache NetBeans packaging
Apache NetBeans provided installer
Anything else
No response
Are you willing to submit a pull request?
Yes
Code of Conduct
Yes
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 23 (18 by maintainers)
@negora assuming everyone agrees with it, this would only result in a change to the default settings. It would not remove the feature from NetBeans. If the feature worked well for you before, it will continue to do so.
I can’t really answer the question about prioritization since I can only speak for myself, but I don’t think anything will change there either. NetBeans is run by the community, so as long someone is interested in this feature there will be probably someone who is also willing to update it.
@neilcsmith-net we can turn it off for everything and make the user fill out a captcha to reactivate it 😛
what I am saying is that the mail is already out and open for discussion. Feel free to suggest more items or comment on existing items. We probably should just turn it off in general without too many checks and conditions.
mvnd is just like maven but it has a warmed up daemon running in the background. Additionally the mvnd launcher is a native image which makes execution time of maven commands very fast while the “regular” hotspot JVM running the the background keeps as much state as possible without having to parse the pom again and again etc. Just point NetBeans to the mvnd installation instead of regular maven and most builds should “just work”.
mvnd runs many tasks in parallel, which is the main reason why some projects with more exotic plugins might fail. I have seen it happening before, but since maven and mvnd are a drop-in replacement its usually not a big deal - I simply switched back on a case by case basis.
NB 17 will have some mvnd specific fixes since the maven output looks slightly different which confused the stacktrace linker and things like that. Release candidates are available, feel free to give it a try.
I can confirm this issue. I’ve versions 15 and 16 of NetBeans IDE installed in Debian 11.6, and the latter works incorrectly when I’ve the option Compile on save enabled (which I always have). Version 15 behaves well.
As @pedro-w mentions, in my case what triggers this error is the use of the newer form of
instanceof, the one that uses pattern matching. All the classes that use this form are affected. I’ve checked this by usingjavapand decompiling them. I’ve found code like this:I’ve tested this with versions 17 and 19 of OpenJDK.