Search code examples
javagoogle-api-clientgoogle-api-java-clientgoogle-search-consolegoogle-oauth-java-client

Java Jar -java.lang.NoClassDefFoundError: com.google.api.client.http.HttpTransport from


i am using webmaster tool Java api V3 to query data , my code work fine in eclipse but when i export it to a Jar i get the error below

Exception in thread "main" java.lang.NoClassDefFoundError: com.google.api.client.http.HttpTransport
    at java.lang.J9VMInternals.prepareClassImpl(Native Method)
    at java.lang.J9VMInternals.prepare(J9VMInternals.java:430)
    at java.lang.Class.getMethod(Class.java:1061)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:506)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:498)
Caused by: java.lang.ClassNotFoundException: com.google.api.client.http.HttpTransport
        at java.net.URLClassLoader.findClass(URLClassLoader.java:665)
        at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:942)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:851)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:82

my dependecies are as follow

enter image description here

enter image description here

i am compiling and exporting the jar in Java6 and executing it using Java7.

i assume it's a dependency issue but i don't know how to resolve it ?? espically it's working in eclipse but when i export the jar from eclipse keeping the default config and selecting the main class i get NoClassDefFoundError , any ideas thank you in advance.


Solution

  • You'll get this error when class com.google.api.client.http.HttpTransport IS available during compilation but is NOT available at runtime.

    Eclipse automatically adds the jar files on the build classpath to your runtime configuration's classpath. Therefore you won't get the Exception when you're running from Eclipse.

    I'm assuming you are running form the command line like this: java -cp myjarfile.jar com.my.Main

    If this is the case, the classpath is missing the dependencies as configured in Eclipse. To solve this, append each of them as follows: java -cp myjarfile.jar;commons-logging-1.1.1.jar;etc... com.my.Main

    Note that the semi-colon ; in the classpath is for Windows. Use a colon : on Linux.

    Update When using a runnable jar file, make sure your class path is correctly set in the manifest file.

    https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html