9 Replies Latest reply on Oct 2, 2008 11:39 AM by johndubchak

    Deployment classpath issue in EAR

    johndubchak

      Hi,

      I have an EAR file that is structured like this:

      EAR
      | -> META-INF
      | | -> application.xml
      | -> WAR
      | | -> META-INF
      | | -> MANIFEST.MF -> Class-path: config.xml
      | | -> WEB-INF -> lib/classes etc
      | -> depA.jar
      | -> depB.jar
      | -> config.xml

      The war contains a servlet that is loaded on startup. The servlet instantiates a class from depA.jar that uses a framework-type of class from depB.jar that (ideally) reads, and parses, the config.xml file.

      However, the deployment produces a FileNotFoundException indicating no such file or directory. Here is a snippet of the stacktrace:

      18:35:44,233 ERROR [FileUtils] java.io.FileNotFoundException: /config.xml (No such file or directory)
      18:35:44,235 ERROR [STDERR] org.dom4j.DocumentException: Error on line -1 of document : Premature end of file. Nested exception: Premature end of file.
      18:35:44,235 ERROR [STDERR] at org.dom4j.io.SAXReader.read(SAXReader.java:482)
      18:35:44,235 ERROR [STDERR] at org.dom4j.DocumentHelper.parseText(DocumentHelper.java:278)
      18:35:44,236 ERROR [STDERR] at com.foo.depA.util.ConfigAdapter.createConfiguration(ConfigAdapter.java:56)
      18:35:44,236 ERROR [STDERR] at com.foo.war.ui.app.server.BootstrapServlet.createConfig(Unknown Source)
      18:35:44,236 ERROR [STDERR] at com.foo.war.ui.app.server.BootstrapServlet.initializeBroker(Unknown Source)
      18:35:44,236 ERROR [STDERR] at com.foo.war.app.server.BootstrapServlet.init(Unknown Source)
      18:35:44,236 ERROR [STDERR] at javax.servlet.GenericServlet.init(GenericServlet.java:212)
      18:35:44,236 ERROR [STDERR] at com.foo.war.ui.app.server.BootstrapServlet.init(Unknown Source)
      18:35:44,236 ERROR [STDERR] at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
      18:35:44,237 ERROR [STDERR] at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
      18:35:44,237 ERROR [STDERR] at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4071)
      18:35:44,237 ERROR [STDERR] at org.apache.catalina.core.StandardContext.start(StandardContext.java:4375)
      18:35:44,237 ERROR [STDERR] at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:790)
      18:35:44,237 ERROR [STDERR] at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:770)

      I even included a Class-path entry in the MANIFEST.MF, for config.xml, in depA.jar and (for good measure) in depB.jar - still no luck.

      What have I messed up with this configuration?

      Thanks for any help,
      John

        • 1. Re: Deployment classpath issue in EAR
          jaikiran

           

          18:35:44,233 ERROR [FileUtils] java.io.FileNotFoundException: /config.xml (No such file or directory)


          Is FileUtils class, part of your application? I guess, its logging an incorrect exception. Because in the exception stacktrace i see this:

          18:35:44,235 ERROR [STDERR] org.dom4j.DocumentException: Error on line -1 of document : Premature end of file. Nested exception: Premature end of file.
          18:35:44,235 ERROR [STDERR] at org.dom4j.io.SAXReader.read(SAXReader.java:482)
          18:35:44,235 ERROR [STDERR] at org.dom4j.DocumentHelper.parseText(DocumentHelper.java:278)


          which makes me believe that the file was found and was being parsed.

          • 2. Re: Deployment classpath issue in EAR
            erasmomarciano

            your config.xml is propably not well-formed

            • 3. Re: Deployment classpath issue in EAR
              johndubchak

              Actually, when I read the contents and print them out using the FileUtils class I wrote:

              String str = FileUtils.newInstance().getFileContents("/config.xml");
              System.out.println("File Contents: " + str);

              Here is the result:

              05:16:59,789 INFO [STDOUT] File Contents:

              So, I am assuming that the dom4j error is merely a symptom of the larger problem of not finding the file config file on the classpath.

              Thanks,
              John

              • 4. Re: Deployment classpath issue in EAR
                johndubchak

                erasmomarciano,

                Thank you for the reply, however, that was one of the first things I verified and it is well-formed.

                Even if it wasn't well-formed, I'd be receiving an error to that effect and not a FileNotFoundException.

                John

                • 5. Re: Deployment classpath issue in EAR
                  peterj

                  What mechanism is FileUtils.getFileContents() using to load the xml file? Could you post the code?

                  Which version of JBossAS are you using? I seem to recall a discussion in the forums that in 5.0 the base directory of an EAR file is not added to the classpath. This could cause an issue if you use Classloader.getResource.

                  • 6. Re: Deployment classpath issue in EAR
                    johndubchak

                    Hi Peter,

                    Thanks for the reply. I've tried two different methods to access the config file: 1) using the FileUtils and 2) getClass().getClassLoader().getResourceAsStream()

                    Here is the relevant code from each:

                    1)

                    public String getFileContents(String fileName) {
                     StringBuffer contents = new StringBuffer();
                    
                     try {
                     BufferedReader br = new BufferedReader(new
                     InputStreamReader(new FileInputStream(fileName)));
                     String input = br.readLine();
                     while (input != null) {
                     contents.append(input);
                     input = br.readLine();
                     }
                     br.close();
                     } catch (Exception e) {
                     log.error(e);
                     }
                    
                     return contents.toString();
                    }


                    And then method 2:

                    InputStream is = getClass().getClassLoader().getResourceAsStream("/config.xml");


                    The InputStream is null. Also, I am using JBoss AS 4.2.3.

                    Thanks,
                    John

                    • 7. Re: Deployment classpath issue in EAR
                      jaikiran

                       

                      "johndubchak" wrote:


                      And then method 2:

                      InputStream is = getClass().getClassLoader().getResourceAsStream("/config.xml");


                      The InputStream is null. Also, I am using JBoss AS 4.2.3.



                      Remove the forward slash from the code. Try:

                      InputStream is = getClass().getClassLoader().getResourceAsStream("config.xml");





                      • 8. Re: Deployment classpath issue in EAR
                        peterj

                        Concerning this code:

                        BufferedReader br = new BufferedReader(new
                         InputStreamReader(new FileInputStream(fileName)));


                        If you passed /config.xml, I believe that it will attempt to read config.xml from the base directory of the file system. You could verify this by adding this line earlier:

                        System.out.println(new File(fileName).getAbsolutePath());


                        If you would have passed the name "config.xml" instead (no leading slash), it would look for the file in jboss_home/bin (which is the current working directory).



                        • 9. Re: Deployment classpath issue in EAR
                          johndubchak

                          Peter,

                          If you would have passed the name "config.xml" instead (no leading slash), it would look for the file in jboss_home/bin (which is the current working directory).


                          You are correct, it printed out the JBOSS_HOME/bin directory as the current directory - something I just learned, thanks.

                          By switching to the getResourceAsStream("config.xml") (without a prepended frontslash) resolved this issue for me.

                          Thank you, and everyone, that helped me.

                          John