How do I put all required JAR files in a library folder inside the final JAR file with Maven?

Mahmoud Saleh :

I am using Maven in my standalone application, and I want to package all the dependencies in my JAR file inside a library folder, as mentioned in one of the answers here:

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

I want my final JAR file to have a library folder that contains the dependencies as JAR files, not like what the maven-shade-plugin that puts the dependencies in the form of folders like the Maven hierarchy in the .m2 folder.

Well, actually the current configuration does what I want, but I am having a problem with loading the JAR files when running the application. I can't load the classes.

Here's my configuration:

<plugins>

    <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}/classes/lib</outputDirectory>
                    <overWriteReleases>false</overWriteReleases>
                    <overWriteSnapshots>false</overWriteSnapshots>
                    <overWriteIfNewer>true</overWriteIfNewer>
                </configuration>
            </execution>
        </executions>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <mainClass>com.myapp.MainClass</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>install</id>
                <phase>install</phase>
                <goals>
                    <goal>sources</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <configuration>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>

</plugins>

The project runs fine from Eclipse, and the JAR files are put in the library folder inside my final JAR file as I want, but when running the final JAR file from the target folder I always get ClassNotFoundException:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        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)
Could not find the main class: com.myapp.MainClass. Program will exit.

How can I fix this exception?

Mahmoud Saleh :

following this link:

How To: Eclipse Maven install build jar with dependencies

i found out that this is not workable solution because the class loader doesn't load jars from within jars, so i think that i will unpack the dependencies inside the jar.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I put all dependencies into one folder inside a JAR file with Maven?

From Dev

maven: how to build the dependency from source.jar to jar files and put the jar file as it dependency?

From Dev

How to retrieve all files contained in a folder inside a JAR?

From Dev

How to package a Maven project in zip file with jar files inside

From Dev

How to use a jar file as a library in a maven project

From Dev

How to access file inside maven shaded jar

From Java

How do I access a config file inside the jar?

From Dev

Maven: How to package local jar into final jar?

From Java

Where do I put the .tld file so that the resulting JAR file built with maven2 is properly packaged?

From Java

How to list the files inside a JAR file?

From Dev

Store a file inside a jar library

From Java

How to put all dependencies in separate folder for runnable jar?

From Java

Where do you put the .jar file for an external library in java

From Dev

.gitignore - Ignore all files in target folder except 1 JAR file

From Dev

How to have a final jar with the dependency inside that jar in the jar format?

From Dev

How can I delete a particular if statement in shell script file and do the same for all files of all folders inside a single folder

From Dev

How to list only folders inside jar using jar cmmand similar to jar -tvf will give all files and directory?

From Java

How do I convert a "jar:file" URI to the path of Jar file?

From Java

How can I get a resource "Folder" from inside my jar File?

From Java

How to load a library that depends on another library, all from a jar file

From Java

Get all files inside directory's in jar file in java

From Dev

How do I delete all files in an Azure File Storage folder?

From Dev

How do I replace all files in a folder with one file?

From Java

How can I add files to a Jar file?

From Dev

How to create jar file without java classes but with a folder using maven

From Java

How do I attach properties files to a jar?

From Dev

I built a library with Maven. How do I find its jar so that I can use it without Maven?

From Java

How do I add a folder full of Jar files to my classpath in Eclipse?

From Dev

How do I create a Netbeans style Jar with all dependencies in a lib folder?

Related Related

  1. 1

    How can I put all dependencies into one folder inside a JAR file with Maven?

  2. 2

    maven: how to build the dependency from source.jar to jar files and put the jar file as it dependency?

  3. 3

    How to retrieve all files contained in a folder inside a JAR?

  4. 4

    How to package a Maven project in zip file with jar files inside

  5. 5

    How to use a jar file as a library in a maven project

  6. 6

    How to access file inside maven shaded jar

  7. 7

    How do I access a config file inside the jar?

  8. 8

    Maven: How to package local jar into final jar?

  9. 9

    Where do I put the .tld file so that the resulting JAR file built with maven2 is properly packaged?

  10. 10

    How to list the files inside a JAR file?

  11. 11

    Store a file inside a jar library

  12. 12

    How to put all dependencies in separate folder for runnable jar?

  13. 13

    Where do you put the .jar file for an external library in java

  14. 14

    .gitignore - Ignore all files in target folder except 1 JAR file

  15. 15

    How to have a final jar with the dependency inside that jar in the jar format?

  16. 16

    How can I delete a particular if statement in shell script file and do the same for all files of all folders inside a single folder

  17. 17

    How to list only folders inside jar using jar cmmand similar to jar -tvf will give all files and directory?

  18. 18

    How do I convert a "jar:file" URI to the path of Jar file?

  19. 19

    How can I get a resource "Folder" from inside my jar File?

  20. 20

    How to load a library that depends on another library, all from a jar file

  21. 21

    Get all files inside directory's in jar file in java

  22. 22

    How do I delete all files in an Azure File Storage folder?

  23. 23

    How do I replace all files in a folder with one file?

  24. 24

    How can I add files to a Jar file?

  25. 25

    How to create jar file without java classes but with a folder using maven

  26. 26

    How do I attach properties files to a jar?

  27. 27

    I built a library with Maven. How do I find its jar so that I can use it without Maven?

  28. 28

    How do I add a folder full of Jar files to my classpath in Eclipse?

  29. 29

    How do I create a Netbeans style Jar with all dependencies in a lib folder?

HotTag

Archive