运行JPOS Q2时出现NoClassDefFoundError错误求助
JPOS Q2环境启动报错:NoClassDefFoundError: org/osgi/framework/BundleException
首次通过Maven与IntelliJ搭建JPOS Q2环境,运行时触发如下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/osgi/framework/BundleException at Q2_Main.main(Q2_Main.java:6) Caused by: java.lang.ClassNotFoundException: org.osgi.framework.BundleException at java.net.URLClassLoader.findClass(URLClassLoader.java:387) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ... 1 more Process finished with exit code 1
已通过Maven引入jpos 2.1.6依赖,执行了Maven Clean+Install操作,外部库中可见jpos依赖,但问题仍未解决。
环境配置详情
POM文件配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>Q2_JPOS_TEST</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.jpos</groupId> <artifactId>jpos</artifactId> <version>2.1.6</version> </dependency> </dependencies> </project>
运行代码
import org.jpos.q2.Q2; public class Q2_Main { public static void main(String[] args){ Q2 q2 = new Q2("src/main/java/deploy"); q2.start(); } }
操作步骤
- 文件->新建项目->Maven项目(自动生成POM)
- 将主项目中的JPOS依赖复制到新POM文件
- 在IntelliJ Maven面板执行Clean + Install
- 创建Q2_Main.java
- 点击运行按钮后出现上述错误
依赖树信息
[INFO] Scanning for projects... [INFO] [INFO] ----------------------< org.example:Q2_JPOS_TEST >------------ [INFO] Building Q2_JPOS_TEST 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]----------------------- [INFO] [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ Q2_JPOS_TEST --- [INFO] org.example:Q2_JPOS_TEST:jar:1.0-SNAPSHOT [INFO] \- org.jpos:jpos:jar:2.1.6:compile [INFO] +- commons-cli:commons-cli:jar:1.4:compile [INFO] +- org.apache-extras.beanshell:bsh:jar:2.0b6:compile [INFO] +- org.bouncycastle:bcprov-jdk15on:jar:1.67:compile [INFO] +- org.bouncycastle:bcpg-jdk15on:jar:1.67:compile [INFO] +- org.hdrhistogram:HdrHistogram:jar:2.1.12:compile [INFO] +- org.javatuples:javatuples:jar:1.2:compile [INFO] +- org.jdom:jdom2:jar:2.0.6:compile [INFO] +- org.jline:jline:jar:3.19.0:compile [INFO] \- org.slf4j:slf4j-api:jar:1.7.30:compile [INFO] -------------------------------------------------------------- [INFO] BUILD SUCCESS [INFO] -------------------------------------------------------------- [INFO] Total time: 2.205 s [INFO] Finished at: 2023-02-17T15:43:38-07:00 [INFO] --------------------------------------------------------------
Maven Settings配置
<?xml version="1.0" encoding="UTF-8"?> <settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <servers> <server> <username>user</username> <password>password</password> <id>artifactory</id> </server> </servers> <mirrors> <mirror> <id>artifactory</id> <url>https://artifactory.company.com:443/central</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> <profiles> <profile> <id>artifactory</id> <!--Override the repository (and pluginRepository) "central" from the Maven Super POM to activate snapshots for both! --> <repositories> <repository> <id>central</id> <url>http://central</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://central</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <!--make the profile active all the time --> <activeProfile>artifactory</activeProfile> </activeProfiles> </settings>
解决方案
问题出在jpos 2.x的Q2模块依赖OSGI框架类,但默认的jpos依赖并未传递引入OSGI核心包。需要手动在POM的<dependencies>中添加OSGI核心依赖:
<dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.core</artifactId> <version>6.0.0</version> </dependency>
添加完成后,执行Maven Clean+Install,重新运行Q2_Main即可解决该错误。
内容的提问来源于stack exchange,提问作者tytyty
相关产品推荐
相关产品推荐

