Search code examples
javajspservletshttp-errorrestfb

HTTP Status 500 - Internal Server Error eclipse


I am trying to run an app on Tomcat v9.0 and have already wrote a function that supposedly 'prints' an html table based on an arraylist of data extracted using the library restFB. I have tested the restFB code in another project that doesn't run on a server and it works fine, but when I try to use a button and call the funtion inside the servlet it gives me the status 500 error.

java.lang.NoClassDefFoundError: com/restfb/FacebookClient
    Logica.FacebookCall.doPost(FacebookCall.java:49)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Here is the servlet code:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        // doGet(request, response);
        String button = request.getParameter("button");
        if ("Obtener Datos".equals(button)) {
            FacebookCalls fb = new FacebookCalls();
            ArrayList<String> html = fb.getInfoMiembros();
            PrintWriter out = response.getWriter();
            out.println("<table border=\"1\">");
            out.println("<tr>");
            for (int i = 0; i < html.size(); i++) {
                if (i > 0 && i % 4 == 0) {
                    out.println("</tr><tr>");
                }
                out.println("<td>" + html.get(i) + "</td>");
            }
            out.println("</tr>");
            out.println("</table>");
        }
    }

The FacebookCalls fb = new FacebookCalls(); is a reference to another class in which I have the code that returns the arraylist of facebook data. Shown below:

public FacebookCalls() {}

public ArrayList<String> getInfoMiembros()
{
    String accessToken = "myToken";

    ArrayList<Persona> miembros = new ArrayList<Persona>();

    ArrayList<String> listaNombres = new ArrayList<String>();

    @SuppressWarnings("deprecation")
    FacebookClient fbClient = new DefaultFacebookClient(accessToken);

    JsonObject fetchObjectsResults =
              fbClient.fetchObjects(Arrays.asList("352867045226094"), 
                       JsonObject.class, Parameter.with("fields","members{name,gender}"));

    JSONObject jsonObj = new JSONObject(fetchObjectsResults.get("352867045226094").toString());
    JSONArray array = jsonObj.getJSONObject("members").getJSONArray("data");
    for(int i = 0; i < array.length(); i++)
    {
        Persona miembro = new Persona();
        miembro.setNombre(array.getJSONObject(i).getString("name"));
        try {
            miembro.setGenero(array.getJSONObject(i).getString("gender"));
        }
        catch(Exception e)
        {
            miembro.setGenero("No disponible");
        }
        miembros.add(miembro);
    }       
    for(int i = 0; i < miembros.size(); i++)
    {
        listaNombres.add(miembros.get(i).getNombre());
        //html.add(miembros.get(i).getGenero());
    }
    listaNombres = sortApellido(listaNombres);
    return listaNombres;
}

Solution

  • According to revision we did in the chat session, the problem is that the jar file containing the com.restfb.FacebookClient class is not being exported in the war file / deployed to the application server.

    For the jar to be exported, besides of having it added to the java build path, it also has to be present in the WEB-INF/lib folder of the project.

    In this case the jar file was added from an external location so it wasn't present itn the WEB-INF/lib folder, hence not exported when deploying the application / or generating the the war file.

    To solve the problem add the jar file to the WEB-INF/lib folder of the project.