简体   繁体   English

运行OSGI包作为经典jar时的NoClassDefFoundError

[英]NoClassDefFoundError when running an OSGI bundle as classic jar

I have common code between a Java application and an RCP application. 我在Java应用程序和RCP应用程序之间有共同的代码。 So I have created an OSGI bundle which contains: 所以我创建了一个OSGI包,其中包含:

  • a main class to use it as a classic jar 一个主要类,用它作为经典的jar
  • an OSGi manifest to use it in my RCP application 一个OSGi清单,可以在我的RCP应用程序中使用它

I built all with Tycho Manifest-first and it worked fine until I needed to use an external jar in my common code. 我用Tycho Manifest-first构建了所有内容,并且它工作正常,直到我需要在我的公共代码中使用外部jar。

I need to use jsch so I have add jsch in my MANIFEST.MF : 我需要使用jsch所以我在我的MANIFEST.MF中添加了jsch:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Main-Class: mygroupid.Main
Bundle-Name: Common tools
Bundle-SymbolicName: common-tools
Bundle-Version: 1.0.1.qualifier
Export-Package: mygroupid,
      mygroupid.tools
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.3.0"
Require-Bundle: com.jcraft.jsch;bundle-version="0.1.46"

I build my RCP application with Tycho and it works. 我用Tycho构建我的RCP应用程序,它可以工作。 But when I run the bundle as pure JAR with java -jar myjar.jar , I get the following error: 但是当我使用java -jar myjar.jar将包作为纯JAR运行时,我收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/jcraft/jsch/JSchException

If I export my bundle with Eclipse, it works. 如果我用Eclipse导出我的包,它就可以了。 So I have an error in my tycho configuration... 所以我的tycho配置有错误...

How to solve this problem ? 如何解决这个问题呢 ? Is there a jar-with-dependencies for Tycho ? 对Tycho有依赖的jar吗? It seems not to be the case What I have missed ? 似乎不是这样的我错过了什么?

(My configuration: Eclipse Juno with m2e, Tycho 0.16.0, p2: Juno, Tycho: packaging>eclipse-plugin, target-platform-configuration : resolver=p2 and pomDependencies=consider.) (我的配置:带有m2e的Eclipse Juno,Tycho 0.16.0,p2:Juno,Tycho:打包> eclipse-plugin,target-platform-configuration:resolver = p2和pomDependencies =考虑。)

Just add the maven-assembly-plugin to your build, and let it build a jar with all dependencies: 只需将maven-assembly-plugin添加到您的构建中,然后让它构建一个包含所有依赖项的jar:

<plugin>
   <artifactId>maven-assembly-plugin</artifactId>
   <version>2.4</version>
   <configuration>
      <descriptorRefs>
         <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
   </configuration>
</plugin>

Alternatively, you can also create an assembly where the jars are packed individually . 或者,您也可以创建一个装配,其中罐子是单独包装的 You'll need to write your own assembly descriptor for this. 您需要为此编写自己的程序集描述符

if you are working on Linux/Unix platform, you can try java -classpath :myjar.jar com.yourpackage.mainclass 如果你在Linux / Unix平台上工作,你可以试试java -classpath:myjar.jar com.yourpackage.mainclass

if you are workin on windows platform, you can try java -classpath ;myjar.jar com.yourpackage.mainclass 如果您在Windows平台上工作,可以尝试java -classpath; myjar.jar com.yourpackage.mainclass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM