Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit2 java.lang.NoClassDefFoundError: okhttp3/Call$Factory in JAVA

I am not developing a Android Application ,
I'm just writing some JAVA codes to support Imgur API services.

public interface ImgurAPI {
    String server = "https://api.imgur.com";
    String BASE64 = "base64";

    @POST("/3/upload")
    void postImage(
            @Header("Authorization") String auth,
            @Query("title") String title,
            @Query("description") String description,
            @Query("type") String type,
            @Body String base64Image,
            Callback<ImageResponse> cb
    );

}

Main :

public static void main(String[] args) {

    try{
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ImgurAPI.server)
                .build();
        ImgurAPI myAPI = retrofit.create(ImgurAPI.class);
        String base64Image = new ImageReader(PATH).getBase64String();
        myAPI.postImage(AUTH, "Hi", "Test", ImgurAPI.BASE64, base64Image, new MyCallBack());

    }catch(Exception err){
        err.printStackTrace();
    }
}

and the exception throwing :

    Exception in thread "main" java.lang.NoClassDefFoundError: okhttp3/Call$Factory
    at Main.main(Main.java:14)

Caused by: java.lang.ClassNotFoundException: okhttp3.Call$Factory

I found out lots of solutions of Android. So I'm wondering if Retrofit available in only JAVA.
thanks .

like image 574
OOD Waterball Avatar asked Mar 30 '17 07:03

OOD Waterball


2 Answers

I solved it , If you are writing Java (Only Java), And you downloaded the Jar of Retrofit2, It might not contain certain libraries which built-in in Android Studio, So you have to download them manually.

  1. OkHttp3 OkHttp3 3.0.0 Jar download
  2. Okio Okio 1.6.0 Jar download
  3. Retrofit-converter gson Retrofit converter gson-2 beta3 Jar download (If you want to convert other types of data , just download other Jars in Retrofit )
  4. Gson Gson 2.2.3 Jar download

Import the jar files , then It could work

Make sure you got all of itsenter image description here

like image 89
OOD Waterball Avatar answered Oct 29 '22 20:10

OOD Waterball


If you find this post when looking for a solution after getting this error when you use the Influxdb-java.jar in a Java EE project, you will need the following dependencies:

  • OKHttp3: https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp/3.9.1
  • Retrofit2: https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit/2.3.0
  • Moshi Converter for Retrofit2: https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-moshi/2.3.0
  • Moshi: https://mvnrepository.com/artifact/com.squareup.moshi/moshi/1.5.0
  • Okio: https://mvnrepository.com/artifact/com.squareup.okio/okio/1.13.0
  • OKHttp3 logging interceptor: https://mvnrepository.com/artifact/com.squareup.okhttp3/logging-interceptor/3.9.1

It was annoying to sort this out, so hope it helps someone.

like image 39
Maija Vilkina Avatar answered Oct 29 '22 18:10

Maija Vilkina