1

so i created an standalone app and it generates a jar. When I run it from Eclipse it works, "Run As" -> java application.

When I try to run it from console I started to get problems from swt, so I added to my manifest swt.jar and I started to get problems with my next dependency on my pom.

Here is an example of my pom.xml with swt.jar

Manifest-Version: 1.0
Main-Class: mapper.osm.main.Main
Class-Path: swt.jar

I downloaded swt and copied it on the same folder of my jar. But I dont want to add all my libraries manually to my manifest. It feels like there should be an easier, more maven way to do this.

This is the error I got after adding swt

$ java -jar mapper.jar
java.lang.NoClassDefFoundError: org/opengis/geometry/coordinate/Position
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.opengis.geometry.coordinate.Position
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main"

Before it was exactly the same but with some swt graphical libraries. This is my pom.xml

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

    <groupId>mapper</groupId>
    <artifactId>mapper.osm</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mapper,osm</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <geotools.version>13.2</geotools.version>
    </properties>

    <dependencies>
        <!-- Eclipse's JFace extensions -->
        <dependency>
            <groupId>org.eclipse.ui</groupId>
            <artifactId>org.eclipse.ui.workbench</artifactId>
            <version>3.7.1.v20120104-1859</version>
        </dependency>
        <!-- SWT -->
        <dependency>
            <groupId>org.eclipse.swt</groupId>
            <artifactId>${swt.artifactId}</artifactId>
            <version>4.4</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-swing</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>com.vividsolutions</groupId>
            <artifactId>jts</artifactId>
            <version>1.12</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <!-- RXTX -->
        <dependency>
            <groupId>org.bidib.jbidib.org.qbang.rxtx</groupId>
            <artifactId>rxtxcomm</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.8</version>
        </dependency>

        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.11.0</version>
        </dependency>

        <!-- Instalador izpack -->
        <dependency>
            <groupId>org.codehaus.izpack</groupId>
            <artifactId>izpack-maven-plugin</artifactId>
            <version>5.0.6</version>
            <scope>provided</scope>
        </dependency>


    </dependencies>

    <repositories>
        <repository>
            <id>swt-repo</id>
            <url>https://swt-repo.googlecode.com/svn/repo/</url>
        </repository>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net repository</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>osgeo</id>
            <name>Open Source Geospatial Foundation Repository</name>
            <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
    </repositories>
    <build>
        <finalName>mapper</finalName>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>mapper.osm.main.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.zenjava</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <mainClass>org.korecky.myjavafx.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>unix-amd64</id>
            <activation>
                <os>
                    <family>unix</family>
                    <arch>amd64</arch>
                </os>
            </activation>
            <properties>
                <swt.artifactId>org.eclipse.swt.gtk.linux.x86_64</swt.artifactId>
            </properties>
        </profile>
        <profile>
            <id>windows-x86</id>
            <activation>
                <os>
                    <family>windows</family>
                    <arch>x86</arch>
                </os>
            </activation>
            <properties>
                <swt.artifactId>org.eclipse.swt.win32.win32.x86</swt.artifactId>
            </properties>
        </profile>
        <profile>
            <id>windows64</id>
            <activation>
                <os>
                    <family>dos</family>
                    <arch>amd64</arch>
                </os>
            </activation>
            <properties>
                <swt.artifactId>org.eclipse.swt.win32.win32.x86_64</swt.artifactId>
            </properties>
        </profile>
        <profile>
            <id>windows-x86_64</id>
            <activation>
                <os>
                    <family>windows</family>
                    <arch>x86_64</arch>
                </os>
            </activation>
            <properties>
                <swt.artifactId>org.eclipse.swt.win32.win32.x86_64</swt.artifactId>
            </properties>
        </profile>
        <profile>
            <id>macosx-x86</id>
            <activation>
                <os>
                    <family>mac</family>
                    <arch>x86</arch>
                </os>
            </activation>
            <properties>
                <swt.artifactId>org.eclipse.swt.cocoa.macosx</swt.artifactId>
            </properties>
        </profile>
        <profile>
            <id>macosx-x86_64</id>
            <activation>
                <os>
                    <family>mac</family>
                    <arch>x86_64</arch>
                </os>
            </activation>
            <properties>
                <swt.artifactId>org.eclipse.swt.cocoa.macosx.x86_64</swt.artifactId>
            </properties>
        </profile>
    </profiles>
</project>
Juan Diego
  • 1,396
  • 4
  • 19
  • 52
  • This a classpath error. Need to include the classes/jar required in classpath. Have a look at http://stackoverflow.com/questions/22605831/how-to-resolve-this-error-caused-by-java-lang-classnotfoundexception – Balwinder Singh Jan 27 '16 at 23:30
  • I understand, but what I am asking is there a better way of doing this since I am using maven, and maven is pulling all the libraries from their respective repos. – Juan Diego Jan 27 '16 at 23:35
  • What is your current classpath? Also, I assume all the required class files are within the given jar – Balwinder Singh Jan 27 '16 at 23:37
  • 1
    [Here is a link](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) how you can create an executable jar. – Hendrik Jander Jan 27 '16 at 23:43

1 Answers1

1

Try using the Maven Dependency Plugin to copy the jars, and Maven JAR Plugin with the "addClasspath" node to add the dependencies to the manifest:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib</classpathPrefix>
                <mainClass>my.test.App</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

This will copy the jars to target/lib and add them to the manifest with the prefix lib/

ecarlos
  • 209
  • 1
  • 6