Java – add spring jar to class path


I have a netbeans java console project which uses Spring 3.0 ( which is a bunch of jars instead of a single jar).

when i try to execute it from the command line i get

 Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
Could not find the main class: com.aosprojects.transportservice.runner.InitialRunner. Program will exit.

How to fix this?

I have tried using -cp to specify the folder with all the jars in it like

java -cp libs/spring/* -jar dist/XXXXX.jar

Best Solution

If you're using maven to build your application, it runs in the netbeans IDE, but you'll need to define a plugin in your maven pom.xml in order to include all of the required .jars in your final .jar to make it executable.

                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">

This approach uses the maven shade plugin. You could also be using the maven assembly plugin, be prepared to face nasty bugs though :p


Related Question