9 Replies Latest reply: Nov 4, 2010 10:13 AM by 802695 RSS

    Put the param-value property of web.xml relative instead absolute

    802695
      Hi,

      In my web.xml I have the <param-value> the absolute direction which points to the properties file. But this method has a problem when I run the aplication in other computer, because I have to change the direction to run the aplication again. For this reason I want to put the a realtive direction to the aplication, but I dont know how to do it.

      At the moment the web.xml strutcture is the following:

      ..............
      <init-param>
      <param-name>
           chart.properties</param-name>
      <param-value>
      E:\\Aplication\\web\\WEB-INF\\chart.properties
           </param-value>
      </init-param>

      ..................

      I would be very grateful if somebody could help me to solve this problem.

      Regards
        • 1. Re: Put the param-value property of web.xml relative instead absolute
          JoachimSauer
          Your best bet is probably to load the properties file as a resource using the class loading mechanism, because everything else is pretty much undefined/broken/implementation-dependent in a JEE environment.

          In other words: Use [url http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String)]Class.getResource() (or getResourceAsStream) to load the data and specify a path relative to your classpath (for example by putting the properties file into WEB-INF/classes.
          • 2. Re: Put the param-value property of web.xml relative instead absolute
            802695
            Hi Joachim,

            Following with this thread, I have used the method Class.class.getResourceAsStream(propertiesFile), and quit the piece of xml code I have put in the first post. But when I try to run the aplication the log gives me folowing error:


            FAIL - Deployed application at context path /CarritoCompra but context failed to start
            E:\CarritoCompra\nbproject\build-impl.xml:580: The module has not been deployed.

            I cannot run the application.

            Regards
            • 3. Re: Put the param-value property of web.xml relative instead absolute
              DrClap
              Then perhaps that code didn't load the file correctly. Have you considered looking in the server logs to find out exactly what went wrong? That would be far more practical than asking a group of strangers to guess what went wrong.
              • 4. Re: Put the param-value property of web.xml relative instead absolute
                802695
                Hi,

                I solved the last problem ( the module was not deployed), but I havent solved the problem yet, because I tried to apply get Class.getResource() or , getResourceAsStream, quitting the param-value property of the web.xml, but It doesn`t run.

                Any other alternative solution?

                Regards
                • 5. Re: Put the param-value property of web.xml relative instead absolute
                  EJP
                  You don't need an alternative solution. This is the solution. You need to get it working. It works. Clearly the name you used to get the resource doesn't agree with where it actually is on the disk. Check the Javadoc ...
                  • 6. Re: Put the param-value property of web.xml relative instead absolute
                    794117
                    There are always alternative solutions :-)
                    In this case the alternative solution would be to use the ServletContext method getRealPath() or getResourceAsStream().
                    That way you can load a resource relative to your web application, no matter where it is deployed.
                    It is pretty much the same solution, except the resource is specified relative to your web app, rather than your classpath.

                    It is a quite acceptable alternative to the Classpath method, and perhaps better in terms of using the servlet API rather than the standard java classpath.

                    cheers,
                    evnafets
                    • 7. Re: Put the param-value property of web.xml relative instead absolute
                      802695
                      Hi,

                      I have to aply the solutions given by the 2 way that you said.


                      First way:

                      URL CONF_FILE=Class.class.getResource("WEB-INF/chart.properties");
                      String CONFIGURATION_FILE=CONF_FILE.getFile();
                      boolean correctLoad=ConfigurationManagement.loadConfiguration(CONFIGURATION_FILE);


                      Second way:

                      InputStream CONF_FILE=Class.class.getResourceAsStream("WEB-INF/chart.properties");
                      String CONFIGURATION_FILE=CONF_FILE.toString();
                      boolean correctLoad = ConfigurationManagement.loadConfiguracion(CONFIGURATION_FILE);

                      In both ways when I debug the application, the CONF_FILE, gives a NullPointerException.

                      Regards.
                      • 8. Re: Put the param-value property of web.xml relative instead absolute
                        EJP
                        I have to aply the solutions given by the 2 way that you said.
                        Nobody recommended this code. It is all your own work.
                        URL CONF_FILE=Class.class.getResource("WEB-INF/chart.properties");
                        If that's null the next line will throw a NullPointerException. It is customary to check these things, rather than just assuming that your code is perfect, and that methods that are specified to return null if their arguments aren't correct will never do so, and being surprised to the point of bafflement when they do.
                        InputStream CONF_FILE=Class.class.getResourceAsStream("WEB-INF/chart.properties");
                        String CONFIGURATION_FILE=CONF_FILE.toString();
                        That doesn't even begin to make sense. What exactly are you expecting InputStream.toString() to yield? Whatever it is, it isn't something that appears in the Javadoc.
                        In both ways when I debug the application, the CONF_FILE, gives a NullPointerException.
                        I'm not surprised. You're still not making the effort I suggested in the first place to ensure that the file actually is where you're sayjng that it is. You're not even checking it in your code, let alone your deployment. And you've completely ignored the suggestion about getRealPath(). Instead you are just making things up that will never work.

                        I don't see the point of this method of working.
                        • 9. Re: Put the param-value property of web.xml relative instead absolute
                          802695
                          Hi again,

                          The question is solved, using the solution given by evnafets.

                          Regards.