Creating an executable JAR file


References

Jar

public class HelloWorld {

    public static void main(final String[] args) {
        System.out.println("hello, world");
    }
}
$ javac HelloWorld.java

$ java HelloWorld
hello, world

$ cat HelloWorld.mf
Main-Class: HelloWorld

$ jar cfm HelloWorld.jar HelloWorld.mf HelloWorld.class

$ java -jar HelloWorld.jar
hello, world

$

Maven

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <!-- The Basics -->
  <groupId>test</groupId>
  <artifactId>hello-world</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <dependencies>
    <dependency>
      <groupId>org.imgscalr</groupId>
      <artifactId>imgscalr-lib</artifactId>
      <version>4.2</version>
    </dependency>
  </dependencies>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <!-- Build Settings -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <archive>
            <manifest>
              <mainClass>test.HelloWorld</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <archive>
            <manifest>
              <mainClass>test.HelloWorld</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <!-- More Project Information -->
  <name>hello-world</name>
  <url>http://maven.apache.org</url>

  <!-- Enrivonment Settings -->

</project>
$ ls -R src/main/java/
src/main/java/:
test/

src/main/java/test:
HelloWorld.java*

$ cat src/main/java/test/HelloWorld.java
package test;

public class HelloWorld {

    public static void main(final String[] args) {
        System.out.println( "hello, world");
    }
}

$ mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.613s
[INFO] Finished at: Sat Apr 27 00:27:33 KST 2013
[INFO] Final Memory: 15M/223M
[INFO] ------------------------------------------------------------------------

$ ls -l target/
archive-tmp/
classes/
hello-world-1.0-SNAPSHOT.jar*
hello-world-1.0-SNAPSHOT-jar-with-dependencies.jar*
maven-archiver/
surefire/

$ jar tf target/hello-world-1.0-SNAPSHOT.jar
META-INF/
META-INF/MANIFEST.MF
test/
test/HelloWorld.class
META-INF/maven/
META-INF/maven/test/
META-INF/maven/test/hello-world/
META-INF/maven/test/hello-world/pom.xml
META-INF/maven/test/hello-world/pom.properties

$ java -jar target/hello-world-1.0-SNAPSHOT.jar
hello, world

$ jar tf target/hello-world-1.0-SNAPSHOT-jar-with-dependencies.jar
META-INF/
META-INF/MANIFEST.MF
org/
org/imgscalr/
org/imgscalr/AsyncScalr$1.class
org/imgscalr/AsyncScalr$10.class
org/imgscalr/AsyncScalr$11.class
org/imgscalr/AsyncScalr$12.class
org/imgscalr/AsyncScalr$13.class
org/imgscalr/AsyncScalr$14.class
org/imgscalr/AsyncScalr$2.class
org/imgscalr/AsyncScalr$3.class
org/imgscalr/AsyncScalr$4.class
org/imgscalr/AsyncScalr$5.class
org/imgscalr/AsyncScalr$6.class
org/imgscalr/AsyncScalr$7.class
org/imgscalr/AsyncScalr$8.class
org/imgscalr/AsyncScalr$9.class
org/imgscalr/AsyncScalr$DefaultThreadFactory.class
org/imgscalr/AsyncScalr$ServerThreadFactory.class
org/imgscalr/AsyncScalr.class
org/imgscalr/Scalr$1.class
org/imgscalr/Scalr$Method.class
org/imgscalr/Scalr$Mode.class
org/imgscalr/Scalr$Rotation.class
org/imgscalr/Scalr.class
META-INF/maven/
META-INF/maven/org.imgscalr/
META-INF/maven/org.imgscalr/imgscalr-lib/
META-INF/maven/org.imgscalr/imgscalr-lib/pom.xml
META-INF/maven/org.imgscalr/imgscalr-lib/pom.properties
test/
test/HelloWorld.class
META-INF/maven/test/
META-INF/maven/test/hello-world/
META-INF/maven/test/hello-world/pom.xml
META-INF/maven/test/hello-world/pom.properties

$ java -jar target/hello-world-1.0-SNAPSHOT-jar-with-dependencies.jar
hello, world

$
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s