在服务器上运行一个具有主类的java文件?

时间:2014-10-25 11:28:37

标签: java servlets jasper-reports

我有一个使用 JasperReports 生成演示文件的报告,它可以作为独立文件正常工作。我想在网络应用中使用它;也就是当用户选择生成报告时,应该执行该文件。我能够运行该文件,但它会抛出一个未找到文件的异常(.jrxml文件)。现在我不知道为什么我收到此错误,因为我已经尝试将文件放入与我的servlet相同的文件夹中,但我仍然遇到同样的错误。

这是我的servlet

 @WebServlet("/ReportGenerator")
public class ReportGenerator extends HttpServlet {
private static final long serialVersionUID = 1L;

public ReportGenerator() {
    super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws   ServletException, IOException {
    PrintWriter pr= response.getWriter();
    pr.println("hello");

    empRep.ReportGenerator gen= new empRep.ReportGenerator();
    gen.generateReport();
}

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

这是empRep.ReportGenerator。

 public class ReportGenerator {

 Connection conn;

public void generateReport() {
 String url = "jdbc:mysql://localhost:3306/";
  String dbName = "shop";
  String driver = "com.mysql.jdbc.Driver";
  String userName = "root"; 
  String password = "root";
try {
Class.forName(driver).newInstance();
conn = (Connection) DriverManager.getConnection(url+dbName,userName,password);
    System.out.println("Loading Report Designs");
    InputStream input = new FileInputStream(new File("jrxml/EmployeeReport.jrxml"));
    JasperDesign jasperDesign = JRXmlLoader.load(input);

    System.out.println("Compiling Report Designs");
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

    System.out.println("Creating JasperPrint Object");
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("ReportTitle", "PDF JasperReport");

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null,conn);

    //Exporting the report
    OutputStream output = new FileOutputStream(new File("report/EmployeeReport.pdf"));

    JasperExportManager.exportReportToPdfStream(jasperPrint, output);

    System.out.println("Report Generation Complete");
    conn.close();
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (JRException e) {
    e.printStackTrace();
  } catch (ClassNotFoundException e) { 
    e.printStackTrace();
  } catch (SQLException e) {
    e.printStackTrace(); 
  } catch (Exception e) {
e.printStackTrace();
  }
}

public static void main(String[] args) {
    new ReportGenerator().generateReport();
}

}

我在名为EmployeeReport.jrxml的文件夹中有jrxml,该文件夹位于项目名称下。

我收到了这些错误:

Loading Report Designs
java.io.FileNotFoundException: jrxml\EmployeeReport.jrxml (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at empRep.ReportGenerator.generateReport(ReportGenerator.java:41)
at myPack.ReportGenerator.doGet(ReportGenerator.java:63)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at                                                                                             org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at         org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at     org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at     org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:     607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

我想我在servlet的代码中遗漏了一些东西。 请指导我完成这个。

1 个答案:

答案 0 :(得分:0)

在Maven项目中,您应该将文件夹jrxml移动到main / resources文件夹中,然后您可以通过调用来获取路径:

ReportGenerator.class.getResource("/jrxml/EmployeeReport.jrxml").getPath()

使用Tomcat时,您可以尝试将jrxml移动到WEB-INF中并执行以下操作:

public static String getWebInfPath(){
    Class<ReportGenerator> c = ReportGenerator.class;
    URL classUrl = c.getResource(c.getSimpleName() + ".class");
    String path = classUrl.getPath().split("classes")[0]; 
    return path;
}

然后您的文件路径将是:

String path = getWebInfPath() + "jrxml/EmployeeReport.jrxml";