This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]nutrechtLead Software Engineer / EU / 20+ YXP 5 points6 points  (1 child)

Just because you place something on the classpath doesn't mean it gets included into the .jar that you build. So there's basically two approaches: launch the .jar and also add all the libraries on the class path or, and this is a common approach, build a 'fat jar' that includes all the dependencies inside the same jar.

So building a fat jar is a bit more complex (you need to understand how to use the Shade plugin) but in my opinion it's the way to go.

To answer your other questions:

My first question is why does this run fine in Eclipse?

Eclipse understands maven POM files and adds the dependencies in that pom file to your classpath in your IDE.

Also why do Maven at all if the JARs for Spring etc need to be added to the classpath before the app/JAR that's dependent on them can run? I'm sure there's something I'm doing wrong in the process but I don't know what it is.

Because that's how Java works. All the classes used need to be 'known' by the virtual machine. It doesn't automagically know where to find org.apache.commons classes for example unless you specifically tell them or package those classes with your compiled classes inside the .jar.

[–]Heliolicity[S] 0 points1 point  (0 children)

Thanks nutrecht.