38

I'm fairly new to Java and Android programming in general but I would like to create a small Java library for sharing some code between an ordinary (non-Android) Java application and an Android application; so my question is what is the best way of creating an ordinary Java library with Eclipse?

I didn't find anywhere the option for creating a new Java library project from Eclipse; does this mean that I must use an ordinary Java application as my starting point? As this will be used for an ordinary Java application as well, I cannot use an Android library.

Also, what would be the options that I must set up?

Finally, as this library will be in the same workspace as the application projects, is it still mandatory to create a JAR file if I can establish a direct link to the library project?

Ed The ''Pro''
  • 875
  • 10
  • 22
SylvainL
  • 3,926
  • 3
  • 20
  • 24

2 Answers2

36

You don't have to create any library, if you use the same workspace.

Option 1: Just use the source

In the properties of the project which has the dependencies you can add another source-folder:

Properties > Java Build Path > Tab: Source > Add Folder...

In the Project Tab you can "add" the whole project to the other project, too. There are many ways to achieve your goal.

Option 2: Create and add the library to the Build-Path

Adding an existing Jar (your own library):

If it is in the workspace:

Properties > Java Build Path > Tab: Libraries > Add JARs...

If it is somewhere on the drive:

Properties > Java Build Path > Tab: Libraries > Add External JARs...

Exporting a source folder as a library:

Context Menu of Source Folder > Export > Jar File

There are two types: Executable Jars and "normal" Jars. You don't need an executable Jar.

C12-H22-O11
  • 557
  • 3
  • 6
  • Thanks, adding the whole project under the Project tab works but only if I've also select it in the "Order and Export" tab. Creating a Jar also work. I'll try the other option later. – SylvainL Jun 26 '12 at 20:14
  • Did adding the whole Java library project under the project tab for the Android project work? For me this compiles, but gives a `NoClassDefFoundError`. – Felix May 26 '13 at 21:00
  • "You must not create any library" - do you mean "You don't have to"? – kraxor Jun 27 '14 at 11:29
  • Probably the easiest approach is to put the code in an Android Library Project and then reference the jar it generates in the bin directory (option 2) for your non-android project. The jar files that the Android environment generates are still standard java 6. As long as you don't call any code which tries to load Android specific classes they will run on the PC. – JT. Jul 03 '14 at 16:50
7

A Java library is basically a set of JAR files. So what you can do is generate a JAR based on your source code and add it as a external JAR to the Java build path of your Android and non-Android projects.

Ed The ''Pro''
  • 875
  • 10
  • 22
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
  • 4
    Thanks, exporting a Jar from an ordinary java project works perfectly. The documentation for Eclipse should make it clearer that there is no independant project or option for creating a java library. – SylvainL Jun 26 '12 at 20:13