Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes IDEs, Version Control and other tools and the fly likes yet another question regarding creating a runnable jar with Eclipse/Maven Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "yet another question regarding creating a runnable jar with Eclipse/Maven" Watch "yet another question regarding creating a runnable jar with Eclipse/Maven" New topic

yet another question regarding creating a runnable jar with Eclipse/Maven

Stuart Rogers
Ranch Hand

Joined: Oct 02, 2008
Posts: 133
So I have a Maven project named myProj in Eclipse.
Amongst all the other files in the project there exists myProj/src/main/resources/ApplicationContext.xml .
There also exists class myProj/src/main/java/foo/model/TestQueries that contains the main() method.

Within Eclipse I can run TestQueries just fine. So I have a Run Configuration named TestQueriesRunC.

I can successfully create a runnable jar using File->Export->Runnable Jar -> Launch Configuration (select TestQueriesRunC) --LibraryHandling (extract required libraries into generated JAR)

Examining the resulting myProj.jar shows it contains a META-INF/MANIFEST.MF file

and also resources/ApplicationContext.xml

So far so good.

But attempted to execute this from the command line

fails with the following:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document
from class path resource [ApplicationContext.xml]; nested exception is class path resource
[ApplicationContext.xml] cannot be opened because it does not exist

Well it obviously does, so there's some little tweak I need to do when creating the runnable jar but I see no place in the process to put
such a tweak. Or is Eclipse' create-runnable-jar process known to be broken when dealing with Maven projects and I need to use a
different process (and if so, what is this new process?) .

An alternate plan could be to use Maven to create the runnable jar using this in the pom:

which looks promising. But I'd like to be able to get Eclipse to do it on its own.

As always, all suggestions, comments, constructive critisms and especially examples are all welcome.


Still-learning Steve

Peter Johnson

Joined: May 14, 2008
Posts: 5812

Since you are using maven anyway, I would go with using the Assembly plugin to create the runtime JAR (like you showed in your post). Then when you need to create the JAR, in Eclipse right-click the project, choose Run As and then choose "mvn package".

It is better to have only one way to build things, and not two ways. And having a build that can be performed from a command line is much preferred over having one that requires someone to click buttons in an IDE. By making your build in Maven, and having Eclipse invoke that, you are getting the best of both.

JBoss In Action
Stuart Rogers
Ranch Hand

Joined: Oct 02, 2008
Posts: 133
Plan B seems to be working ( I get a different error now but the context.xml is getting read now and the main class starts executing) so I guess I'll run with that.
But I'd still like an answer for Plan A - I managed to get Eclipse' way to work a year ago, long before I incorporated Maven into my Eclipse and Spring into my app. And whatever notes I took are long gone.

Oh well, thanks for your reply!


Still-learning Steve
Stuart Rogers
Ranch Hand

Joined: Oct 02, 2008
Posts: 133
here is my complete solution for having Maven create a runnable jar for a project that uses the Spring framework.

maven-assembly-plugin doesn't work for projects using the Spring framework because each of Spring's jars contain a spring.handlers, spring.schemas and perhaps a spring.tooling file and all these files wind up being packaged into the META-INF directory. So now you have a directory containing a bunch of files with the same names - not good! - for when you try to execute the jar all these files get loaded IN SEQUENCE and only the *LAST* occurance is used. This is why you get the same "unable to locate Spring NamespaceHandler" error but pointing to DIFFERENT namespaces for different projects, depending on the order of namespaces listed in the projects' context files.

So the solution is to merge the contents of these files so you wind up with only one spring.handlers, one spring.schemas and one spring.tooling file in the META-INF directory. Other posts on the 'net tell you to use maven-assembly-plugin then merge these manually - ugh! maven-shade-plugin is tailor-made for handling this exact situation AND will let you set the main-class and class-path in the file as well - everything you need for "mvn package" to create a runnable jar when you enter "java -jar myProj.jar" on the command line.

Hope this helps others,


Still-learning Steve
wood burning stoves
subject: yet another question regarding creating a runnable jar with Eclipse/Maven
Similar Threads
Using Maven using own folder structure
File encoding ASCII & UTF8 && ISO
jars in the custom ear not getting referenced at compile time
Build problem with maven and eclipse web project
Wagon-ssh with Maven 2.2