I believe that the “right” way that should do is getResourceAsStream(), but it just doesn't cut it.
My situation:
I have a Java web application that will eventually be deployed to several different web servers. GlassFish , Tomcat, and WildFly >. I am currently working on a mac with Netbeans, and I have access to WildFly and GlassFish . The project is stunned.
What I need to do is read and write two different Excel files. One of them serves as a "template", and one serves as an output file. The application previously ran offline, and I'm trying to turn it into a JSF website.
I need to get FileInputStreamfor my one of the methods and ultimately create an output file that will be sent to the user via http.
From what I'm compiling, there should be a suitable way to do this:
Xlsx file
/src/main/resources/ANALYSIS_template.xlsx
Location after compiling to WAR file :: WildFly
/usr/local/opt/wildfly-as/libexec/standalone/deployments/fleetForecast-1.0-SNAPSHOT.war/WEB-INF/classes/ANALYSIS_template.xlsx
Location once compiled into a WAR file :: GlassFish
~/devel/fleetforecast/target/fleetForecast-1.0-SNAPSHOT/WEB-INF/classes/ANALYSIS_template.xlsx
Java code
It seems to me that I may have managed to get the input stream to a file using the command:
InputStream is1 = getClass().getResourceAsStream("ANALYSIS_template.xlsx");
I also tried ("/ANALYSIS_template.xlsx")to behave similarly. When I try to read from this input stream
try {
is1.read();
} catch (IOException ex) {
Logger.getLogger(FleetBean.class.getName()).log(Level.SEVERE, null, ex);
}
I get it java.lang.NullPointerException.
Caused by: java.lang.NullPointerException
at org.mitre.fleetforecast.bean.FleetBean.<init>(FleetBean.java:57)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at java.lang.Class.newInstance(Class.java:433)
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:186)
... 76 more
, , .