我正在尝试将Java 8项目升级到Java 11,并在这样做时遇到了很多问题,但是我已解决了所有问题,但每次尝试在Netbeans中运行我的项目:
[警告:在上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'loginDialogFXController'的bean时出错:通过字段'ldapTemplate'表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建在com.decisioninsight.teleios.spring.config.LdapConfig中定义的名称为“ ldapTemplate”的bean时出错:通过工厂方法的Bean实例化失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.ldap.core.LdapTemplate]:工厂方法“ ldapTemplate”抛出了异常;嵌套的异常是org.springframework.beans.factory.BeanCreationException:在com.decisioninsight.teleios.spring.config.LdapConfig中定义名称为'contextSource'的bean创建错误:通过工厂方法实例化Bean失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.ldap.core.support.LdapContextSource]:工厂方法'contextSource'引发了异常;嵌套的异常是java.lang.IllegalAccessError:类org.springframework.ldap.core.support.AbstractContextSource(在spring.ldap.core模块中)无法访问com.sun.jndi.ldap.LdapCtxFactory类(在java.naming模块中)模块java.naming不会将com.sun.jndi.ldap导出到模块spring.ldap.core
我确实知道该错误的解决方案是按指定的in this answer将--add-exports标志添加到POM文件。这是我的POM文件的一部分:
<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>
但是,每次在Netbeans中运行项目时,我仍然会收到此错误。我想念什么吗?
因此,经过无数次尝试,我终于发现Netbeans / Maven使用了错误的java.exe来运行该程序,即使它们据说指向了正确的可执行文件。我有一些Java程序需要Java 8的32位版本,因此我的计算机上安装了32位(默认)和64位的AdoptOpenJDK 8,以及AdoptOpenJDK 11 64位。我终于弄清楚了Java 11可执行文件确实具有--add-exports选项,即使Netbeans和Maven都抱怨说在使用Java 8可执行文件时它不是一个选项。我最终将nbactions.xml文件更改为专门指向Java 11可执行文件,它开始正常工作!
这里是nbactions.xml文件:
<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>
以前exec.executable看起来像这个java。确保Netbeans / Maven确实使用了正确的可执行文件!