15

I have a standard Java project in which I've written code to parse some BBC Radio XML data from the internet. The project is called BBCSchedules and the class I'm interested in is called BBCChannel.

I'm now trying to use the BBCChannel class in an Android application. How should I go about doing this?

I've tried various things, following various bits of advice on the internet, and the place I've got to at the moment is compiling my BBCSchedules project to a .jar file, and importing that to the Android project using the Build Path/Library/Add External Jar option. However, Eclipse still doesn't recognise the BBCChannel class, and won't let me run the application because of this.

I guess I've done something silly wrong, but what is it?

UPDATE: I've tried the steps listed in How can I use external JARs in an Android project? and various other StackOverflow questions I can find that seem to be related to this, but absolutely nothing seems to work. Any other ideas?

UPDATE: The discussions I've had with the author of the first answer below suggest that it is something to do with how I am using Eclipse to attach the .jar file. The project he sent me with the .jar file already attached didn't work. Any other ideas anyone?

Community
  • 1
  • 1
robintw
  • 24,689
  • 48
  • 125
  • 196

3 Answers3

24

Put the JAR in your project's libs/ directory. Then Build Path -> Library -> Add JAR should allow you to pick the one out of your project. IIRC, this works with Eclipse.

If you decide someday to dump Eclipse, just having your JAR in libs/ is enough -- the command-line build tools will pick it up automatically.

UPDATE: If you have the R17 or newer version of the ADT Eclipse plugin, now you only need to add a libs/ directory and put your JAR in there. It will automatically be added to your build path, much like with command-line builds.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • 1
    Thanks. I've added the .jar to the libs/ directory and then selected it in the Build Path in Eclipse, but Eclipse still doesn't seem to recognise the BBCChannel class, and therefore won't let me compile and run my application. Any other ideas? – robintw Apr 23 '10 at 08:58
  • Are you sure your classes are public? – CommonsWare Apr 23 '10 at 11:20
  • Yes, my class is public, and it works fine from another simple command-line test program. I can't work out why it's not working! I can upload the files somewhere if you want... – robintw Apr 23 '10 at 15:31
  • Here is a project demonstrating incorporating a JAR into an Android application: http://github.com/commonsguy/cw-android/tree/master/Java/AndShell/ – CommonsWare Apr 23 '10 at 16:12
  • Thanks for your help. Interestingly, when I load that example project into Eclipse it complains about not being able to find the bsh module or the Interpreter class. Obviously it works for you, so it must be something to do with my configuration. Any ideas? – robintw Apr 23 '10 at 21:19
  • 2
    I don't use Eclipse, so your difficulty is somehow tied to how you are attaching the JAR to your Eclipse project. Outside of Eclipse, it's automatic. Since it's been a couple of months since I last taught my course, I am rusty on the Eclipse steps -- sorry! – CommonsWare Apr 23 '10 at 21:46
  • That's ok - I'll keep investigating. Thanks for your help anyway. – robintw Apr 23 '10 at 21:50
  • Ah - I've just been playing around with it a bit more, and I've managed to get your sample code to work, by manually going into build properties and adding the JAR file. However, doing exactly the same thing for my project doesn't seem to work. Do you think it might be something to do with my .jar file? It works fine in a standalone command-line application. – robintw Apr 23 '10 at 22:01
  • Is your "standalone command-line application" part of the JAR itself? If so, try separating that out into its own JAR, and see if it still works. – CommonsWare Apr 23 '10 at 22:45
  • Nope - it's a completely separate project, just importing the jar file. – robintw Apr 24 '10 at 11:36
  • 1
    :: shrug :: If you visit http://github.com/commonsguy, all of the "cwac-" projects are designed to build reusable JARs for Android. Take a peek at those and see if anything leaps out at you as being the source of your difficulty. – CommonsWare Apr 24 '10 at 11:59
  • @CommomsWare : I tried to add a jar according to the steps discussed here.I'm able to access the class and its methods but now I'm getting an IO Exception when I run the project..What do I do? – Deepthi Jun 05 '12 at 09:18
1

Here is what I did, and it worked :

  • Right-click on your project
  • Build Path > Add External Archives
  • Select your archive (.jar)

The library.jar will be added to the 'Referenced Libraries' in your project, but not in the folder of your project. I can now run my project with that external library, and I don't get errors, but I don't know if it is the usual way to do it. My archive is not in the 'libs' folder, as everyone. You should try what I did.

cleroo
  • 1,085
  • 2
  • 10
  • 17
1

Check the version of jar and also your system Java version. If jar version is compatible to the system version it will run.

Moreover, check the Jar by running java -jar filename.jar on the command prompt.

After that do the same thing as mentioned in the above version.

If not please pass your jar and sample code.

Peter O.
  • 28,965
  • 14
  • 72
  • 87
Kumar Shorav
  • 529
  • 4
  • 16
  • This one worked for me, thanks. Android eclipse sets java to 1.6 compliance level but I was building a JAR of other classes at 1.7 compliance level, as soon as I set my other project to 1.6 and exported the jar it all started working. – Damon Smith Feb 04 '13 at 10:45