2

I am trying to convert a server application to a Maven project, and export it as a .jar using Maven. After importing the project into eclipse and converting it using the Maven plugin, I got a POM file. After running mvn package there were many errors involving .jar library dependencies within the project.

I added these dependencies into the POM, and added a plugin that allows for the export of a runnable .jar file. My POM file is below:

<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>160422_v3_Metadecoder</groupId>
  <artifactId>160422_v3_Metadecoder</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
 <plugin>
      <!-- Build an executable JAR -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.0.2</version>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>com.class.ws.classServer</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
    </plugins>
  </build>

  <dependencies>
  <dependency>
  <groupId>gson-2.6.2</groupId>
  <artifactId>gson-2.6.2</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\gson-2.6.2.jar</systemPath>
</dependency>
  <dependency>
  <groupId>classDecLib</groupId>
  <artifactId>classDecLib</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\classDecLib.jar</systemPath>
</dependency>
<dependency>
  <groupId>logging-interceptor-2.7.5</groupId>
  <artifactId>logging-interceptor-2.7.5</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\logging-interceptor-2.7.5.jar</systemPath>
</dependency>
<dependency>
  <groupId>javaClientAPI-1302</groupId>
  <artifactId>javaClientAPI-1302</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\javaClientAPI-1302.jar</systemPath>
</dependency>
<dependency>
  <groupId>javaClientAPI-2202</groupId>
  <artifactId>javaClientAPI-2202</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\javaClientAPI-2202.jar</systemPath>
</dependency>
<dependency>
  <groupId>joda-time-2.9.3</groupId>
  <artifactId>joda-time-2.9.3</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\joda-time-2.9.3.jar</systemPath>
</dependency>
<dependency>
  <groupId>okhttp-2.7.5</groupId>
  <artifactId>okhttp-2.7.5</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\okhttp-2.7.5.jar</systemPath>
</dependency>
<dependency>
  <groupId>swagger-annotations-1.5.9</groupId>
  <artifactId>swagger-annotations-1.5.9</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\swagger-annotations-1.5.9.jar</systemPath>
</dependency>
<dependency>
  <groupId>cdi-websocket-9.3.6.v20151106</groupId>
  <artifactId>cdi-websocket-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\cdi-websocket-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>servlet-api-3.1</groupId>
  <artifactId>servlet-api-3.1</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\servlet-api-3.1.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-xml-9.3.6.v20151106</groupId>
  <artifactId>jetty-xml-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-xml-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-webapp-9.3.6.v20151106</groupId>
  <artifactId>jetty-webapp-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-webapp-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-util-9.3.6.v20151106</groupId>
  <artifactId>jetty-util-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-util-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-servlet-9.3.6.v20151106</groupId>
  <artifactId>jetty-servlet-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-servlet-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-server-9.3.6.v20151106</groupId>
  <artifactId>jetty-server-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-server-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-security-9.3.6.v20151106</groupId>
  <artifactId>jetty-security-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-security-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-schemas-3.1</groupId>
  <artifactId>jetty-schemas-3.1</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-schemas-3.1.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-rewrite-9.3.6.v20151106</groupId>
  <artifactId>jetty-rewrite-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-rewrite-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-quickstart-9.3.6.v20151106</groupId>
  <artifactId>jetty-quickstart-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-quickstart-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-proxy-9.3.6.v20151106</groupId>
  <artifactId>jetty-proxy-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-proxy-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-plus-9.3.6.v20151106</groupId>
  <artifactId>jetty-plus-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-plus-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-nosql-9.3.6.v20151106</groupId>
  <artifactId>jetty-nosql-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-nosql-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-jndi-9.3.6.v20151106</groupId>
  <artifactId>jetty-jndi-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-jndi-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-jmx-9.3.6.v20151106</groupId>
  <artifactId>jetty-jmx-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-jmx-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-jaspi-9.3.6.v20151106</groupId>
  <artifactId>jetty-jaspi-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-jaspi-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-jaas-9.3.6.v20151106</groupId>
  <artifactId>jetty-jaas-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-jaas-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-io-9.3.6.v20151106</groupId>
  <artifactId>jetty-io-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-io-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-infinispan-9.3.6.v20151106</groupId>
  <artifactId>jetty-infinispan-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-infinispan-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-http-9.3.6.v20151106</groupId>
  <artifactId>jetty-http-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-http-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-deploy-9.3.6.v20151106</groupId>
  <artifactId>jetty-deploy-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-deploy-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-continuation-9.3.6.v20151106</groupId>
  <artifactId>jetty-continuation-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-continuation-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-client-9.3.6.v20151106</groupId>
  <artifactId>jetty-client-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-client-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-annotations-9.3.6.v20151106</groupId>
  <artifactId>jetty-annotations-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-annotations-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-alpn-server-9.3.6.v20151106</groupId>
  <artifactId>jetty-alpn-server-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-alpn-server-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>gcloud-session-manager-9.3.6.v20151106</groupId>
  <artifactId>gcloud-session-manager-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\gcloud-session-manager-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>cdi-servlet-9.3.6.v20151106</groupId>
  <artifactId>cdi-servlet-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\cdi-servlet-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>websocket-servlet-9.3.6.v20151106</groupId>
  <artifactId>websocket-servlet-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\websocket-servlet-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>websocket-server-9.3.6.v20151106</groupId>
  <artifactId>websocket-server-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\websocket-server-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>websocket-common-9.3.6.v20151106</groupId>
  <artifactId>websocket-common-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\websocket-common-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>websocket-client-9.3.6.v20151106</groupId>
  <artifactId>websocket-client-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\websocket-client-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>websocket-api-9.3.6.v20151106</groupId>
  <artifactId>websocket-api-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\websocket-api-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>javax.websocket-api-1.0</groupId>
  <artifactId>javax.websocket-api-1.0</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\javax.websocket-api-1.0.jar</systemPath>
</dependency>
<dependency>
  <groupId>javax-websocket-server-impl-9.3.6.v20151106</groupId>
  <artifactId>javax-websocket-server-impl-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\javax-websocket-server-impl-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>javax-websocket-client-impl-9.3.6.v20151106</groupId>
  <artifactId>javax-websocket-client-impl-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\javax-websocket-client-impl-9.3.6.v20151106.jar</systemPath>
</dependency>
  </dependencies>
</project>

The project builds properly but the .jar that comes out gives the following error when i run java -jar example.jar:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jetty/server/Handler
        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.eclipse.jetty.server.Handler
        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

Any advice on what could be causing this would be much appreciated.

edit: I changed my plugin to this:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <!-- get all project dependencies -->
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <!-- MainClass in mainfest make a executable jar -->
                    <archive>
                      <manifest>
                        <mainClass>com.class.ws.classServer</mainClass>
                      </manifest>
                    </archive>

                </configuration>
                <executions>
                  <execution>
                    <id>make-assembly</id>
                                        <!-- bind to the packaging phase -->
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                  </execution>
                </executions>
            </plugin>

in an attempt to get all the jar dependencies with the exported JAR, but this produces the same output.

JakeD
  • 407
  • 2
  • 7
  • 19
  • That's because the JAR only contains your code and none of the dependencies. You need to [shade](https://maven.apache.org/plugins/maven-shade-plugin/) or [assemble](http://maven.apache.org/plugins/maven-assembly-plugin/). – Seelenvirtuose Feb 24 '17 at 18:59
  • This looks promising thanks.. Could you give an example of how to do this? Is it just a run command or do I need to add these as plugins to my POM? I see assembly POM plugins that I need to specify each jar file, which would be unmanageable. – JakeD Feb 24 '17 at 19:02
  • Seems silly to be pulling those JARs from your project. That's what a central Maven repo should be for. The whole point should be to manage dependencies and versions outside your source. – duffymo Feb 24 '17 at 19:02
  • @duffymo what would be the reasonable choice here then? Create an additional maven repo? The reason that I'm converting the project to Maven is so I can get it building on Visual Studio Online, and then manage it and easily deploy to azure. – JakeD Feb 24 '17 at 19:05
  • Reasonable choice? You can pull from Maven Central in the public domain or a company repo that's private. None of this has anything to do with Visual Studio or Azure; it's just the proper use of Maven. You need a plug in that will create a fat JAR with all your dependencies in it. – duffymo Feb 24 '17 at 19:07
  • @duffymo check the updated question, I added a new plugin that seems promising but ultimately the same error occurs. – JakeD Feb 24 '17 at 19:09
  • You should open the JAR and verify yourself that everything you need is inside. My guess is that you count on this being in place because you run in Eclipse, but it's not there when you deploy outside. – duffymo Feb 24 '17 at 19:13
  • Good call.. the jars still arn't there, so there must be something wrong with the plugin in my pom file. – JakeD Feb 24 '17 at 19:18
  • 1
    Might want to check out the end of the older answer about this topic (it has 2 example projects for the different approaches to this end result): http://stackoverflow.com/a/37553587/775715 – Joakim Erdfelt Feb 24 '17 at 19:48
  • I think this is what you want: http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven – duffymo Feb 24 '17 at 20:04

1 Answers1

3

In the build part of your pom you need the following plugin to include the dependencies to your jar's classpath, they should be then located in the target/lib folder with this configuration):

              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Edit: Below is what the pom should look like for the section (of course you can modify the plugins' settings to your requirements):

<build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>org.my.main.class</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
Adonis
  • 4,670
  • 3
  • 37
  • 57
  • When you unzip your jar, what do you have in your manifest file? Normally you should get your main class, and a big classpath correct? – Adonis Feb 24 '17 at 19:52
  • Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Built-By: JakeD Created-By: Apache Maven 3.3.9 Build-Jdk: 1.8.0_121 .... looks like it's not getting the class. – JakeD Feb 24 '17 at 19:54
  • See my edit, it works fine for me, I get from the MANIFEST.MF: Manifest-Version: 1.0 Built-By: ado Class-Path: lib/jsoup-1.10.2.jar lib/log4j-api-2.8.jar lib/log4j-core- 2.8.jar Created-By: Apache Maven 3.3.9 Build-Jdk: 1.8.0_121 Main-Class: org.my.main.class – Adonis Feb 24 '17 at 20:04
  • Now it says could not load main class... the manifest does have the proper class path though. I basically took your section in the edit, modified the class name and removed the classpathPrefix. didn't work before removing this either – JakeD Feb 24 '17 at 20:17
  • You should let the classpathPrefix as it points to the directory your dependency should have been copied to (i.e. target/lib), and you will need them to run your program. Could you edit your original post with what you have in your MANIFEST.MF, your new pom.xml, and a listing of what's in target/lib directory? – Adonis Feb 24 '17 at 20:24
  • I'll do this but I know the problem already.. the .jar contains none of my java files anymore.. although the MANIFEST.MF is correct and the target/lib contains all the dependencies i need. – JakeD Feb 24 '17 at 20:39