Search code examples
javanoclassdeffounderrornetbeans-8okhttp

OkHttp NetBeans NoClassDefFoundError


I have an Android application with a network speed test using OkHttp. Now I need to create another client for the desktop with NetBeans 8.2, but I can not make OkHttp work. I include the libraries okhttp-3.11.0 and okio-2.0.0, but I get java.lang.NoClassDefFoundError.

My code is:

public class TestCheckNetwork {

private final JFrame frame;
private final OkHttpClient client;

long startTime;
long endTime;
long fileSize;

TestCheckNetwork(JFrame frame) {
    this.frame = frame;
    client = new OkHttpClient();
}

void initTest() {
    Request request = new Request.Builder().url("http://ipv4.ikoula.testdebit.info/50k.iso").build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
            Utils.speedNetwork = 0;
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {

            startTime = System.currentTimeMillis();

            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }

            Headers responseHeaders = response.headers();
            for (int i = 0, size = responseHeaders.size(); i < size; i++) {
                //TODO log
            }

            InputStream input = response.body().byteStream();

            try {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];

                while ((System.currentTimeMillis() - startTime) < 50000 && input.read(buffer) != -1) {
                    bos.write(buffer);
                }
                byte[] docBuffer = bos.toByteArray();
                fileSize = bos.size();
                bos.close();

            } finally {
                input.close();
            }

            endTime = System.currentTimeMillis();

            // calculate how long it took by subtracting endtime from starttime
            double timeTakenMills = Math.floor(endTime - startTime);  // time taken in milliseconds
            final double timeTakenSecs = timeTakenMills / 1000;  // divide by 1000 to get time in seconds
            final int kilobytePerSec = (int) Math.round(1024 / timeTakenSecs);


            // get the download speed by dividing the file size by time taken to download
            final double speed = fileSize / timeTakenMills;
            Utils.speedNetwork = (int) speed;



        }
    });
}
}

And the error obtained is:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: kotlin/TypeCastException
at okhttp3.ResponseBody.create(ResponseBody.java:210)
at okhttp3.internal.Util.<clinit>(Util.java:60)
at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:123)
at assistantremote.TestCheckNetwork.<init>(TestCheckNetwork.java:35)
at assistantremote.AssistantRemote.inicializate(AssistantRemote.java:119)
at assistantremote.AssistantRemote.<init>(AssistantRemote.java:81)
at assistantremote.AssistantRemote.lambda$main$0(AssistantRemote.java:1758)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: kotlin.TypeCastException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 21 more
BUILD STOPPED (total time: 6 seconds)

I've only found solutions for Maven or Android. Where are my mistake or how can I make it without OkHttp? Thanks you


Solution

  • Try to add the kotlin stdlib and stdlib-common dependencies to your build.