0

I'm trying to use some external jar files in my project but when I run the application it gives me ether java.lang.IllegalStateException or java.lang.ClassDefNotFoundError. I have tried almost every method mentioned in here but couldn't found a solution. I'm using ADT version 22 and Android SDK version 17.

Here is the log:

8-26 16:50:21.022: E/AndroidRuntime(16642): FATAL EXCEPTION: main
08-26 16:50:21.022: E/AndroidRuntime(16642): java.lang.NoClassDefFoundError: sun.misc.BASE64Encoder
08-26 16:50:21.022: E/AndroidRuntime(16642):    at com.test.testlibs.MainActivity.convertMD5(MainActivity.java:48)
08-26 16:50:21.022: E/AndroidRuntime(16642):    at com.test.testlibs.MainActivity$1.onClick(MainActivity.java:30)
08-26 16:50:21.022: E/AndroidRuntime(16642):    at android.view.View.performClick(View.java:4128)
08-26 16:50:21.022: E/AndroidRuntime(16642):    at android.view.View$PerformClick.run(View.java:17142)
08-26 16:50:21.022: E/AndroidRuntime(16642):    at android.os.Handler.handleCallback(Handler.java:615)
08-26 16:50:21.022: E/AndroidRuntime(16642):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-26 16:50:21.022: E/AndroidRuntime(16642):    at android.os.Looper.loop(Looper.java:213)
08-26 16:50:21.022: E/AndroidRuntime(16642):    at android.app.ActivityThread.main(ActivityThread.java:4787)
08-26 16:50:21.022: E/AndroidRuntime(16642):    at java.lang.reflect.Method.invokeNative(Native Method)
08-26 16:50:21.022: E/AndroidRuntime(16642):    at java.lang.reflect.Method.invoke(Method.java:511)
08-26 16:50:21.022: E/AndroidRuntime(16642):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
08-26 16:50:21.022: E/AndroidRuntime(16642):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
08-26 16:50:21.022: E/AndroidRuntime(16642):    at dalvik.system.NativeStart.main(Native Method)

and the class implimentation:

package com.test.testlibs;

import sun.misc.BASE64Encoder;

import com.test.testlibs.R.id;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(id.btnTest);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                convertMD5();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void convertMD5()
    {

        TextView textView = (TextView) findViewById(id.txtTest);
        String text = textView.getText().toString();

        BASE64Encoder base64Encoder = new BASE64Encoder();
        String uname64 = base64Encoder.encode(text.getBytes());

        Toast toast = Toast.makeText(getApplicationContext(), text+" "+uname64, Toast.LENGTH_LONG);
        toast.show();
    }
}

I can email my project even if that's necessary too.

SajithRu
  • 225
  • 1
  • 8
  • 24

4 Answers4

2

You are using a Sun(Oracle) specific class for Base64 encoding(sun.misc.BASE64Encoder). This file isn't going to be available on android.

I would suggest you use the android's Base64 class instead for encoding/decoding. As an additional benefit you would not even require to include any external library.

Manish
  • 3,913
  • 2
  • 29
  • 45
0

Add the .jar file to /libs folder of your project.

Now right click the .jar file and Build Path > Add to Build Path.

Re build the project and run.

You may also look at this https://stackoverflow.com/a/2267049/826657 & this How to get the JAR file for sun.misc.BASE64Encoder class?

The BASE64ENCODER class is not available for all JVMs.

Community
  • 1
  • 1
Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51
0

Check whether your workspace is having the JRE System Library in its classpath or not.

actually I can see in the exception stack that java.lang.NoClassDefFoundError for sun.misc.BASE64Encoder and BASE64Encoder.class is present in rt.jar i.e. the part of JRE System library.

Sandy
  • 972
  • 5
  • 15
  • 28
0
08-26 16:50:21.022: E/AndroidRuntime(16642): java.lang.NoClassDefFoundError: sun.misc.BASE64Encoder

You are using sun.misc.BASE64Encoder for Base64 encoding. And it is not found whenever your application runs.

  1. If you want to continue using it then try following.

    Right Click your project-->properties-->Java Build path-->Order and export tab
    
    then check the jars you are using.
    

2.If you are interested only in Base64 encoding then you can try android.util.Base64 for Base64 encoding.

Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72