8 Replies Latest reply on Jul 13, 2006 5:13 AM by ashudagr8

    File Path in EAR..!!!

    ashudagr8

      Hi Forum,
      Here is the situation:

      WAR file..
      |
      |..conf/config.xml
      |..web-inf/classes/toppackage/subpackage/read.class
      |
      |

      Now i have to read data from config.xml in read.class.How should i specify the path for the config.xml in code ?
      I have tried with in = new fileReader(./conf/config.xml) but it is not working..

      thanks in advance..


        • 1. Re: File Path in EAR..!!!
          jaikiran

          Try this out:

          InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("conf/config.xml");




          • 2. Re: File Path in EAR..!!!
            ashudagr8

            Thanks for reply..

            Atually i have a third party method that takes as input object of type
            java.io.Reader.
            So, after using FileReader in = new FileReader(filelocation) i have to pass
            the "in" object to this method...
            So i am afraid i cannot use getResourceAsStream()...

            How can i get through this problem?

            • 3. Re: File Path in EAR..!!!
              jaikiran

               

              third party method that takes as input object of type java.io.Reader


              This is what you can do:

              InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("conf/config.xml");
              
              InputStreamReader inputStreamReader = new InputStreamReader(inputStream) ;
              
              thidPartyLib.someAPI(inputStreamReader);


              Have a look at the InputStreamReader:

              http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStreamReader.html







              • 4. Re: File Path in EAR..!!!
                ashudagr8

                Thanks for Patience ..!!!
                but things are still not working... i am prompted with the exception like

                java.lang.NullPointerException
                at java.io.Reader.(Unknown Source)
                at java.io.InputStreamReader.(Unknown Source)

                Any help ???

                -Ashu

                • 5. Re: File Path in EAR..!!!
                  jaikiran

                   

                  java.lang.NullPointerException


                  Looks like the inputStream was null. Which means that the xml file was not found by the getResourceAsStream method. Try out the following, add a
                  System.out.println("Inputstream is: " + inputStream);

                  after the call to getResourceAsStream method. See if it prints null. Also, execute the following command and post the output:

                  jar -tf yourApp.ear


                  or if your application is a war

                  jar -tf yourApp.war




                  • 6. Re: File Path in EAR..!!!
                    ashudagr8

                    Here are the output's

                    InputStream = null

                    jar -tf myApp.war gives

                    META-INF/
                    META-INF/MANIFEST.MF
                    META-INF/application.xml
                    WEB-INF/
                    WEB-INF/jboss-web.xml
                    WEB-INF/web.xml
                    WEB-INF/classes/
                    WEB-INF/classes/sss/
                    WEB-INF/classes/sss/tt/
                    WEB-INF/classes/sss/tt/rt/
                    WEB-INF/classes/sss/tt/rt/FT.class
                    WEB-INF/classes/sss/tt/xd/
                    WEB-INF/classes/sss/tt/xd/P.class
                    WEB-INF/classes/sss/tt/xd/Tt.class
                    WEB-INF/classes/sss/tt/xd/TD.class
                    WEB-INF/classes/sss/tt/xd/TS.class
                    WEB-INF/conf/
                    WEB-INF/conf/tm.xml
                    WEB-INF/Data/
                    WEB-INF/Data/Fc.xml
                    WEB-INF/lib/
                    WEB-INF/lib/ct-1.0.1-xml.jar
                    WEB-INF/lib/cus-1.7.2.jar

                    I am trying to read /conf/tm.xml and /Data/Fc.xml in class TD.class..

                    Any Suggestions ?

                    • 7. Re: File Path in EAR..!!!
                      jaikiran

                       

                      WEB-INF/conf/tm.xml


                      This means that your tm.xml file is in youWar.war/WEB-INF/conf directory. So your code should look like:

                      InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("WEB-INF/conf/tm.xml");
                      
                      InputStreamReader inputStreamReader = new InputStreamReader(inputStream) ;
                      
                      thidPartyLib.someAPI(inputStreamReader);




                      • 8. Re: File Path in EAR..!!!
                        ashudagr8

                        Thanks a lot things are working now.. !!!