6

I have created simple app to test the scenario which is failing with NoClassDefFoundError.

Lets take for example i have Test1 android project with Class TestClass with some methods. This Test1 project i exported as a test1.jar and exported only source folder with Class TestClass and classpath and .project files.

In another android project Test2 i added this test1.jar in my libs folder. And in this Test2 project i have a class Test2Class which calls methods of test1.jar class.

After this i exported this Test2 project as test2.jar file following above steps.

So when i use this test2.jar in another project i get this above error NoClassDefFoundError. It is the scenario of jar inside a jar.

Is anywhere should i do something so i am able to access a jar inside a jar.

Thanks in advance.

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
Naveen Kumar
  • 979
  • 1
  • 13
  • 24

4 Answers4

3

Try this Step by step:
1. Remove all library projects then Clean.
2. Go to first Project Test1 Right click Properties -> Android. Check Is Library(make it library).
3. Go to second Project Test2 Right click Properties -> Android in the Library section select Add -> Add first project as library.
4. Go to second Project Test2 Right click Properties -> Android. Also, Check Is Library.
5. Go to third project C Right click Properties -> Android. In the Library section select Add -> Add First and Second as library. (Do not make Proj C as library)
6. Now Clean everything and run Proj C.
Edit: You can also see this bug Bug 405212 - Do not pack jars bundled inside a packed jar and Bug 361628 - Nested jars cannot be unpacked with Java 1.7. Try the above way it should work.

Mohammed Ali
  • 2,758
  • 5
  • 23
  • 41
  • If you want to apply patch for the bug you can see [this](https://bugs.eclipse.org/bugs/attachment.cgi?id=229472&action=edit). Also see eclipse [help to apply patch](http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Ftasks%2Ftasks-68c.htm). – Mohammed Ali Dec 03 '14 at 19:39
0

Remove the jars from the java build path. Add new folder name as "libs" and posted all the jars in that folder. Application will run fine

Amit Thaper
  • 2,117
  • 4
  • 26
  • 49
  • 1
    I still have the NoClassDefFoundError. – Naveen Kumar Nov 27 '14 at 11:58
  • Please cross check the libraries and Order and Export section. There should be no jar present in that. If yes then remove it. If exception arise again then please send the screenshot for the libraries and Order and Export of java build path. – Amit Thaper Nov 27 '14 at 12:15
  • Ya as you said i put test1.jar file inside libs folder of Test2 project and made sure it is not in order and export and created a test2.jar.. And as well as this test2.jar i have put in another new project's libs folder and made sure it is not in order and export. – Naveen Kumar Nov 27 '14 at 12:23
  • Which class displays an error of NoClassFoundException? Please send the logcat – Amit Thaper Nov 29 '14 at 06:40
  • Sorry for late reply. I am getting NoClassDefFoundError exception for class present inside test1.jar. As i am trying to call method of test1.jar inside test2.jar method. – Naveen Kumar Dec 02 '14 at 06:27
  • The class in test2.jar has the same class in test1.jar also or vice versa. – Amit Thaper Dec 04 '14 at 05:37
  • No Class inside test2.jar is different. You can refer other question of mine similar to this. http://stackoverflow.com/questions/27272404/in-android-create-a-jar-inside-jar-and-accessing-the-classes-of-first-jar – Naveen Kumar Dec 04 '14 at 06:26
  • As reference URL, Why not you call TestAppMethods.testMethod2(); rather than TestApp2Class.TestApp2Method(); both will do the same call – Amit Thaper Dec 04 '14 at 08:18
  • No, in that example it is just testing app. Basically i will be using some methods of first jar in my second jar and also i have other functionality in my second jar. To achieve this i have to create a wrapper. – Naveen Kumar Dec 04 '14 at 08:27
  • Ok. Add test1.jar in test2 application. create jar of test2 application. Only add this jar to your project. remove test1.jar from the project – Amit Thaper Dec 04 '14 at 09:54
0

Try this :

Go to Project/Properties/Java Build Path/Order and Export -- Make sure there's a check in front of Android Dependencies and the support library, if you use it.

Mark all checkboxes and Click on Apply and clean the project.

Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
0

It cant be done. You will need to unzip the JARs and add them to the classpath to expose the nested JAR. See this Java bug for reference.

There are several alternatives to handling it in a proper way, see this question for example, which recommends One-jar, Uber jar or Shade

Note: This is relevant for Java, Andoid might behave different, but it seems like it has the same issue.

Community
  • 1
  • 1
Fredrik
  • 10,626
  • 6
  • 45
  • 81
  • So that's it. It cant be done your saying. One-jar or Uber jar method is bit complex and no proper tutorial too. – Naveen Kumar Dec 08 '14 at 19:03
  • No, it cannot be done by ordinary means. To verify, do a google search for "java nested jars", you will find a lot of people with the same issue. The workaround is to unzip the zar and add them unnested or using some other solution. – Fredrik Dec 09 '14 at 07:26