views:

383

answers:

3

Hi there,

I'm new to Java/Eclipse/Android, so this is probably an easy (if not stupid) question:

After creating a new Android project, I want to import some (what I think are standard) java libraries.

However, some import statements throw an error:

HelloAndroid.java:

import java.awt.Color; (The import java.awt.Color cannot be resolved)

import javax.imageio.ImageIO; (The import javax.imageio cannot be resolved)

while others don't:

import java.io.File; (no error)

I've done some googling and tried to add these classes to my classpath by going to project->properties->libraries but I haven't been able to figure out what to do exactly.

Any pointers are greatly appreciated!

Cheers,

Martin

+4  A: 

Android provides its own API and only a subset of the standard Java API.
awt and imageio are not included in the Android API, so you can't use it.

For example, if you want to manipulate images, use classes from the android.graphics package.

Desintegr
+1  A: 

Although Android apps are written in Java, they're not executed on the JVM, but instead a smaller virtual machine called Dalvik. This machine only supports a subset of the classes supported by the standard Java VM, so it's likely that those classes are not part of that support-set.

In general:

When you're not sure where the classes that you want are located (i.e., what package they're in), one method of discovering it is to simply not try to import them, but instead just use them in your code. Most IDEs will highlight your code to tell you that you need to import that class before using it, and they'll either a) add the import statement if there's only 1 class with that name, or b) provide a list of options containing various packages that all have that class in it, from which you can choose which you want.

EDIT: Technically, the language is not Java. Check out the comment below.

Shakedown
Android isn't technically Java. It just uses the same language syntax, many of the same libraries and the runtime follows many of the same rules. But since it's not proper Java it doesn't have to (and thus doesn't) include everything from the standard library.
Adam Batkin
A: 

As other people have indicated, not all Java classes are present in Android. If you want to check if a particular class is present, the easiest thing to do is to check the reference docs.

Erich Douglass