0

I want to create a report in pdf format by using servlet, .jasper file,but there is a exception:

HTTP Status 500 - Servlet execution threw an exception

--------------------------------------------------------------------------------

type Exception report

message Servlet execution threw an exception

description The server encountered an internal error that prevented it from fulfilling     this request.

exception 

javax.servlet.ServletException: Servlet execution threw an exception

root cause

java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JasperRunManager one.Patient.doGet(Patient.java:46) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.


servlet code is:

package one;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.HashMap;

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

//import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;

/**
 * Servlet implementation class Patient
 */
@WebServlet("/Patient")
public class Patient extends HttpServlet implements Servlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Patient() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        Connection con=DBCon.getConnection();
        InputStream inS=getServletContext().getResourceAsStream("/report-src/ganeshaji.jashper");

        OutputStream outS=response.getOutputStream();
        response.setContentType("application/pdf");

        try {
            JasperRunManager.runReportToPdfStream(inS, outS, new HashMap<String, Object>(),con);
            outS.flush();
            con.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            response.setContentType("text/html");
            e.printStackTrace();
        }   
        }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

pls. provide me solution

Alex K
  • 22,315
  • 19
  • 108
  • 236
Neeraj singh
  • 257
  • 1
  • 8
  • 18

2 Answers2

3

By looking into the root Cause

java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JasperRunManager 

it seems like you either miss the jasper jar or have the wrong version which doesn't support JasperRunManager.

Including the proper jar in the class path will solve your issue.

Please download these jars:

itext-4.2.0.jar 

itextpdf-5.3.4.jar 

jasperreports-4.0.1.jar 

jasperreports-fonts-4.0.0.jar 

and code something like:

try { 
FileInputStream fis = new FileInputStream(YOURREPORTPATH.jasper"); 
BufferedInputStream bufferedInputStream = new BufferedInputStream(fis); 
jasperReport = (JasperReport) JRLoader.loadObject(bufferedInputStream); 
} catch (Exception e2) {
 e2.printStackTrace(); 
}

and then after filling the parameters and the datasource create pdf like this:

JasperExportManager.exportReportToPdfFile(jasperPrint, OUTPUTFILENAME+".pdf");

Thanks...

Mr.777

SSC
  • 2,956
  • 3
  • 27
  • 43
  • sir i include this jasperreports-4.1.2.jar – Neeraj singh Feb 25 '13 at 12:40
  • Please download these jars: itext-4.2.0.jar itextpdf-5.3.4.jar jasperreports-4.0.1.jar jasperreports-fonts-4.0.0.jar and code something like: try { FileInputStream fis = new FileInputStream(YOURREPORTPATH.jasper"); BufferedInputStream bufferedInputStream = new BufferedInputStream(fis); jasperReport = (JasperReport) JRLoader.loadObject(bufferedInputStream); } catch (Exception e2) { e2.printStackTrace(); } and then after filling the parameters and the datasource create pdf like this: JasperExportManager.exportReportToPdfFile(jasperPrint, OUTPUTFILENAME+".pdf"); – SSC Feb 25 '13 at 12:50
  • Did you try using the above mentioned steps? – SSC Feb 25 '13 at 15:30
  • sir i got it - my mistake is that i am added all liabraries in build configuration path and another fault is in .jrxml file language=groovy – Neeraj singh Feb 28 '13 at 18:18
  • so i added all library in the webcontent\lib folder of project and delete the language= groovy from .jrxml file .......... – Neeraj singh Feb 28 '13 at 18:19
0

detected the root Exception, which was thrown before NoClassDefFoundError:net/sf/jasperreports/engine/util/JRStyledTextParser :

java.lang.NoClassDefFoundError: sun/awt/X11GraphicsEnvironment

The Sun AWT classes on Unix and Linux have a dependence on the X Window System. When you use these classes, they expect to load X client libraries and be able to talk to an X display server. This makes sense if your client has a GUI; unfortunately, it's required even if your client uses AWT but does not have a GUI (which is my case, generating a report from a web application)

The way to bypass this, is setting a system property java.awt.headless=true on system startup.

For Example

Let java execute the static block as early as possible by moving it to the top of the class!

  public class Foo() {
    static { /* works fine! ! */
      System.setProperty("java.awt.headless", "true");
      }

     public static void main() {}
  }

or

I think this parameter can be set by passing it as an argument to Java Virtual Machine e.g.

-Djava.awt.headless=true. Not sure if this would have impact on other components of your application.

or

Try putting this in your ${CATALINA_HOME}/bin/catalina.sh script:

export JAVA_OPTS="${JAVA_OPTS} -Djava.awt.headless=true"

refered from-https://stackoverflow.com/a/3651196/1602230

Community
  • 1
  • 1
Sathish
  • 4,403
  • 7
  • 31
  • 53