ClassNotFoundException Android - java

So I ran into a problem today while working on my Android program. I have a class that turns that an XML string into a Java object (third party) and it works fine in as a regular java project but on Android I get this weird error:
06-21 22:44:26.402: DEBUG/App(259): java.lang.ClassNotFoundException: com.package.mycode.Class in loader dalvik.system.PathClassLoader#4001b500
06-21 22:44:26.402: DEBUG/App(259): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
I hide my application name and my package for obvious reasons but I was wondering if anyone has ever encountered problems like this. Class is in the correct package, which is a library I have added. Other classes that I reference before are there and those can be made. Are there any other reasons a ClassNotFoundException is thrown?
Thanks,
Jake

private static void fixClassLoaderIssue()
{
ClassLoader myClassLoader = MyClass.class.getClassLoader();
Thread.currentThread().setContextClassLoader(myClassLoader);
}
This is the code I currently have that I believe fixed this problem. MyClass is just a class I have in my project. Like I said, a co-worked showed it to me, but it seems pretty straight forward.

Are there any other reasons a ClassNotFoundException is thrown?
IIRC, it can also be thrown if some other class in the static dependencies of "com.package.mycode.Class" cannot be loaded, or if there is a problem during static initialization. But these should show up as a nested exception ... the first time you attempt to load the class.

Android has not all Java classes available that are available on a normal machine.
There are some classes missing that could cause this error. But this should be already be shown at compile time. Look at this list to see which classes are supported and which are not supported.
Again I'm only guessing because the compiler should see the missing classes.

For an android app, I'd same issue with an external jar file. In my case the solution was to move the jar from "lib" folder to the android default "libs" folder.
When the jar was in lib folder (was added to build path as well), while there are was no build issue in Eclipse, the app was giving ClassNotFoundException at runtime. Once I moved the jar to "libs" folder, the jar started appearing under "Android Dependencies" and app started working fine.
These are some of the other discussions about the same topic.
Adding a library/JAR to an Eclipse Android project
Android: What is the folder name of the jar files (LIB or LIBS)?
How to specify lib folder for JARs when using Android-generated ant build file?

i had a problem starting an aynctask. it used to work just fine but then i added
line = StringEscapeUtils.unescapeHtml4(line);
(its a commons.apache.org library) to the asynctask then lots of problems. it compiles ok but when i tried to run, it said class not found on my asynctask class. but it i hit resume it kept going in the async task, then it died.
so i guess the problem is StringEscapeUtils isn't installed properly, but i had no way of figuring that out from the error messages i was getting.

Are there any other reasons a ClassNotFoundException is thrown?
In my case, I got a ClassNotFoundException for my main activity when building the apk with eclipse but did not have any problem when building with maven.
The problem was that my main activity was implementing an interface from a plain java project "POJO library". In the Android project>Properties>Java Build Path>Order and Export I had to tick the box to export the "POJO Library". After cleaning the project, the interface was included in the classes.dex in the apk and the problem was solved.
I was directed here when trying to understand the problem, and I hope it will help somebody else in the same situation.

Related

Java LWJGL (linux): Could not initialize class org.lwjgl.system.MemoryAccess

I just added lwjgl 3.1 to a project in Intellij IDEA. To verify everything was done correctly I tried to run the guide code in the webpage (https://www.lwjgl.org/guide), but when compiling the following error came out:
"Exception in thread "main" java.lang.NoClassDefFoundError: Could not
initialize class org.lwjgl.system.MemoryAccess"
After a google search I found out that the possible error is in the configuration of the native libraries, which I have added when configuring the library in the project.
When LWJGL was downloaded from the webpage, I got a a zip file with all the jar files of the modules (classes, docs, natives and sources), what I did was to separate them accordingly and then add them as a single library in the global libraries section of the IDE.
Global Libraries
After this, the library appears as a module of the project.
Still, the program won't compile. Is there anything else I should configure?
You claim that this is a compile-time error, however, after looking through the documentation, I have found out that this is a runtime error, where you successfully compile, but the JVM is unable to find the class at runtime. To solve this, I would suggest merging your .jar output and the lwjgl jar using jarsplice to see if that fixes it. If it fixes it, the problem is with intelliJ not correctly adding your libraries to your jar.

java.lang.ClassNotFoundException: Didn't find class with TeamCity build

After adding android-support-v7-appcompat and google-play-services_lib to the Android project application starts to crash right after start with
java.lang.ClassNotFoundException exception.
I read all questions on StackOverflow related to this problem, and opened Eclipse -> Java Build Path -> Order and Export and verified that:
Android Private Libraries, Android dependencies, Google Play Services.jar are checked and Android API unchecked here.
So apk built locally works fine. But on TeamCity side apk which we get as result crash with exception. So I suppose, that I need to add some changes to Ant script regarding android-support-v7-appcompat.
Please suggest steps which I can try.
ClassNotFoundException is exactly that. A class that is references cannot be found. When working with third party libraries, you must ensure that their dependancies (if any) are also included in your class path.
Without seeing the actual stacktrace, its difficult to narrow down the problem, but I would check if the dependancies of the third party libraries that I am adding are also included in the class path.
Hope that helps.

Conversion to Dalvik format failed with error 1 while compiling jitsi project

I am trying to compile and run the project
https://github.com/jitsi/jitsi-android on Eclipse by Importing.
I have imported the project successfully,Added External Jar files and Fix some of compilation errors.
When I try to run app, It is giving
Conversion to Dalvik format failed with error 1 Error on the console.So that I Clean the project, checked the all the jar files from Order and Export tab and build it again.
but it doesn't work for me.
Finally, I unchecked the all the libraries from Order and Export tab,build and run it again.
But still its giving me same error and my application is getting crashed every time i run it.
Any help will be appreciated , i really want to run this application.
here is my libs folder screenshot:-
I've had the same problem once.
Here is what I found out when I was trying to solve my issue:
The error is caused when there are two Classes/Packages with the same name (For example when LibaryX has Class org.a.b.c.Sample and LibaryY has class org.a.b.c.Sample aswell)
Different approaches which worked for some people (So if you have the error yourself, make sure to test following things out):
A) Two Libaries have the exactly same package/class-> Delete the class in one of the Libaries
B) One Jar is detected twice because it's for example in the project source folder and in the Java Build Path Libaries aswell
C) Updating Proguard often fixes it aswell (Put the new Proguard jars in [Android SDK folder]\tools\proguard\lib)
D) Cleaning the Project also helped many people (you already did that, so that won't be your solution)
When I had this issue myself it was caused because I did not follow the instructions of one of the libaries that I used. After following the libary instructions step by step, everything worked fine again.
Greetings
Felix

Java and Scala ClassNotFoundException after upgrading to IntelliJ 14

I recently upgraded to IntelliJ 14, then added Scala support.
My main files stopped running. For example the GUI file, which has no errors and is a totally self contained hello world style for testing this error:
Exception in thread "main" java.lang.ClassNotFoundException: GUI
It makes no sense and I'm pretty frustrated that the IntelliJ devs are slacking in not auto-detecting a solution to this. I have tried everything to fix it. From settings, compilers, to making the project from scratch.
I only had a bit of luck when making it from scratch, but as soon as I started to add my other files (files that were not even being used!) it starts with the error.
It seems that any files outside of the original project module (when creating a new project to test fix) will not run giving ClassNotFoundException.
This may be related to my other question: https://stackoverflow.com/questions/27516673/cannot-run-file-located-out-of-main-module-intellij-14-java-scala
Sample View of the problem
I had the same problem and was able to fix it by right clicking the directory my class was in, and clicking Mark as sources root. This should make your class discoverable and able to run.
Solution
Turns out that Make, No Error Check does not work the same for scala as it does for Java.
Errors in the scala files will cause an ambiguous error message on run, with no clear link to the error.
Fixing the scala files fixes the error.
In my case, the solution was at the JDK and java level.
When I set JDK 14 this error occurred.
So when I work with a scala project, I set java 8 everywhere in the project(module) settings and in the configuration of the main method.
In project(module) settings section: Project, Modules, SDK's
In main method settings: JRE

Approach for fixing NoClassDefFoundError? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NoClassDefFoundError - Eclipse and Android
I'm seeing this question is getting asked a lot in many different contexts. Perhaps we can set some strategies for locating and fixing it? I'm noobish myself so all I can contribute are horror stories and questions, sorry...
It seems this is thrown when a class is visible at compile time but not at run time... how can this happen?
In my case I am developing an app that uses the Google APIs, in Eclipse, for the Android platform. I've configured the Project Properties / Java Build Path / Libraries to include the gdata .jars and all is well. When I execute in the emulator I get a force close and the logcat shows a NoClassDefFoundError on a simple new ContactsService("myApp"); I've also tried a new CalendarService("myApp") with the same results.
Is it possible or desirable to statically bind at compile time to avoid the problem?
How could dynamic binding of an add-on library work in the mobile environment anyway? Either it has to be bound into my .apk or else I need to "install" it? ... hmmm.
Advice much appreciated.
It seems this is thrown when a class
is visible at compile time but not at
run time... how can this happen?
The build classpath may include JARs that are not being packaged into the APK.
Is it possible or desirable to
statically bind at compile time to
avoid the problem?
It is possible, desirable, and necessary.
Outside of Eclipse, you just put the JARs you need in libs/ in your project, compile with Ant, and you are done.
Inside of Eclipse, one pattern I have had students use with success is to put the JARs you need in libs/ in your project, add them as JARs to the build path (note: not external JARs), and they get packaged as part of the APK. Note, though, that I do not personally use Eclipse, and so my experience with it is limited.
For those having problem I was having the same error with my app. what I did to solve that was create a new project and copy my resource and source folders along with my manifest file into the new project (I deleted in advance those within the new project created) and voila.
When I got this, the problem was actually deeper in the queue; Dalvik converter had failed to convert some of the referenced libraries and still Eclipse allowed me to launch the project.
Check the Android SDK console to see if there are any errors reported.
In my case, I'm using my own library (MyLib) shared between 2 apps. App A was closed when I added a new class to the library.
When I opened App A to work on it, Eclipse recognised the new class, and I was able to reference it. However on running I got the error.
It turned out that the imported library folder in App A (named something like MyLib_src) didn't reflect the changes made to my library project (MyLib).
To solve this I refreshed App A, the changes reflected, and Android could build my project correctly.
I have found no reference to this version of the problem, so thought I would add it to this list.

Categories

Resources