This content has been marked as final. Show 9 replies
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.
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.
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.
I have to aply the solutions given by the 2 way that you said.
boolean correctLoad = ConfigurationManagement.loadConfiguracion(CONFIGURATION_FILE);
In both ways when I debug the application, the CONF_FILE, gives a NullPointerException.
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");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.