Announcement Announcement Module
Collapse
No announcement yet.
ClassPathXmlApplicationContext XML location Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ClassPathXmlApplicationContext XML location

    Hi all

    I have a class which instantiates a ClassPathXmlApplicationContext and passes to it the name of the XML containing the DB configuration information.

    ApplicationContext ac = new ClassPathXmlApplicationContext("spring-jdbc.xml", Data.class);

    I have packaged up my application as a JAR file and wanted to run this from the command line e.g. java -jar test.jar...

    When I run this command the xml file cannot be found. Please could someone tell me where or how I should be specifying this so I can run the app?

    Thanks for any suggestions.

  • #2
    Would the experts recommend having the config file on the classpath or within the deployed JAR?

    Comment


    • #3
      Is "spring-jdbc.xml" at the root of your jar file, or available in the classpath that you provide to the command line? That's likely the only way that can work.

      Comment


      • #4
        I have just checked and its definitely in the JAR file at the root level.

        Comment


        • #5
          As a test, I suggest you write code in your main class that just uses "myclass.getClassLoader().getResource(filename )" to see if it can locate the file. If that doesn't work, then your problem has nothing to do with Spring.

          Comment


          • #6
            Ok so I have tried that and it looks like the XML file lives in the bin directory of my project. I want to be able to deploy the XML in the JAR so that someone else can run the code with the XML being picked up from the JAR if thats possible?

            Comment


            • #7
              Ok, so as I suspected, it's not in the root, but in the "bin" directory.

              You have a few choices, I believe.

              You could build an Ant script which assembles your jar however you want it, with the XML file in the root, most likely.

              I'm not sure if this would work, but you could instead put a META-INF/MANIFEST.MF file in the jar file that contains a "Class-Path:" property (verify that spelling) specifying the "bin" directory.

              You could also change the string reference in the ClassPathXmlApplicationContext call, giving it the "bin/" prefix.

              Comment


              • #8
                You seem to be having a packaging problem. In general JARs already container the compiled application so there is no point in having a dedicated bin/ folder. As a best practice, try to place the configurations into one place since they are easier to locate (the root being a common place).
                I would not resort to MANIFEST classpath entries since these are not portable (across different containers or in OSGi for example) which sort of defeats the idea of classpath. Just use reasonable conventions and the "built in" path and you should be fine.

                Comment

                Working...
                X