Java – Android ClassNotFoundException : Didn’t find class MainActivity

Android ClassNotFoundException : Didn’t find class MainActivity… here is a solution to the problem.

Android ClassNotFoundException : Didn’t find class MainActivity

First, there is an image (my IDE’s print screen) that makes it easier to show my packages and files, but when I try to submit my issue, the system says “you need at least 10 credits to post an image” What does that mean?????

I use the ADT plugins from Eclipse Juno and Google to develop Android applications.

At first, no problem, I could run my app in the emulator or even on my Android device.

The problem only started to appear after I added the Eclipse plugin for Maven.
This is the plugin “Integration for the Android Development Tools and the m2eclipse.”
Maven2 plugin version 0.4.3″

I’ve tried everything since then but have spent too much time to move on.

I get this exception at runtime :

01-10 09:56:17.804: E/Trace(1180): error opening trace file: No such file or directory (2)
01-10 09:56:17.914: D/AndroidRuntime(1180): Shutting down VM
01-10 09:56:17.914: W/dalvikvm(1180): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
01-10 09:56:17.935: E/AndroidRuntime(1180): FATAL EXCEPTION: main
01-10 09:56:17.935: E/AndroidRuntime(1180): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.shoppinglist2/ com.example.shoppinglist2.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.shoppinglist2.MainActivity" on path: /data/app/ com.example.shoppinglist2-1.apk
01-10 09:56:17.935: E/AndroidRuntime(1180):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at android.os.Looper.loop(Looper.java:137)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at java.lang.reflect.Method.invoke(Method.java:511)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at dalvik.system.NativeStart.main(Native Method)
01-10 09:56:17.935: E/AndroidRuntime(1180): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.shoppinglist2.MainActivity" on path: /data/app/ com.example.shoppinglist2-1.apk
01-10 09:56:17.935: E/AndroidRuntime(1180):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
01-10 09:56:17.935: E/AndroidRuntime(1180):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
01-10 09:56:17.935: E/AndroidRuntime(1180):     ... 11 more

If I can add a picture, you can see that there are no errors in package com.example.shoppinglist2 (from Package Explorer View) and the class name is MainActivity.

Here is the contents of my AndroidManifest.xml file (question from Activity android:name).

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shoppinglist2"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.example.shoppinglist2.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest> 

Solution

You are using the Maven plugin in your project, and “Maven -> Maven Update…” may cause problems with ClassNotFoundException. As you know, the default class output folder should be the /bin/classes specified in the .classpath file and Eclipse build path. But once you execute “Maven->Maven Update…”, the output folder will be set to “target/classes”. You can find it in the .classpath file (output=”target/classes”).

.classpath file:

...    
<classpath>
            <classpathentry kind="src" path="gen"/>
            <classpathentry kind="src" output="target/classes" path="src/main/java">
...

To fix this, explicitly specify the class output folder as “bin/classes” in the pom.xml file.

pom.xml file:

...
<build>
            <sourceDirectory>src/main/java</sourceDirectory>
            <outputDirectory>bin/classes</outputDirectory>
...

Related Problems and Solutions