1 Reply Latest reply on Jan 21, 2009 8:45 AM by Andreas Gies

    Reading file with relative URL from Bean

    Harshit Kapoor Newbie

      Hi,

       

      I have implemented a MessageExchangeListener to integrate my java class with servicemix.

      I have to access a property file from this class. The class file say MyListener is in package MyPackage.

      I created a folder called MyPropFolder in resources folder and inside MyPropFolder I have MyPropFile.txt

      The folder structure of source is as follows

      MyBean

              src

                      main

                              java

                                      MyPackage

                                              MyListener.java

                              resources

                                      MyPropFolder

                                              MyPropFile.txt

       

      When I create a service assembly and see inside the zip archive created the structure looks as follows:

      MyBean.zip

              MyPackage

                      MyListener.class

              MyPropFolder

                      MyPropFile.txt

       

      MyListener.class tries to access the property file as follows:

      BufferedReader in = new BufferedReader(new FileReader("./MyPropFolder/MyPropFolder.txt"));

       

      I get the following Exception

      java.io.FileNotFoundException: ..\..\MyPropFolder\MyPropFolder.txt (The system cannot find the path specified) ...

       

      I have tried each of the following Paths, however none of them work

       

      "MyPropFile.txt"

      "MyPropFolder/MyPropFile.txt"

      "./MyPropFolder/MyPropFile.txt"

      "../MyPropFolder/MyPropFile.txt"

      "./MyPropFile.txt"

      "./resources/MyPropFolder/MyPropFile.txt"

      "../resources/MyPropFolder/MyPropFile.txt"

      "../../resources/MyPropFolder/MyPropFile.txt"

       

      Maven Build has also succeeded. When I open assembly.zip, I see appropriate serviceunit .zip in it. And when I open serviceunit.zip I see MyFolder which has MyProp.txt

       

      Infact after deploying, when I have a look at the SERVICEMIX_HOME/data folder I see

      D:\installation\apache-servicemix-3.2.2\data\smx\service-assemblies\MyAssembly\version_15\sus\servicemix-bean\MyBean\MyFolder\MyPropFile.txt

       

      D:\installation\apache-servicemix-3.2.2\data\smx\service-assemblies\MyAssembly\version_15\sus\servicemix-bean\MyBean\MyPAckage\MyBean.class

       

                       

      What am I doing wrong? How do I read a file with relative URL?

        • 1. Re: Reading file with relative URL from Bean
          Andreas Gies Newbie

          Hello,

           

          well, relative file names are always tricky. They are referenced with respect to the current directory when the JVM was started. You can query that directory name by accessing the user.dir System property.

           

          This having said, I would suggest to revisit the code, make a base directory configurable in your application and then you can create file urls relative to that configured directory.

           

          Best regards

          Andreas