frazman
frazman

Reputation: 33223

How to include third party jars while running a java program

A very noob question. I have a java class

package org.foobar;
import thirdparty_jar1;
import thirdparty_jar2;

public class FooBar{
  public static void main(String[] args){
   // some code
 }
}

I use eclipse as my ide and I added these thirdparty jar 1 and jar 2 in its build path. and then I proceeded to export the jar. Now I have a foobar.jar file and now I want to run it..

I did java -cp /path/to/foobar.jar org.foobar.FooBar

but it complains about missing third party libraries. How do i run my jar (or probably build a fat jar) . I just want to run my program from command line. Thanks

Upvotes: 2

Views: 3623

Answers (3)

martinez314
martinez314

Reputation: 12332

Since you said that you are using Eclipse, did you know you can export a runnable JAR and all its dependecies via the IDE? Right-click on your project and select Export. Search for "Runnable JAR". You should see this:

enter image description here

Now you have a few options:

Extract required libraries into generated JAR.

This will unpack the third party JARs and repack them, along with your class, into a single JAR.

Package required libraries into generated JAR.

This will include the third party JARs into your JAR, as well as some special Eclipse magic to unpack them when needed.

Copy required libraries into a sub-folder

This will copy the third party JARs into a folder and then update the manifest of your JAR, adding the third-party JAR relative file references to your classpath.

Upvotes: 0

Alvin Thompson
Alvin Thompson

Reputation: 5448

If you're using Maven, you can add this to your pom.xml file. This will create an additional "fat" jar whenever you build with Maven. It goes in the 'build.plugins' section:

<build>
    <plugins>

        ...

        <!--
            The plugin below creates an additional, executable JAR with all dependencies
            included in it.
        -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>attached</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>${package}.FooBar</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        ...

    </plugins>
</build>

Upvotes: 1

Nivas
Nivas

Reputation: 18334

A jar has a manifest file called MANIFEST.MF, under the folder META-INF. This contains details about the jar file, the main class etc. Eclipse creates one by default, you can modify that or specify your own manifest file.

See http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html and Understanding the Default Manifest.

To add libraries to the classpath, you need to add the jar files' path to the manifest file, like this:

Class-Path: jar1-name jar2-name directory-name/jar3-name

See Adding Classes to the JAR File's Classpath

Upvotes: 1

Related Questions