-1

I am trying to get a jar as stream from the below path, but it always throws null pointer error.

Below is my Servlet Code

response.setContentType("application/jar");

ServletContext ctx = getServletContext();

InputStream is = null;

try {
    ctx.getRealPath("/WEB-INF/jar/teamspace.jar");
    is = ctx.getResourceAsStream("/WEB-INF/jar/teamspace.jar");
    ///WEB-INF/pdf/order.pdf
}catch(Exception e){

    e.printStackTrace();

}
if(is==null){
    System.err.println("it is null");
}

Error is below

I have put the jar file in the below location

C:\Users\jacksons5\workspace\BeerApp\WebContent\WEB-INF\jar
SEVERE: Servlet.service() for servlet BeerServlet threw exception
java.lang.NullPointerException
    at com.beer.controller.BeerServlet.doPost(BeerServlet.java:87)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:745)

Hi Stdunbar,

I tried the solution provided in the link, but its not working, Still shows the same error.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub


//response.setContentType("text/html");

    System.out.println("It reached the server");

    //PrintWriter out = response.getWriter();

    String c = request.getParameter("color");


    response.setContentType("application/jar");

    ServletContext ctx = getServletContext();

    InputStream is = null;

    try {
        //ctx.getRealPath("/WEB-INF/jar/teamspace.jar");
     is = ctx.getResourceAsStream("/WEB-INF/teamspace.jar");
//   InputStream inStream = class.getClass().getClassLoader().getResourceAsStream("Web-INF/web.xml");

    // is = class.getClass().getClassLoader().getResourceAsStream("Web-INF/web.xml");
    ///WEB-INF/pdf/order.pdf
    }catch(Exception e){

        e.printStackTrace();

    }

When running this code, im still getting the same error !!!

JimHawkins
  • 4,843
  • 8
  • 35
  • 55
Jackson2489
  • 11
  • 3
  • 10
  • You're making it too difficult - see [this answer](http://stackoverflow.com/questions/1108434/howto-load-a-resource-from-web-inf-directory-of-a-web-archive). – stdunbar Sep 05 '16 at 23:02
  • HI Stdunbar, I tried the link you provided, it did not work unfortunately, I have edited my original post after trying the solution provided in the link Please help me in resolving this – Jackson2489 Sep 06 '16 at 00:32
  • you've written that you try to access `/WEB-INF/jar/teamspace.jar`. In response to @stdunbar you wrote `/WEB-INF/teamspace.jar`. Really? – JimHawkins Sep 06 '16 at 09:52

2 Answers2

0

I'm not really sure what is going on in your environment - your code looks good. I created a simple webapp example that takes most of your code and serves it the way you're trying to do. I've validated it on Tomcat 8.0.x and 8.5.x. It does use Maven but hopefully it will get you where you need. It is saved here - it is source only, about 6K in size, and includes a sample jar for serving. Again, your code looks correct but something else must be not working.

To make debugging a bit easier at first you could change the doPost to a doGet so that it can be tested in a browser.

stdunbar
  • 16,263
  • 11
  • 31
  • 53
0

Actually, based on the previous posts I tried with both codes, one by putting in web-inf and other by putting inside a jar folder. But both did not work.

Hi Stunbar,

Thanks very much for giving a sample code, I tried it in my workspace. When i run the code, the browser is asking me to download the file,but it actually downloads 0 kb file.

I mean the input stream still returns null.

The reason behind the pop up asking to download the jar file is because of below line in the code

response.setHeader("Content-Disposition", "filename=\"aws-java-sdk-1.11.21.jar\"");

Even i tried changing to get to see from the browser if it shows any error on not in the developer tool, it is not showing any errors.

it is just downloading the empty file without issues.

    package com.beer.controller;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//@WebServlet(urlPatterns="/mmmmbeer")
public class BeerServlet extends HttpServlet {



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("It reached the server");

        response.setContentType("application/jar");

        ServletContext ctx = getServletContext();

        try (InputStream inputStream = ctx.getResourceAsStream("/WEB-INF/jar/aws-java-sdk-1.11.21.jar") ) {

            System.out.println( "input stream is " + (inputStream == null ? "null" : "not null"));

            response.setHeader("Content-Disposition", "filename=\"aws-java-sdk-1.11.21.jar\"");

            byte[] buffer = new byte[1024];

            for (int length = 0; (length = inputStream.read(buffer)) > 0;) {
                response.getOutputStream().write(buffer, 0, length);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }








}

In the sysout in your code, i can see input stream as null in the console.

I am running tomcat V6.0 and in Windows 8.

My local workspace is kept under

C:\Users\jacksons5\workspace\BeerApp

I have also attached my workspace below

enter link description here

Not sure what is the problem, Please advise !!!

Jackson2489
  • 11
  • 3
  • 10