-1

I am trying to run jar with dependency built by maven.Dependency is installed in local repository and just added dependency to pom.xml.. it is getting compiled successfully but getting ClassNotFound Exception at runtime. I explored on internet but no luck

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xyz</groupId>
  <artifactId>XYZ</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Test</name>
  <url>http://maven.apache.org</url>
  <build>
        <resources>
            <resource>
              <directory>WEB-INF/classes/</directory>
            </resource>
        </resources> 
         <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>com.xyz.Abc</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    </plugins>
    </build>
    <repositories>
      <repository>
         <id>localrepository</id>
         <url>${user.home}/.m2/repository</url>
      </repository>
   </repositories>      
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency> 
    <dependency>    
        <groupId>com.abc</groupId>  
        <artifactId>jarname</artifactId>    
        <version>1.0</version>
        <scope>compile</scope>
    </dependency> 
  </dependencies>
</project>
Prasad
  • 1
  • 1
  • In the title you say `NoClassDefFoundException` but in the text you say `ClassNotFoundException`. Which one is it? They are not the same. Please add the Stacktrace to you question, or an excerpt with the relevant part, if it is very long. And also, how exactly are you building and executing the app? Check out https://stackoverflow.com/help/how-to-ask to learn what to include in your question. – anothernode Apr 23 '18 at 08:50
  • It is ClassNotFound Exception – Prasad Apr 23 '18 at 09:43
  • This might help [Java ClassNotFoundException with Maven Dependency ](https://stackoverflow.com/questions/12811392/java-classnotfoundexception-with-maven-dependency) – Gadhia Reema Apr 23 '18 at 10:25
  • @GadhiaReema I checked tht one ...not working – Prasad Apr 23 '18 at 11:47

1 Answers1

0

To create a jar that runs "anywhere", it is possible to create an executable jar:

How can I create an executable JAR with dependencies using Maven?

Otherwise, you need to be careful with the class path, because otherwise Java will not find your dependencies.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142