Java – Can Java applications run on phones (at least android) AND Windows

androidjavajvmportabilitywindows

Hi all I love stackoverflow so helpful.

Afaik IOS has some security measures that dont let other apps (such as jvm) execute code from your app. Not sure how much this is true.

But anyway, I heard that there are some problems when you try to run java application targeted for Windows on android phone.

Is that because some libraries (or most) are not cross platform? Or maybe even standard library is not cross platform (at least not completely)?

So basically if I wanted to code app for both windows,linux,mac,etc AND android, would I have to create functions like:

  IF unix THEN unixfunction()
  IF android THEN androidfunction()

or I wouldnt have to worry about it?

What should I know if I want to target both android phones AND computers (windows,linux,etc)? Is the above approach okay or I need to know something else? Is this even possible?

Sorry for a lot of questions, But portability was why I got interested in Java.

Best Answer

Java applications are run on a java virtual machine, which is what allows them to work on multiple platforms. However, android sdks have quite a few additional libraries and features pertaining exclusively to an android-compatible application that would make direct porting to windows impossible without adaptation.

That said, there are libraries which when used will allow an application for android to work on windows, with the exception to features of the android phone which obviously absent when run on windows. My guess is these libraries would provide a means to distinguish, but ideally you would want to distinguish as little as feasibly possible.

So in short no, not without some adaptations. If you're considering creating a program which will work on both, I would recommend you first find one such library, as it will be far more difficult to change later.