Garmin
Garmin

Reputation: 13

ClassLoader.getSystemClassLoader().getResourceAsStream() returns null In Servlet?

My question may be very vague. Let me elaborate on it. My project directory structure (in IntelliJ IDEA):

Server/--------------------------Module
      src/
          java/-----------------------------------classpath directory
              com.example.demo/
                               HelloServlet.java   ------a servlet respons
                               TestCode.java       ------a servlet that responds to network requests
                               Utils.java          ------a class to test by junit
          resources/------------------------------classpath directory
                    hello.txt------------------------------------------ "hello word" writed in this file

In Utils.java, I have the following code:

public class Utils {
    private static BufferedReader reader = null;
    static{
        try {
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("hello.txt");
            reader = new BufferedReader(new InputStreamReader(is));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static String getString() throws IOException {
        return reader.readLine();
    }
}

In TestCode.java:

public class TestCode {
    @Test
    public void test() throws IOException {
        System.out.println(Utils.getString());
    }
}

When I run the method test(), I get the following console output:

Hello World

It's OK in this way, But something wrong in servlet. In HelloServlet.java, I hava the following code:

@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "init() has invoked.";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        //NullPointerException throws
        message = Utils.getString();
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h1>" + message + "</h1>");
        out.println("</body></html>");
    }

    public void destroy() {
    }
}

When I call the Utils.getString() in doGet() method, and send a GET request by chrome.Then servlet will throw NullPointerException.

java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    at com.example.demo.Utils.<clinit>(Utils.java:17)
    at com.example.demo.HelloServlet.doGet(HelloServlet.java:20)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

BUT when I do some changes in Utils.java like below:

 private static BufferedReader reader = null;
    static{
        try {
            //use getClassLoader() instead of getSystemClassLoader()
            InputStream is = Utils.class.getClassLoader().getResourceAsStream("hello.txt");
            reader = new BufferedReader(new InputStreamReader(is));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static String getString() throws IOException {
        return reader.readLine();
    }

Then I send a GET request by chrome."Hello World" will be printed on the browser. When I refresh the website(or request the servlet again), the second question that null will be printed occurs. Why? Can someone answer my question? Thanks

Upvotes: 1

Views: 421

Answers (1)

Dmitry Ovchinnikov
Dmitry Ovchinnikov

Reputation: 718

Apache Tomcat 8 uses multiple classloaders: https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html.

If you want to make your code implementation independent, use the following snippet:

classLoader = request.getSession().getServletContext().getClassLoader()

You'll get a reliable way to obtain "the class loader of the web application represented by this ServletContext" according the JavaEE docs.

Upvotes: 1

Related Questions