简体   繁体   中英

NoClassDefFoundError when running an OSGI bundle as classic jar

I have common code between a Java application and an RCP application. So I have created an OSGI bundle which contains:

  • a main class to use it as a classic jar
  • an OSGi manifest to use it in my RCP application

I built all with Tycho Manifest-first and it worked fine until I needed to use an external jar in my common code.

I need to use jsch so I have add jsch in my MANIFEST.MF :

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. But when I run the bundle as pure JAR with java -jar myjar.jar , I get the following error:

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

If I export my bundle with Eclipse, it works. So I have an error in my tycho configuration...

How to solve this problem ? Is there a jar-with-dependencies for Tycho ? 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.)

Just add the maven-assembly-plugin to your build, and let it build a jar with all dependencies:

<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

if you are workin on windows platform, you can try java -classpath ;myjar.jar com.yourpackage.mainclass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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