Loot
Loot

Reputation: 11

Error compiling

I just opened a "project" I made when I was studying Java some years ago, and although it runs fine, I get these errors and I don't know what they mean:

warning: [options] bootstrap class path not set in conjunction with -source 1.5 Note: D:***\src\java\reportes\ServletRLineaMunieca.java uses or overrides a deprecated API.

Note: Recompile with -Xlint:deprecation for details.

Note: D:***\src\java\reportes\ServletRLineaMunieca.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

1 warning


This web project includes many files, but I will only paste the one that gives the error:

package reportes;

import com.sql.ConectaDb;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class ServletRLineaMunieca extends HttpServlet {

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

    try {
        HttpServletRequestWrapper srw =
                new HttpServletRequestWrapper(request);
        String fpath = srw.getRealPath("") +
                "/reporte/RLineaMunieca.jrxml";

        JasperDesign jasperDesign =
                JRXmlLoader.load(fpath);
        JasperReport jasperReport =
                JasperCompileManager.compileReport(
                jasperDesign);

        HashMap jasperParameter = new HashMap();
        jasperParameter.put("logo", "http://localhost:8084/tarea/images/logo.jpg");

        Connection cn = new ConectaDb().getConnection();
        byte[] bytes =
                JasperRunManager.runReportToPdf(
                jasperReport, jasperParameter, cn);
        cn.close();

        response.setContentType("application/pdf");
        response.setContentLength(bytes.length);
        ServletOutputStream out =
                response.getOutputStream();
        out.write(bytes, 0, bytes.length);
        out.flush();
        out.close();

    } catch (Exception e) {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>Reportes</title></head>");
        out.println("<body>");
        out.println("<pre>");
        e.printStackTrace(out);
        out.println("</pre>");
        out.println("</body>");
        out.println("</html>");
    }



} 

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** 
 * Handles the HTTP <code>GET</code> method.
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
} 

/** 
 * Handles the HTTP <code>POST</code> method.
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
}

/** 
 * Returns a short description of the servlet.
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

Since I made this a long time ago I have forgotten a lot about Java. Please bear with me, this is also my first time posting here, if my code is pasted wrong please show me how to do it properly.

i forgot to add that this part:

String fpath = srw.getRealPath("") +

the getrealpath is strikeout (is that how you say it?)

Upvotes: 0

Views: 448

Answers (3)

Loot
Loot

Reputation: 11

i already solved my problem i changed it like this:

String fpath = srw.getRealPath("") +

to

String fpath = srw.getSession().getServletContext().getRealPath("") +

also

HashMap jasperParameter = new HashMap();

to

HashMap<String, String> jasperParameter;
jasperParameter = new HashMap<>(); 

thanks all :)

Upvotes: 1

Mel Nicholson
Mel Nicholson

Reputation: 3225

When the nice people at Oracle retire a class or method either because is has a problem or they got bored of updating it, they mark it as @Deprecated. One of the methods you are using is marked like this.

javac -Xlint:unchecked myFile.java will tell you which method you used, at which point you can look up the javadoc on that method and find out why it was retired and whether you cared.

You might not. For example if a timer method was retired because it mishandled daylight savings time, your application might not use it in a way that this flaw matters.

In this case, you didn't add the type-checking for your HashMap Check the link below for how Generics work. It's worth learning even though it won't change the behavior of this program.

http://docs.oracle.com/javase/tutorial/java/generics/

Upvotes: 0

Jayamohan
Jayamohan

Reputation: 12924

Since Java 5 if you're using collections without type specifiers (e.g., HashMap() instead of HashMap()). It means that the compiler can't check that you're using the collection in a type-safe way, using generics.

To get rid of the warning, just be specific about what type of objects you're storing in the collection. So, instead of

HashMap jasperParameter = new HashMap();

use

HashMap<String, String> jasperParameter = HashMap<String, String>();

Upvotes: 0

Related Questions