Android: java.lang.NoClassDefFoundError after importing jar. Wrong jar? - java

After I've imported jar file into Android project and ran it, I get java.lang.NoClassDefFoundError At first I thought that I'm doing something wrong while importing it, but then I tryed to import jar that I've from some online class and it worked! So here is my question:
Is there any specific way of creating jar files for Android project?
I've to point out that first jar is created from Java class that I wrote some time ago. This jar works without problems in another java app. The jar was created using Eclipse Kepler.
Thanks for your help.

The solution shoved up to be pretty simple. Currently I'm using Java 1.7 to create my jar files, but Android accepts only jars that were created with Java 1.6. So the problem is caused probably by some incompatibility in bytecode and therefore you need to force compiler to use Java 1.6.
If you're using Eclipse, go to Project properties > Java compiler and select Compiler compliance level: 1.6 Save changes and rebuild your project. You're ready to export "working" jar :)

Related

Why is my fat JAR not running on different PCs - main class not found?

I built a javaFX artifact using IntelliJ. My first problem was in the Application Class box, where it didn't automatically detect my main class, or even suggest any classes as main class when I searched in the search box. I saw that it was a bug with IntelliJ, somewhere on StackOverflow, and I just manually entered my packageName.Main in the application class box.
I have two dependencies that I extracted into this output root. The output of building the artifact looks like this.
Then after rebuilding the project the JAR runs on my PC 100%, even if I move the JAR to another directory, it works 100%.
So I copied this JAR (Swansong.jar) to my other PC and when I double-clicked it, nothing happended. When I ran it in the cmd:
java -jar Swansong.jar I got this error:
Error: Could not find or load main class swansong.Main
Caused by: java.lang.ClassNotFoundException: swansong.Main
I read about manifest files but I can't find a manifest file anywhere in my project, but if I extract the JAR file the manifest file that was generated looks like this. Can anyone please help me, why does the JAR run on one PC but not on the other? And how can I fix this?
UPDATE:
This is the contents of my jar file and this is the contents of the swansong directory inside the jar file.
UPDATE 2:
I feel stupid. My main PC runs jdk8 and my other pc runs jdk11. And JavaFX is not included in the jdk11. So I think this is the problem, but not 100% sure yet. Will test and see. Are there any workarounds for this?
I hate to admit it, but the solution to the problem was staring me in the face all along, and I am truly embarrassed. I used Java 8 to develop the program. Target PC was using Java 11 and I never thought to check the JDK version, because I thought I was using JDK8 on both PCs.
JavaFX is included in JDK8, but not in JDK11. It should be added to a project as an external library when using newer JDK versions. I just used JDK8 for running the program and everything works 100%. Thanks for the answers and comments though, I learned a lot about manifests and classpaths trying to solve this problem, so the effort was not in vain!
Any executable jar file needs to have manifest file which points to the main class.
-JAR File
\---META-INF
\--MANIFEST.MF
In the MANIFEST.MF, you should introduce the main class as this:
Main-Class: com.mycompany.MainClass
Class-Path: .
com.mycompany.MainClass should be replaced with appropriate path (fully qualified name of your main class)

Decompiled .class file in Android studio

I got problem during adding library into Android Studio project.
I add jar file to directory libs. I clicked Add as library and the problem is that when I try to run project, it fails.
The problem is decompiled .class file bytecode version 51.0 (Java 7).
Error in Android Monitor is NoClassDefFound.
What I got for now, is that I should have java files, but I have .class files.
Can anybody tell me how to solve this problem?
I solve my problem just using Java Decompiler and adding new java files to project.

Java Protobuf in Android - NoClassDefFoundError

I'm building an Android app in ADT v21.0.0. This application needs to use a protobuf library I've compiled into a jar.
By putting this jar as well as protobuf-java-2.4.1.jar in my libs/ folder, Eclipse recognizes it at build time, however whenever I attempt to run the application in my Android emulator, I get a NoClassDefFoundError exception when attempting to instantiate one of my auto-generated custom classes produced by the protobuf compiler.
I've seen a lot of tips regarding the naming of the libs/ folder (apparently a prior ADT update broke this by requiring the /lib folder to be /libs), as well as adding .jars explicitly from the "Java Build Path" settings window (and checking the jars in the Order and Export pane).
None of these have helped, however, and I'm wondering if there's something more fundamental preventing my protobuf classes from being recognized at runtime.
Note: if I create a standard Java application in Eclipse, I am able to use my proto classes just fine. This is only an issue when running the Android application.
seems that your project import some library what your auto-generated jar depends..and is not on device or emulator..

Java project won't compile to a JAR correctly for Android

in an app I'm working on I have an issue with a Java library which is here - https://github.com/kennydude/Boid-Twitter-API
When I run ant android which builds an Android package it builds fine and Eclipse can read the Jar fine.
But on my device it says that dalvik cannot locate any of the classes and eventually throws a ClassDef Exception.
I have used Jars before, so I'm wondering if it's my ant file that's broken
I would open your app project in eclipse (the one that depends on the Java library that is not found at run-time) and add the jar file the Java library produced to its build path. Then execute
android -s update project -p .
in the same directory as your apps' Manifest.xml file in order do generate ant build files.
After this, you should be able to build your app using ant by doing
ant debug
or
ant release
I fixed it by ensuring Ant was compiling to 1.6 of Java which is my target in Eclipse for the application.
Perhaps you're not placing your jar in the libs directory as opposed to lib directory? Here's a similar issue Java (Android) - class not found exception
First of all to build an apk with ant for Android you have to use ant debug or ant release, i don't know what ant android does and why you are using it but this command is not part of the standard approach to ant for Android, that's for sure.
Ant is just a building tools, if a developer provide a building solution in ant, does not mean that his application is for Android.
Under Android everything that is not a native library is converted in a DEX object, which is something similar to bytecode for the Dalvik, there is no jar or other container in your app.
This question may be helpful for you Adding a library/JAR to an Eclipse Android project .
There are a lot of conceptual errors in this topic, you probably want to study the Android platform before using it.

Java JNA does not seem to be working

I recently downloaded the Java JNA from https://github.com/twall/jna/servlets/ProjectDocumentList?folderID=7408&expandFolder=7408&folderID=0 and tried using it. However, as exemplified by the screenshot below, the demo source code provided in https://github.com/twall/jna/ won't compile, no matter where I place the files. Mind you, I've tried placing them both in the default package an every other combination of package mix-ups, and this is only the latest version. Does anybody know why the compiler can't find what is contained within the JAR file? (Also, as you can see, the class files are all in the jar file to be found)
Basically, the JAR should go in the classpath. In Netbeans, you can do it by adding it as library to the build path of the project through the project properties.

Categories