Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Java cross platform?

I want to develop a cross platform application.

Is Java cross platform? I mean, can I develop a Java application in Windows and use it in Mac OS X and Linux?

If yes, how?

I find application written in Java, has two setup file one for Windows and other for Mac. This confuses me.

Any illustration or suggestion will be highly appreciated.

like image 468
AMH Avatar asked May 12 '11 12:05

AMH


People also ask

Why is Java not cross-platform?

Java is, but as others have said, it's setup is not (like setting JAVA_HOME / making sure Java is in your path etc.). That's really the point. A ready-for-use application in C/C++ will only run on the platform it was built to and is therefore by definition not cross-platform. Java is, as are other languages like HTML.

Can Java be run on any platform?

Java code can run on any hardware platform, operating system, and browser. It can run on any device. This means that once you have written your application in Java, you can run it on any device.

On which platforms does Java not run?

Java runs mostly on all platforms. For running a Java program we need JVM on all the platforms. If JVM is not installed on any platform than we are not able to run Java programs. Java API has standard library and API that can run on any platform.

Can Java only run on Windows?

Java is Platform independet. This says it all.It can run on any operating system because it compiles to bytecode which then runs on a Virtual machine.


3 Answers

Is Java a cross platform?

Java is cross platform in the sense that a compiled Java program runs on all platforms for which there exists a JVM. (This holds for all major operating systems, including Windows, Mac OS and Linux.)

I mean I can develop Java application in windows and use it in mac and Linux?

Yes, this is possible.

This (and the security aspect) is one of the main advantages of running the programs in a virtual machine.

If yes how?

  • Write your application in Java (In .java files)
  • Compile your application using Eclipse or javac (into .class files)
  • (Optionally) Bundle your .class files in an executable (.jar file)

The very same .jar file can be distributed and executed on Windows systems, Mac systems, etc.

I find application written in Java, has two setup file one for windows and other for mac. This confuses me.

This is because some applications rely on platform-specific features. They are then bundled with different platform-specific libraries.

Unless you're developing an application that itself relies on platform-specific features, (such as for instance low-level system calls), you should be able to do just fine with ordinary Java.

Important comment by @Peter Lawrey:

It can be while the application is platform independent, the setup program is not. e.g. IntelliJ has three platform specific installers which are not written in Java, but have a zip which you can just unzip on any platform and it will work.

like image 128
aioobe Avatar answered Sep 23 '22 20:09

aioobe


You can run Java applications on every device that has a JVM. If it does not, you're out of luck.

The comment from Oded is important. C and C++ have compilers on lots of devices.

Java byte code won't need to be recompiled when you switch platforms.

C and C++ will require that the developer recompile the application before distributing it to the target system, but once that's done clients will be able to run without an issue.

The problem of platform-specific customizations and settings has to be dealt with no matter which language you choose. The more your application depends on platform-specific features, the less portable it will be.

UPDATE:

Let's revisit the original words in the question:

I want to develop a cross platform application.

Here's the objective - a direct quote. No details about web, mobile, or desktop app.

Is Java cross platform? I mean, can I develop a Java application in Windows and use it in Mac OS X and Linux?

Define "cross platform". Sounds like the bias here is "byte code portability". C/C++ can't do that, but "source code portability" is certainly possible as long as you stick to ANSI C/C++ and refrain from using vendor extensions.

Java's claim to fame from the beginning has always been byte code portability. That's what the JVM gets you. That does not mean your whole application will be portable, because you might not have managed other dependencies well.

If I substitute "C/C++" for "Java in that bloc, then cross platform means something different. I cannot pick up a .exe or .so compiled for one platform and expect to run it on another, but if I create an .exe or .so for each platform and make them available it's certainly possible to make the same source code runnable on multiple platforms.

If yes, how?

If you have packaged your Java app as a JAR, you can put that on any platform you like.

If you have multiple C/C++ .exes for the platforms you're interested in, you can certainly run it when you need to.

like image 21
duffymo Avatar answered Sep 23 '22 20:09

duffymo


There is an important caveat with regard to Java portability. "Business logic" (non-UI stuff) is quite portable, but there are at least a half-dozen different (and incompatible) user interface paradigms for Java, so, eg, Java code written to run on an Android (even ignoring Android's incompatible JVM) won't run on a Nokia phone, and code for either one will not run on a desktop PC.

But there's no other language that does better, to my knowledge.

like image 26
Hot Licks Avatar answered Sep 22 '22 20:09

Hot Licks