2

I am trying to run a jar file via cmd line that uses Spring and a spring xml configuration file.

The cmd line call is similar to:

java -cp lib/MyJar.jar my.package.MyClass

The error I get is:

Caused by: java.io.FileNotFoundException: class path resource 
   [myPath/mySpringCfg.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)

My manifest classpath is similar to:

Class-Path: 3rdPartyJar1.jar 3rdPartyJar2.jar ./myPath/

The call that loads the file equates to:

context = new ClassPathXmlApplicationContext("myPath/mySpringCfg.xml");

Is there a way to correctly pull in XML files in the classpath so that Spring will work as expected? It seems like the classpath docs only talk about archive files and folders.

Thanks!

UPDATE

It seems to run fine when I switch over to FileSystemXmlApplicationContext. I guess the ClassPathXmlApplicationContext cannot be used from command-line

user973479
  • 1,629
  • 5
  • 26
  • 48
  • 1
    Incorrect assumption - ClassPathXmlApplicationContext works fine from the command line. – RonK Apr 09 '13 at 18:41

2 Answers2

2

Your reference to the XML is myPath/mySpringCfg.xml - this means that myPath has to be in the classpath.

Change your manifest to be:

Class-Path: 3rdPartyJar1.jar 3rdPartyJar2.jar ./

This way myPath will be a part of the classpath and not just its contents.

Note: The application configuration XML is a part of your application's code, don't mistake it for a configuration.

If you want configuration - put it outside in a properties file and use place-holders in your XML configuration file.

Update:

I think the root cause of your problem is in the code (I didn't test it though) - try this instead:

context = new ClassPathXmlApplicationContext("/myPath/mySpringCfg.xml");

The difference is in the '/' before 'myPath'

RonK
  • 9,472
  • 8
  • 51
  • 87
  • Unfortunately that did not work. It must be something beyond that. Is the manifest the proper place to reference XML files? – user973479 Apr 09 '13 at 17:50
  • An XML file is just like any other resource - and manifests have nothing to do with that. It is a matter of classpath and your solution should work. What is your directory structure? – RonK Apr 09 '13 at 17:52
  • The bat file is in the root dir. The lib folder contains the jar in question and is a child folder of the root. The myPath folder is also a child of the root. I have also tried modifying the manifest to go one level out (../), but no luck. I can even pass in an absolute path that passes with new File(thePath).exists(), but I still get that error. – user973479 Apr 09 '13 at 17:58
  • I'm sorry to say this but "it works on my computer" :). The references in the manifest should be relative to the location of the jar, but absolute should work as well – RonK Apr 09 '13 at 18:30
  • Try looking at this blog post, it might help you: http://todayguesswhat.blogspot.in/2011/03/jar-manifestmf-class-path-referencing.html?m=1 – RonK Apr 09 '13 at 19:17
0

I am not aware of the architecture of your project, but why not place your xml configuration file into your project jar?

P. Lalonde
  • 694
  • 1
  • 7
  • 17