-1

I am trying to create a jar file from my project in Eclipse IDE. I was trying to run mvn package on my pom.xml that looks like this:

<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>com.javacodegeeks.snippets.enterprise</groupId>
    <artifactId>springexample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>



    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.javacodegeeks.snippets.enterprise.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            </plugins>
            </build>        
            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                <version> 3.2.4.RELEASE</version>
                </dependency>
                <dependency>
                    <groupId>com.rapplogic</groupId>
                    <artifactId>xbee-api</artifactId>
                    <version> 0.9</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                    <version>3.2.4.RELEASE</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-orm</artifactId>
                    <version>3.2.4.RELEASE</version>
                </dependency>
                <dependency>
                    <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>jfreechart</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>jcommon</groupId>
            <artifactId>jcommon</artifactId>
            <version>0.9.5</version>
        </dependency>
        <dependency>
            <groupId>org.graphstream</groupId>
            <artifactId>gs-core</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.graphstream</groupId>
            <artifactId>gs-ui</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.graphstream</groupId>
            <artifactId>gs-algo</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.9.Final</version>
        </dependency>           
        <dependency>
            <groupId>org.rxtx</groupId>
            <artifactId>rxtx</artifactId>
            <version>2.1.7</version>
        </dependency>
        <dependency>
            <groupId>org.bidib.jbidib.org.qbang.rxtx</groupId>
            <artifactId>rxtxcomm</artifactId>
            <version>2.2</version>
        </dependency>
    </dependencies>
</project>

Afterwards, I have gotten a file MyProject-0.0.1-SNAPSHOT.jar in the target directory of the project. Double clicking on that jar in Eclipse gave me a pop-up Windows window saying "Java Virtual Machine Launcher - A java Exception has occured!". To find more about what a possible problem could be, I have copied that jar on the desktop and run java -jar MyProject-0.0.1-SNAPSHOT.jar from that. After that, I got a following error:

Error afer running jar from command line

Assuming from the error log, I tried to search if I have included org.hibernate.cfg.NamingStrategy in my Maven repository, and yes, I do have it there. After that I got stuck. Any ideas? Thanks.

MichalB
  • 3,281
  • 6
  • 32
  • 46
  • Your jar file does not contain the dependencies. Have a look at this [thread](http://stackoverflow.com/questions/21274151/apache-camel-packaging-an-executable-jar/21276127#21276127) for one way to create an "uber-jar" with all dependencies included. It shows you how to handle some resource clashes with the shader plugin. – Ralf Apr 03 '14 at 06:26
  • As mentioned by many you need to include additional classes. This can be done in several ways. For now the "put all classes from all jars together in my jar" approach is most likely what is best for you. – Thorbjørn Ravn Andersen Apr 03 '14 at 06:36

2 Answers2

0

You need to create an executable fat jar: one that contains your jar dependencies as well. See: Building a fat jar using maven

Community
  • 1
  • 1
Luciano
  • 8,552
  • 5
  • 32
  • 56
0

You have to include all the dependencies on the classpath that are required by your code.

You can naively do this with a shell script that manually adds everything to java -cp parameter, but this is brittle and a complete fail when something changes.

The simplest way to do this is to use the shade plugin to make an uber-jar that includes all the dependencies.

Note that this won't work correctly if any of the dependencies are cryptographically signed, unfortunately the signing stuff is in the MANIFEST file in such a way that it applies globally to the entire .jar and not individual classes or packages. You would have to manually remove the signatures from the uber jar.

Community
  • 1
  • 1