6

I want to use Netbeans' Swing GUI builder together with a Maven project and java modules. It seems like the GUI Builder can not find the compiled classes if they are compiled as part of a java module. It gives the error "Error in loading component (...) Cannot load component class (...)" (See the whole error in the screenshot at the end of the example)

I have pinned it down to a minimal example, using Netbeans 11:

  • File > New Project > Java with Maven > Java Application (use default suggestions and click Finish)

  • In the project tree under "Source Packages", right click and add a new JFrame, and a JPanel.

  • Compile the project (Run > Build project), so that you can use the classes with GUI Builder

  • Using the GUI Builder, add an instance of the JPanel to the JFrame, by opening the JFrame in design mode, and dragging the JPanel to the JFrame.

  • Update the project to using java modules, by adding the file src/main/java/module-info.java:

module MavenGuiTest {
    requires java.desktop;
    requires java.logging;
}

  • Run "Build project" again. Netbeans then detects that it is now a java module project and shows a dialog: enter image description here

  • Click Yes, and the following will be added to pom.xml:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
            </plugin>
        </plugins>
    </build>
  • Run "Build project" again. Now the project is compiled.

  • Close the JFrame if it's open in the editor.

  • Open it again, and switch to the "Design" tab.
  • Now, you get an error:

enter image description here

When clicking Show Exceptions, you can see that the GUI Builder cannot find the compiled class of the JPanel:

enter image description here

Here is the full exception text:

java.lang.ClassNotFoundException: com.mycompany.mavenproject1.NewJPanel
    at org.netbeans.modules.form.project.ProjectClassLoader.findClass(ProjectClassLoader.java:163)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at org.netbeans.modules.form.project.FormClassLoader.findClass(FormClassLoader.java:58)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:398)
    at org.netbeans.modules.form.project.ClassPathUtils.loadClass(ClassPathUtils.java:89)
    at org.netbeans.modules.form.FormUtils.loadClass(FormUtils.java:1552)
    at org.netbeans.modules.form.PersistenceObjectRegistry.loadClass(PersistenceObjectRegistry.java:73)
    at org.netbeans.modules.form.GandalfPersistenceManager.restoreComponent(GandalfPersistenceManager.java:673)
    at org.netbeans.modules.form.GandalfPersistenceManager.loadComponent(GandalfPersistenceManager.java:949)
    at org.netbeans.modules.form.GandalfPersistenceManager.loadForm(GandalfPersistenceManager.java:484)
    at org.netbeans.modules.form.GandalfPersistenceManager.loadForm(GandalfPersistenceManager.java:260)
    at org.netbeans.modules.form.FormEditor$2.run(FormEditor.java:327)
    at org.netbeans.modules.form.FormLAF$2.run(FormLAF.java:268)
    at org.netbeans.modules.openide.util.NbMutexEventProvider$Event.doEventAccess(NbMutexEventProvider.java:115)
    at org.netbeans.modules.openide.util.NbMutexEventProvider$Event.readAccess(NbMutexEventProvider.java:75)
    at org.netbeans.modules.openide.util.LazyMutexImplementation.readAccess(LazyMutexImplementation.java:71)
    at org.openide.util.Mutex.readAccess(Mutex.java:225)
    at org.netbeans.modules.form.FormLAF.executeWithLookAndFeel(FormLAF.java:251)
    at org.netbeans.modules.form.FormEditor.loadFormData(FormEditor.java:324)
    at org.netbeans.modules.nbform.FormEditorSupport.loadOpeningForm(FormEditorSupport.java:436)
    at org.netbeans.modules.nbform.FormDesignerTC.loadForm(FormDesignerTC.java:256)
    at org.netbeans.modules.nbform.FormDesignerTC.access$300(FormDesignerTC.java:64)
    at org.netbeans.modules.nbform.FormDesignerTC$PreLoadTask$1.run(FormDesignerTC.java:245)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:136)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I cannot figure out how to fix it, and I have not found any workaround.

foolo
  • 804
  • 12
  • 22
  • Seems you are facing a reported and fixed (in coming version 11.3) issue original [NETBEANS-2963](http://mail-archives.apache.org/mod_mbox/netbeans-commits/201908.mbox/%3CJIRA.13249039.1565078283000.115591.1565078340170@Atlassian.JIRA%3E) the cleaned up version in JIRA [NETBEANS-2983](https://issues.apache.org/jira/browse/NETBEANS-2983). – SubOptimal Dec 16 '19 at 07:45
  • 1
    Can you copy and paste full Exception in here – Kalana Dec 16 '19 at 11:53
  • 1
    @SubOptimal That issue seems to be related to custom components and Gradle. The problem I have happens for standard components as well, and I'm using Maven, not gradle. Are you sure it is related? – foolo Dec 16 '19 at 14:09
  • @Kalana Sure, I have pasted the whole exception in the question. – foolo Dec 16 '19 at 14:10
  • @foolo I would believe it's the same, based on the origin of the exception `org.netbeans.modules.form.project.ProjectClassLoader.findClass(ProjectClassLoader.java:163)` – SubOptimal Dec 17 '19 at 10:09
  • @SubOptimal Yeah, the stacktrace looks similar. I checked the corresponding pull request for the JIRA ticket: https://github.com/apache/netbeans/pull/1641 and it seems to affect only Gradle-related code, but let's hope that it still will fix this problem. – foolo Dec 17 '19 at 11:40
  • @foolo Frankly I did not dig that deep. I agree with you, based on the pull request it might be a fix for Gradle projects only. You might raise a ticket for your problem, so someone could have a look for it. – SubOptimal Dec 17 '19 at 13:11

1 Answers1

4

This is just a warning not an error. You can run this project even this occurs. This warning showing when these things are happen.

  1. When you switch one version to another version.
  2. You have unused or corrupted library in your project. Try to remove that kind of libraries.
  3. You used to have a custom GUI component and you modified that class and new IDE does not recognize it anymore. There for just press Allow editing button and perform clean and build(the broom and hammer icon). After build complete, Close netbeans and reopen it.
Kalana
  • 5,631
  • 7
  • 30
  • 51
  • 1
    Thank you for your question. The project builds just fine. The problem is that the GUI Builder does not work, since it cannot find the compiled classes. So basically the questions is "How can I use Netbeans GUI Builder together with Maven and a java-modules project". I have added the details to the question, with what is shown when clicking "Show Exceptions". – foolo Dec 16 '19 at 10:28
  • 1
    The minimal example I provided is created from scratch, so it can not have anything to do with "a corrupt library in my project", or "a custom GUI component". Clicking "Allow Editing" does not solve the problem, because that just removes all the code associated with the component which could not be found. – foolo Dec 16 '19 at 10:33
  • 1
    Please read this -> https://stackoverflow.com/a/10569417/11383441. Does that answer your question – Kalana Dec 16 '19 at 15:28