2

Using Maven 3.1
Eclipse Helios

Aspekt:

Try to create a runable jar file using maven-jar/dependency-plugins.

Problem: After creating jar file and dependencies there are NoCLassDefFoundErrors when i try to start the jar file with command

  java -jar program.jar

But the Class/File is available in ./dependency-jars folder??? I Also tried following command:

  java -cp ./dependency-jars -jar program.jar

This didn't work, too.

Question:

Do you know why jvm can not find these classes? What is wrong?

pom.xml:

<build>
    <finalName>program</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>

                    <manifest>

                        <addClasspath>true</addClasspath>
                        <classpathPrefix>dependency-jars/</classpathPrefix>
                        <mainClass>de.test.MainCLass</mainClass>
                    </manifest>
                </archive>
                <outputDirectory>${package-target-folder}</outputDirectory>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.9.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>javadoc</goal>
                    </goals>
                    <phase>package</phase>
                    <id>create-javadoc</id>
                    <configuration>
                        <charset>UTF-8</charset>
                        <outputDirectory>${package-target-folder}/docs</outputDirectory>
                        <reportOutputDirectory>${package-target-folder}/docs</reportOutputDirectory>

                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${package-target-folder}/dependency-jars/</outputDirectory>
                    </configuration>
                </execution>
                <execution>


                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

[UPDATE]

The created MANIFEST.MF looks like

Manifest-Version: 1.0
Built-By: ahausden
Build-Jdk: 1.7.0_25
Class-Path: dependency-jars/spring-core-4.0.0.RELEASE.jar dependency-j
ars/commons-logging-1.1.1.jar dependency-jars/spring-jdbc-4.0.0.RELEA
SE.jar dependency-jars/spring-beans-4.0.0.RELEASE.jar dependency-jars
/spring-tx-4.0.0.RELEASE.jar dependency-jars/spring-oxm-4.0.0.RELEASE
.jar dependency-jars/spring-batch-core-2.2.0.RELEASE.jar dependency-j
ars/xstream-1.3.jar dependency-jars/xpp3_min-1.1.4c.jar dependency-ja
rs/jettison-1.1.jar dependency-jars/spring-aop-3.2.0.RELEASE.jar depe
ndency-jars/spring-context-3.2.0.RELEASE.jar dependency-jars/spring-e
xpression-3.2.0.RELEASE.jar dependency-jars/spring-batch-infrastructu
re-2.2.0.RELEASE.jar dependency-jars/spring-retry-1.0.2.RELEASE.jar d
ependency-jars/spring-batch-test-2.2.0.RELEASE.jar dependency-jars/co
mmons-io-1.4.jar dependency-jars/commons-dbcp-1.2.2.jar dependency-ja
rs/commons-pool-1.3.jar dependency-jars/commons-collections-3.2.jar d
ependency-jars/spring-test-4.0.0.RELEASE.jar dependency-jars/javax.in
ject-1.jar dependency-jars/slf4j-log4j12-1.6.1.jar dependency-jars/sl
f4j-api-1.6.1.jar dependency-jars/log4j-1.2.16.jar dependency-jars/sp
ring-orm-4.0.0.RELEASE.jar dependency-jars/aopalliance-1.0.jar depend
ency-jars/mariadb-java-client-1.1.1.jar dependency-jars/hibernate-cor
e-4.3.1.Final.jar dependency-jars/jboss-logging-3.1.3.GA.jar dependen
cy-jars/jboss-logging-annotations-1.2.0.Beta1.jar dependency-jars/jbo
ss-transaction-api_1.2_spec-1.0.0.Final.jar dependency-jars/dom4j-1.6
.1.jar dependency-jars/xml-apis-1.0.b2.jar dependency-jars/hibernate-
commons-annotations-4.0.4.Final.jar dependency-jars/hibernate-jpa-2.1
-api-1.0.0.Final.jar dependency-jars/javassist-3.18.1-GA.jar dependen
cy-jars/antlr-2.7.7.jar dependency-jars/jandex-1.1.0.Final.jar depend
ency-jars/hibernate-annotations-3.5.6-Final.jar dependency-jars/hiber
nate-commons-annotations-3.2.0.Final.jar dependency-jars/hibernate-jp
a-2.0-api-1.0.0.Final.jar dependency-jars/javassist-3.12.1.GA.jar dep
endency-jars/commons-cli-1.3-20140221.042048-103.jar
Created-By: Apache Maven 3.1.0
Main-Class: de.test.MainClass
Archiver-Version: Plexus Archiver

And the last "line" is as line separator. Seems that the MANIFEST.MF file looks a little bit corrupt, doesn't it?

JohannesDienst
  • 455
  • 4
  • 11
Dennis Ahaus
  • 969
  • 1
  • 8
  • 17

7 Answers7

2

Not sure what's going on, but this is what my pom looks like for building a runnable jar using shade:

 <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>false</filtering>
                <includes>
                    <include>schema.xsd</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <shadedArtifactAttached>true</shadedArtifactAttached>
                    <shadedClassifierName>stand-alone</shadedClassifierName>
                    <artifactSet>
                        <excludes>
                            <exclude>org.slf4j:slf4j-api:jar:</exclude>
                            <exclude>org.slf4j:slf4j-log4j12:jar:</exclude>
                            <exclude>org.slf4j:jcl-over-slf4j:jar:</exclude>
                            <exclude>commons-logging:commons-logging:jar:</exclude>
                            <exclude>commons-logging:commons-logging-api:jar:</exclude>
                        </excludes>
                    </artifactSet>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                    <finalName>MyFinalRunnableJarName</finalName>
                    <transformers>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>class.with.main.Method</mainClass>
                        </transformer>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                            <resource>META-INF/spring.handlers</resource>
                        </transformer>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                            <resource>META-INF/spring.schemas</resource>
                        </transformer>
                    </transformers>
                </configuration>
            </plugin>
        </plugins>
    </build>
Community
  • 1
  • 1
Mark Giaconia
  • 3,844
  • 5
  • 20
  • 42
  • Ok. Seems that using shade-plugin solves the problem. But now there is another problem with injecting org.hibernate.SessionFactory. I will try to find solution. For now source problem is solved. Thanks! – Dennis Ahaus Feb 28 '14 at 17:03
  • Thanks for the Shade suggestion, this worked for me in create a executable .jar with Spring dependencies. – Sudheer Palyam Mar 23 '16 at 07:10
1

Try to provide entry into manifest file:

Main-Class: YourClassWithMainMethod

And also see this post. So you can make it manually and see the difference.

If dependency-jars is a directory also try:

java -cp ./dependency-jars/* -jar program.jar

I still cannot post comments, so please show what are these NoCLassDefFoundErrors. And have you tried to do the same jar file manually from Eclipse and see the difference?

You have dependency dependency-jars/commons-cli-1.3-20140221.042048-103.jar in your manifest but you wrote you have SNAPSHOT version in directory. And there is only commons-cli:commons-cli:20040117.000000 in maven central.

@robermann please see this post (to work it should be in double quotes though)

Community
  • 1
  • 1
senyor
  • 332
  • 4
  • 9
1

The simplest solution is to use maven-assembly-plugin like this:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <!-- NOTE: We don't need a groupId specification because the group is
             org.apache.maven.plugins ...which is assumed by default.
         -->
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        [...]
</project>
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Ok, that was a good solution. but now i have another problem with spring that context can not find declarationi of "beans". But thanks for the first solution – Dennis Ahaus Feb 28 '14 at 15:57
0

Are you sure your java version, launched at command line, is equal or greater than that used by maven when compiling? try java -version

robermann
  • 1,722
  • 10
  • 19
  • Yes already checked. I have only one java version on computer and its exactly the same as i developed with – Dennis Ahaus Feb 28 '14 at 13:54
  • So, which is the class with NoCLassDefFoundErrors ? de.test.MainCLass? – robermann Feb 28 '14 at 13:56
  • Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Unknown Source) – Dennis Ahaus Feb 28 '14 at 14:04
  • But the jar file commons-cli-1.3-SNAPSHOT.jar (in which the missing class is inside) still remains in ./dependency-jars folder – Dennis Ahaus Feb 28 '14 at 14:05
  • It seems org/apache/commons/cli/ParseException is not in maven: http://search.maven.org/#search%7Cga%7C1%7Cc%3A%22org.apache.commons.cli.ParseException%22 – robermann Feb 28 '14 at 14:07
  • It is because in eclipse the Application works correct. And the depedency is maintented by maven – Dennis Ahaus Feb 28 '14 at 14:08
  • Could you give artifact & group id of your commons-cli-1.3-SNAPSHOT.jar? – robermann Feb 28 '14 at 14:11
  • commons-cli commons-cli 1.3-SNAPSHOT compile --- apache.snapshots http://repository.apache.org/snapshots central http://repo.maven.apache.org/maven2 – Dennis Ahaus Feb 28 '14 at 14:12
  • yes. Using jvm Java(TM) SE Runtime Environment (build 1.7.0_25-b17) Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode) and its excatly the same as i developed with. And the execution environment is the same on the same server – Dennis Ahaus Feb 28 '14 at 14:20
0

via command line, go to the parent dir of "dependency-jars" and run: java -jar program.jar

robermann
  • 1,722
  • 10
  • 19
0

Ok, i solved the second/updated Problem:

I first startet to use eclipse with maven plugin to create the runable jar file. This runs in the described problems.

After all i tried to use maven 3.1 from console application and after all it works. Seems that eclipse maven plugin has some problems.

Dennis Ahaus
  • 969
  • 1
  • 8
  • 17
0

Use this command to create jar file of any maven project.

mvn clean compile install

Ravi Thapa
  • 79
  • 1
  • 7