My build path in Eclipse looks like this:
ProjectName
-- WEB-INF
-- classes
-- myClass.class
-- configs
-- myConfig.xml
My absolute path to the config currently looks like this:
C:\Development\Java\ProjectName\WEB-INF\configs\myConfig.xml
I’m using JAXB for the binding, and it is expecting a FileInputStream. The FileInputStream needs to be a stream for the XML config file. However, I can’t figure out how to get the FileInputStream for my config, and I keep getting a FileNotFoundException.
I want this config to be loaded in such a way that someone doesn’t have to hardcode the path to the config because I plan on releasing the project open source. I see a lot of examples where someone just hardcodes the full absolute path, but I need it to be something more flexible “like” this:
new FileInputStream(“/WEB-INF/configs/myConfig.xml”);
Thanks!
I’d recommend putting that myConfig.xml in the WEB-INF/classes directory instead and loading it via the class loader, since it’s in the classpath. Calling getResourceAsStream() on the servlet context will return an InputStream that you can use. It’s relative to the context root, so you can pick up that WAR and put it anywhere – your code will still work.