2

Hi I am using followng maven dependencies. When I run the application in Eclipse, it is working fine. but when i deploy the application as jar file, it is throwing following error.

Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder
 at java.net.URLClassLoader.findClass(Unknown Source)

Following is my maven dependency file.

<dependencies>  
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey.client.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>${jersey.client.version}</version>
        </dependency>

        <!-- http://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>

    </dependencies>

    <properties>
        <jersey.client.version>2.21</jersey.client.version>
    </properties>

<dependencies>  

Any help on this.... I gone through following link, but it don't help me.

running standalone java executable in eclipse results in NoClassDefFound

Community
  • 1
  • 1
Hari Krishna
  • 3,658
  • 1
  • 36
  • 57
  • Which version of JAVA do you use ? Do both eclipse workspace and the standalone java use the same JDK ? – Ramachandran.A.G Aug 29 '16 at 05:26
  • I am using Java8 in both the work space and to run jar file – Hari Krishna Aug 29 '16 at 05:35
  • Assuming here that when packaging , the jar is not left out (you can quickly verify if that is the case by opening and verifying contents of the jar file). Also , if you are running the jar as a maven lifecycle, can you edit the post to include mvn dependency:tree as well – Ramachandran.A.G Aug 29 '16 at 05:40

2 Answers2

3

If you distribute only single jar (not fat-jar/uber-jar) you need to provide the classpath to it, that is all the library jars that are required to run it.

In your case it would be something along this lines:

java -jar my.jar -cp $HOME/.m2/repository/org/glassfish/jersey/core/jersey-client/2.21/jersey-client-2.21.jar

And after : you need to add all other dependencies that you have.

Another option is to use e.g. assembly plugin to build uber jar (jar that will contain all other jars, libraries and your code): http://maven.apache.org/plugins/maven-assembly-plugin/usage.html:

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
       </plugin>
    </plugins>
</build>

And then build the jar using: mvn clean package assembly:single, and see that now you have two jars inside target, the larger one is the uber jar that you can distribute.

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
  • If you should want to use the classpath approach, one way to enumerate all the dependencies is through `mvn dependency:build-classpath`. Details are here: http://maven.apache.org/plugins/maven-dependency-plugin/build-classpath-mojo.html – Daniel Aug 29 '16 at 06:00
0

Maybe You are missing the Maven dependency below in your pom.xml

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-client</artifactId>
    <version>3.0.2.Final</version>
</dependency>
David Jesus
  • 1,981
  • 2
  • 29
  • 34