0

i'm trying to build a java server that when it is getting a "POST", it will make a div and inside will be a list of files i got in my google drive.

first, i built the google drive side, and got it to a arrylist. and it's working, when i give the right parameters.

but when i'm trying to combine it with my server, it's going crazy. my doPost method is invoking this list and wants to print it, but then i have some bad errors.

plz advise.

Connection.java (removed the id and sec)

package javaServerPack;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Connection {




    public static void main(String[] args) throws IOException
    {
        System.out.println(Connection.getFilesNames(clientId, clientSecret, refresh));



    }


    public static String getFilesNames(String clientId,String clientSecret,String refresh) throws IOException
    {


        HttpTransport httpTransport = new NetHttpTransport();
        JacksonFactory jsonFactory = new JacksonFactory();


        GoogleCredential credential = logIn(clientId,clientSecret,refresh,httpTransport,jsonFactory);

         Drive service = new Drive.Builder(httpTransport, jsonFactory, credential)
         .setApplicationName("listNumberOne")
         .build();

         return printFilesName(retrieveAllFiles(service));

    }

    private static String printFilesName(List<File> files)
    {
         String lines = "";
         for(File each:files)
         {

             lines += each.getTitle() + "\n";
         }
         return lines;
    }

    private static GoogleCredential logIn(String clientId, String clientSecret, String refresh, 
            HttpTransport httpTransport,JacksonFactory jsonFactory)
    {

        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setClientSecrets(clientId,clientSecret)
                .build();

        credential.setRefreshToken(refresh);
        return credential;
    }


  /**
   * Retrieve a list of File resources.
   *
   * @param service Drive API service instance.
   * @return List of File resources.
   */
  private static List<File> retrieveAllFiles(Drive service) throws IOException {
    List<File> result = new ArrayList<File>();
    Files.List request = service.files().list();

    do {
      try {
        FileList files = request.execute();

        result.addAll(files.getItems());
        request.setPageToken(files.getNextPageToken());
      } catch (IOException e) {
        System.out.println("An error occurred: " + e);
        request.setPageToken(null);
      }
    }
    while (request.getPageToken() != null &&
             request.getPageToken().length() > 0);

    return result;
  }

}

Java servlet:

package javaServerPack;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JavaServer extends HttpServlet {
    private static final long serialVersionUID = 1L;


    /**
     * Default constructor. 
     */
    public JavaServer() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //System.out.println(request.getParameter("name"));

        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");

        response.getWriter().println("<div id =\"someDiv\">" + "hi" +   " </div>");

    }
     /*Connection.getFilesNames(clientId, clientSecret, refresh)
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");

        response.getWriter().println("<div id =\"someDiv\">"  + Connection.getFilesNames(clientId2, clientSecret2, refresh2) + " </div>");
    }

}

ERROR CODE FROM CONSOLE: (after running the server and sending post req)

2016-01-14 14:26:18.007:WARN:oejs.ServletHandler:qtp548246552-13: Error for /JavaServer/JavaServer
java.lang.NoClassDefFoundError: com/google/api/client/http/HttpTransport
    at javaServerPack.JavaServer.doPost(JavaServer.java:54)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:808)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:499)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
    at java.lang.Thread.run(Unknown Source)
Caused by: 
java.lang.ClassNotFoundException: com.google.api.client.http.HttpTransport
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:450)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:403)
    at javaServerPack.JavaServer.doPost(JavaServer.java:54)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:808)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:499)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
    at java.lang.Thread.run(Unknown Source)
Amit
  • 11
  • 6
  • Make sure you have added the google client http jar files to your project. Also, sometimes you might just have to perform a clean on the project depending on the IDE you're using. Try to see if that works. Also take a look at this SO [post](http://stackoverflow.com/questions/10235034/java-lang-noclassdeffounderror-com-google-api-client-http-javanet-nethttptransp) – Andres Jan 15 '16 at 00:24
  • hi, thank you, how can i "clean" the project? – Amit Jan 15 '16 at 11:21
  • It all depends on the IDE which you're using. For example, Eclipse, the clean option is under the Project tab. After you select the clean option, you have the option to "Clean all projects", make sure it's checked. This will remove any invalid resources from the server before doing a full republish. Again, I'm not saying it will fix the issue, but it's worth a try. :) – Andres Jan 15 '16 at 21:39

0 Answers0