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.1 person found this helpful
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.
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.
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?
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 ...1 person found this helpful
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.
The question is solved, using the solution given by evnafets.