5
votes

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.)

2

2 Answers

1
votes

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.

0
votes

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