4

I am trying to reference a pure java-Project in my Android-Project -> The Java-Project has a whole bunch of classes I need to use. Oh, and as the first response pointed out: I am using eclipse, yes :)
Only Problem is: I Always get Could not find class 'XXX', referenced from method com.example.helloworld.MainActivity.onCreate. I seem to have missed SOME step or error...?
What I already did:

  • The Project is added as Project into the Java Build Path (logically nessecary)
  • It is marked in "Order and Export" in the Java Build Path and pushed to the top (this solved the problem for someone else here when dealing with Jar-files)
  • Ir is marked in "Project References" on Project settings.
  • I added the folder where the relevant class is under "Libraries" in build Path... I am not sure if that should be nessecary.

The Java-Project I reference has a whole load of Jars, but if that is the reason, should I not get a different errormessage?

Layna
  • 457
  • 5
  • 19
  • Have you found and fixed the actual problem? Having the exact same issue, pure java project referenced and getting errors about the classes and methods in the referenced project. – Lennert Aug 16 '13 at 08:44
  • I know it has been ages, but I just foudn this again.. it was indeed a problem with the Java Versions, I must have messed those up some time during my setup. – Layna May 02 '14 at 14:18

3 Answers3

2

I had the same issue and after some hours of frustration and search I have finally found the answer here: Android, class not found from imported jar file

Basically, the issue was that the referenced pure-java project or the generated jar was built with Java 1.7, and Android projects are set to use 1.6.

Community
  • 1
  • 1
Alex D
  • 663
  • 1
  • 8
  • 18
1

Two ways to do this.

  • Jar that java project and copy the jar to the libs folder of your Android project.
  • Add the project as a dependent project to your Android project

Both work wonderfully.

But mind you, this pure-java project must add android.jar and not things like rt.jar :). Else you will get Dalvik exceptions.

Edit : Dont forget to refresh and Project - Clean your android project.

taxeeta
  • 1,188
  • 1
  • 12
  • 25
0

Two things to check (you mentioned you are using Eclipse):

  1. If your android project needs to use the pure-java project's JARs, check that those JARs are being exported in the build path options of the pure-java project. This is in the build path of the pure-java project you are trying to include, not the build path of the android project that is trying to include it. Under "Order and Export" tab for the pure-java project, note the comment "Exported entries are contributed to dependent projects". In this case, your android project is the dependent project and your pure-java project needs to do the contributing.

  2. Make sure the pure-java project is actually opened in the IDE in addition to your android project. Probably trivial but sometimes overlooked.

If answer does not suffice, you may wish to mention if the missing classes are in .java source files, or if they are only found in JARs in the build path of the pure-java project

Edit: To further track down the issue, please confirm the following to make sure I understood your question:

  1. The pure-java project does successfully build in the IDE

  2. The 'class not found' error is one you are getting when you try and -build- your android project and not when you try and -run- it.

  3. CLASSPATH (aka build path) can be nasty to untangle. If including the project is not working (assuming it does build), you could try building a JAR of your pure-java project and copying (and including) that and all the other JARs into your android project.

  4. The android SDK does certainly complicate the build environment. One way to find out whether the problem is (A) your android project setup) OR (B) the way the pure-java project is packaged) is to create a different pure java project and try including the first one, preferably using the -exact- same line of code that gives you the build error in the android project, if possible.

taxeeta
  • 1,188
  • 1
  • 12
  • 25
user1445967
  • 1,520
  • 4
  • 14
  • 30
  • I had actually totally missed number 2... still not working, but this might be the trail there. And the not-found-class is a source-file, but somewhere up the line is dependent in a Jar. Also, I found a dependence on a javax-package... I am afraid my problem might lie there? – Layna Apr 18 '13 at 10:39
  • Ok, I am on the problem again, and still no success. I now have a Test-project, pure java, with one tiny class in it. Without using the external class, the Android-project (really just a Hello World) runs perfectly. The moment I add the External class, the building still succeeds, but the whole thing crashes when I try to run it on my Device (Nexus 7, in case that helps). Still, thanks already for all the pointers... will upvote when I can ;) – Layna Apr 19 '13 at 13:13
  • "NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time." I have had this problem many times, seemingly with different fixes. Check some of the answers here, it is almost certainly about finding the right way to include your jar file -http://stackoverflow.com/questions/8678630/noclassdeffounderror-android – user1445967 Apr 26 '13 at 08:44