4

I am trying to upgrade my Java 8 project to Java 11 and have run into quite a few issues while doing so, but I've fixed all the issues except I can't seem to get past this error every time I try to run my project in Netbeans:

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginDialogFXController': Unsatisfied dependency expressed through field 'ldapTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ldapTemplate' defined in com.decisioninsight.teleios.spring.config.LdapConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.ldap.core.LdapTemplate]: Factory method 'ldapTemplate' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contextSource' defined in com.decisioninsight.teleios.spring.config.LdapConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.ldap.core.support.LdapContextSource]: Factory method 'contextSource' threw exception; nested exception is java.lang.IllegalAccessError: class org.springframework.ldap.core.support.AbstractContextSource (in module spring.ldap.core) cannot access class com.sun.jndi.ldap.LdapCtxFactory (in module java.naming) because module java.naming does not export com.sun.jndi.ldap to module spring.ldap.core

I do know that the supposed solution for this error is to add the --add-exports flag to the POM file as specified in this answer. Here is part of my POM file:

<profile>
    <id>default</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                        <fork>true</fork>
                        <compilerArgs>
                            <arg>--add-exports</arg>
                            <arg>java.naming/com.sun.jndi.ldap=spring.ldap.core</arg>
                        </compilerArgs>
                        <verbose>true</verbose>
                    </configuration>
                </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>${java.home}/bin/java</executable>
                            <arguments>
                                <argument>-cp .</argument>
                                <argument>--module-path='${project.build.directory}/modules'</argument>
                                <argument>--module=${moduleName}/${mainClass}</argument>
                            </arguments>
                            <longModulepath>false</longModulepath>
                            <addResourcesToClasspath>true</addResourcesToClasspath>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>copy-libs</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/modules</outputDirectory>
                            <includeScope>runtime</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.coderplus.maven.plugins</groupId>
                <artifactId>copy-rename-maven-plugin</artifactId>
                <version>1.0.1</version>
                <executions>
                    <execution>
                        <id>copy-target</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                       <sourceFile>${project.build.directory}/${project.build.finalName}.jar</sourceFile>
            <destinationFile>${project.build.directory}/modules/${project.build.finalName}.jar</destinationFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

Yet I still get this error every time I run the project in Netbeans. Am I missing something?

Naman
  • 27,789
  • 26
  • 218
  • 353
Tiamat74
  • 81
  • 1
  • 5
  • How about just all of it passed as one arg as follows: ` --add-exports=java.naming/com.sun.jndi.ldap=spring.ldap.core `? And I would hope your [maven version is compatible](https://issues.apache.org/jira/browse/MCOMPILER-279) to process the arg as well. And so is Netbeans! – Naman Mar 17 '20 at 16:34
  • 1
    Unfortunately, I have tried it both ways and neither one seems to be working! – Tiamat74 Mar 17 '20 at 16:48
  • I think you are executing the code and that might need to specify the same as the program arguments as well. `--add-exports=java.naming/com.sun.jndi.ldap=spring.ldap.core` (not sure about Netbeans here). But this is quite similar to the linked question in that sense. – Naman Mar 17 '20 at 16:55

1 Answers1

4

So, after countless tries, I finally figured out that NetBeans / Maven was using the wrong java.exe to run the program even though they were supposedly pointing to the right executable. I have some Java programs that require a 32-bit version of Java 8, so I have AdoptOpenJDK 8 32 bit (default) and 64 bit are installed on my computer, along with AdoptOpenJDK 11 64 bit.

I finally figured out that the Java 11 executable does have an --add-exports option, even though NetBeans and Maven both complained that it wasn't an option while using the Java 8 executable. I ended up changing the nbactions.xml file to point specifically to the Java 11 executable and it started working correctly!

Here is the nbactions.xml file:

<actions>
    <action>
    <actionName>run</actionName>
    <packagings>
        <packaging>jar</packaging>
    </packagings>
    <goals>
        <goal>process-classes</goal>
        <goal>org.codehaus.mojo:exec-maven-plugin:1.6.0:exec</goal>
    </goals>
    <activatedProfiles>
        <activatedProfile>default</activatedProfile>
    </activatedProfiles>
    <properties>
        <exec.args>--add-exports=java.naming/com.sun.jndi.ldap=spring.ldap.core --module-path='${project.build.directory}/modules' --module=${moduleName}/${mainClass}</exec.args>
        <exec.executable>C:\Program Files\AdoptOpenJDK\jdk-11.0.3.7-hotspot\bin\java</exec.executable>
    </properties>
</action>

Previously exec.executable looked like this:

<exec.executable>java</exec.executable>. Make sure that Netbeans / Maven is really using the correct executable!

Meraj al Maksud
  • 1,528
  • 2
  • 22
  • 36
Tiamat74
  • 81
  • 1
  • 5