简体   繁体   中英

Rest FB throwing exception in Servlet

I have a very basic problem in a project.

I wanted to get the friend list of a User in Facebook and for that I have used the API named RestFb(found here

Now coming to the coding, I have successfully created a function called getFriends() with the following format:

public List<User> getFriends(String APP_ID, String APP_SECRET, String CODE){
    //APP_ID -> Application Id generated in Facebook
    //APP_SECRET -> Secret that you get from Facebook when you create the APP
    //CODE -> A key that is sent to your servlet/callback url when you successfully logs in Fb with all the permissions given to App
}

This function is a static function that can be called from anywhere. Now when I calls this function after getting the code from Fb in a callback, using main function, it works and gives me all the users of the App those are my friends. But when I make a servlet and try to call this function it throws this error:

type Exception report

message

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: com/restfb/FacebookClient
    com.av.test.GetFriendList.doPost(GetFriendList.java:45)
    com.av.test.GetFriendList.doGet(GetFriendList.java:31)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

java.lang.ClassNotFoundException: com.restfb.FacebookClient
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
    com.av.test.GetFriendList.doPost(GetFriendList.java:45)
    com.av.test.GetFriendList.doGet(GetFriendList.java:31)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

I have created this project in Eclipse and have added the Jar of RestFb using Add External Jars option of Eclipse. I am pretty confused what to do?

Here is the code: GetFriendListMain.java

public class GetFriendListMain{
   public static List<User> getFriends(String APP_ID, String APP_SECRET, String CODE) throws Exception{
      //code goes here
   }
}

This is my servlet.

GetFriendList.java

public class GetFriendList extends HttpServlet {
   //doGet()
   public void doPost(...){
      String CODE = request.getParameter("code");
      String APP_ID = Config.APP_ID;
      String APP_SECRET = Config.APP_SECRET;
      List<User> users = GetFriendListMain.getFriends(APP_ID,APP_SECRET,CODE);
      //send this list back to caller to show on webpage
   }
}

Please help me. Thanks in advance. :)

Have you tried adding the needed archive files in your dpeloyment assembly?

https://www.genuitec.com/products/myeclipse/learning-center/basics/myeclipse-deployment-assembly/

or you could also use a build tool like apache maven, and add the needed archive as a dependency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM