1

There are two Java applications: application 1 and application 2.

I want to use jar file of application 1 in application 2. I want to create executable jar of application 2 so that I can execute application 2 as jar by command line.

Application one: There are classes in application 1 as ClassA.java, ClassB.java

pom.xml:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

I created jar file of application 1 using mvn clean compile assembly:single

Now I have added jar file of application 1 created before as an external jar in application 2.

In application 2 There is a main class : migration.DataMigration.java There are dependencies also in pom.xml of application 2. DataMigration class is using ClassA.java and ClassB.java.

Now I want to create a executable jar of application.

I tried to create this using maven-assembly-plugin but I got error : ClassA.class not not found, ClassB.class not found : it means jar of application 1 is not being available during executable jar creation.

but when I run application 2 in eclipse it executes correctly without error.

Can any one suggest how to create executable jar of application 2

Brian Burns
  • 20,575
  • 8
  • 83
  • 77
Bhim
  • 51
  • 8
  • possible duplicate of [Classpath including JAR within a JAR](http://stackoverflow.com/questions/183292/classpath-including-jar-within-a-jar) – Braj Aug 01 '14 at 11:35

1 Answers1

1

In application2.jar you can specify to add application1.jar to its classpath. In the META-INF/manifest.mf file of application2.jar add:

Class-Path:application1.jar

if the application1.jar is stored aside (in the same directory) of application2.jar it will be added to the classpath by the runtime.

To realize this in your maven-build:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    ...
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
        </manifest>
      </archive>
    </configuration>
    ...
  </plugin>

Source: http://maven.apache.org/shared/maven-archiver/examples/classpath.html

Conffusion
  • 4,335
  • 2
  • 16
  • 28
  • No, It did not solve the problem : application1.jar is kept in Referenced Libraries folder of application 2. – Bhim Aug 01 '14 at 12:00
  • don't know of "Referenced Libraries" folder. Is this a standard java concept of are you talking about Eclipse ? – Conffusion Aug 01 '14 at 14:23
  • No, its only a folder. If you will add external jar in maven project then it will goes in this folder. – Bhim Aug 01 '14 at 14:33
  • with the maven-jar-plugin configuration as I proposed can you have a look at the content of the META-INF/manifest.mf file in the (by maven) generated application2.jar ? – Conffusion Aug 01 '14 at 14:39
  • 1
    I'm afraid I can't help you further with this. I always use local maven repository to exchange jars between projects. This way all my dependencies are managed with – Conffusion Aug 01 '14 at 15:02