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

3

6

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 .

Nuno answered 30/3, 2017 at 7:43 Comment(0)
N
9

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

Nuno answered 31/3, 2017 at 10:13 Comment(2)
Hello, I'm having the same problem. I added all the JARs manually, EXACTLY like in my Android application (I'm doing a project using Retrofit on the server this time, on the endpoint that gives information to the Android application) but I get the "java.lang.ClassNotFoundException: okhttp3.Call$Factory" error. I can't seem to find a way to solve it yet.Navel
the links seem all down or the site deadGlyoxaline
X
5

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:

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

Xylo answered 3/2, 2018 at 22:19 Comment(0)
S
0

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:

For anyone attempting to get influx-java to work, and as an update to the answer above: There are actually a lot more dependencies involved. A single file with all the dependencies can be found here. Extracting this file and adding all the contents to your project should solve any issues.

Secundas answered 17/1, 2023 at 23:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.