3

I have a project which runs fine from within Eclipse. But when I export it to a runnable JAR with dependencies packed into it it won't run. The error, when running the JAR from console is:

EDIT (didnt run it with -jar before):

INFO: Loading XML bean definitions from class path resource [applicationContext-
framework.xml]
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOExc
eption parsing XML document from class path resource [applicationContext-framewo
rk.xml]; nested exception is java.io.FileNotFoundException: class path resource
[applicationContext-framework.xml] cannot be opened because it does not exist
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:349)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:310)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
        at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:113)
        at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:80)
        at org.springframework.context.support.AbstractRefreshableApplicationCon
text.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
        at org.springframework.context.support.AbstractApplicationContext.obtain
FreshBeanFactory(AbstractApplicationContext.java:422)
        at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:352)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:93)
        at com.horstmann.violet.UMLEditorApplication.getApplicationContext(UMLEd
itorApplication.java:111)
        at com.horstmann.violet.UMLEditorApplication.<init>(UMLEditorApplication
.java:94)
        at com.horstmann.violet.UMLEditorApplication.main(UMLEditorApplication.j
ava:84)
        ... 5 more
Caused by: java.io.FileNotFoundException: class path resource [applicationContex
t-framework.xml] cannot be opened because it does not exist
        at org.springframework.core.io.ClassPathResource.getInputStream(ClassPat
hResource.java:143)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:336)
        ... 20 more 

but the file is in resources/ from the JAR-root.

What am I missing or doing wrong?

Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102
  • @Cobra_Fast Please tell us something more. Make screenshots from your export configuration (wizards) and tell us about your project structure. Have you a exception for us? – Tim Jun 16 '11 at 12:22
  • As I said, it doesn't throw any errors. JAR generation and JAR execution works just fine, with the difference that the JAR exits immediately, where running from eclipse doesn't. Im using the same config for the JAR as for running from eclipse. – Cobra_Fast Jun 16 '11 at 12:25
  • 2
    @Cobra_Fast: try to run jar from command prompt. May be you can see some kinda error on that. – Harry Joy Jun 16 '11 at 12:26
  • Apparently it's trying to convert the JARs filename to a path inside the jar and fails to load that class. – Cobra_Fast Jun 16 '11 at 12:30
  • @Cobra_Fast: why you require to give JAR file name in code? – Harry Joy Jun 16 '11 at 12:31
  • I'm not seeing any code that would try to load classes like the filename (I'm trying to build an open source project from source). – Cobra_Fast Jun 16 '11 at 12:39
  • @cobra_fast: is this the only line in exception? Can you get the full exception stack trace? which class is not found? – Harry Joy Jun 16 '11 at 12:44
  • If my filename was 'build-123.jar' it would be looking for a class at path 'build-123/jar'. Updated question w/ more info. – Cobra_Fast Jun 16 '11 at 12:46
  • Sorry everyone. I was forgetting to run the JAR with the -jar option before, causing the weird error. – Cobra_Fast Jun 16 '11 at 13:42
  • I just found this too - Eclipse seems to put resource property files in one place internally and another place when exporting. It exports the resource property file foo.properties to "/resouces/foo.properties" but when run internally, it expects the resource path to be "/foo.properties". This is annoying. – Thomas Andrews Jan 11 '16 at 15:56

3 Answers3

6

Try to click on the option “Package required libraries into generated JAR”.

A JAR file is a regular compressed archive. Make sure that all the required libraries in your MANIFEST.MF file are present within the generated file.

Alf
  • 76
  • 2
5

Try to run the JAR from the command line - it will most likely display a useful error message.

There probably are some dependencies that eclipse doesn't know about or which aren't configured correctly within it, and are therefore missing from the JAR.

In general, it's not a good idea to rely on IDE functionality to produce deliverables - it depends on too many hidden factors and can't be automated for a build server. Any project that's beyond the experimental/toy stage should have an automated build via Ant or Maven.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • +1 for the last paragraph. One should most of time use ANT like mechanism to build project instead of relying on IDE. – Harry Joy Jun 16 '11 at 12:32
1

The message

Could not find the main class: violet-0.21.2-SVN.jar.  Program will exit.

indicates that you have gotten the Main-Class: MyPackage.MyClass line wrong in the Manifest when packaging together the jar file. You need to give the full name of the class with the main(String[] args) method.

See http://download.oracle.com/javase/tutorial/deployment/jar/appman.html for details.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347