5

I am fairly new to Java or programming in general. On my journeys through the internet to master this language I have come up the saying "write once run anywhere" multiple times.

But I have found many software that requires you to pick the right version for your OS. Sometimes there is only one version available.

Could you explain to me why that is so?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
TomTom
  • 2,820
  • 4
  • 28
  • 46
  • 3
    Java runs on a Virtual Machine, the JVM. In an ideal world this means that the Operating System is abstracted away behind this and you only have to make sure your code works with the JVM which will make it work with the underlying OS. This can already be undone by using the wrong path separators or line endings; it is not an absolute truth. – Jeroen Vannevel Jun 22 '14 at 09:03
  • 1
    @JeroenVannevel care to post it as an answer or community wiki..? :) if we keep contributing in comments the number of *unanswered* questions will keep on increasing... – T J Jun 22 '14 at 09:10
  • 1
    @ TomTomTom: Can you give a few examples? My experience of using Java software doesn't match with your statement that "many" programs require you to download a version specific to your OS. Normally there's just one, or two (one for everyone but Windows, one for Windows), which is usually about the installer. – T.J. Crowder Jun 22 '14 at 09:21
  • Don't confuse initial developer dreams/marketing-speak with crude realities... – vonbrand Jun 22 '14 at 10:15

5 Answers5

11

[expanded per the comments]

Java runs on a Virtual Machine, the JVM. In an ideal world this means that the Operating System is abstracted away behind this and you only have to make sure your code works with the JVM which will make it work with the underlying OS. This can already be undone by using the wrong path separators or line endings; it is not an absolute truth.

An application can use many Operating System-specific approaches/libraries/functions/etc that might make it not feasible to restrict yourself to one general codebase. Instead they might want to leverage some advantages provided by a platform and create a separate application with it.

The statement should probably be somewhere along the lines of "Write once in a general fashion, run anywhere" but that's not as snappy.

This statement is often linked to Java but there are also other languages that incorporate this: weblanguages like Javascript and HTML will run on any browser because the browser itself forms the abstraction between the language and the underlying OS.

Other languages don't have this (entirely?) since they work differently: C# will use the underlying .NET framework which as it is only exists for Windows. There exists a cross platform variant (Mono) but it would be an overstatement to consider C# truly cross platform.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
3

The Java program or source code is compiled to generate "bytecodes" ( an intermediate binary format). Second, the bytecodes is executed by an interpreter which is part of the Java Virtual Machine (JVM). "Write Once, Run Everywhere" refers to the fact that an application written is Java can be run on any hardware which has the Java Virtual Machine (JVM), and that the JVM is now licensed to hundreds of operating systems vendors systems including Microsoft for Windows.

segfault
  • 504
  • 2
  • 6
2

Well, some stuff is really cross-platform (most of standard Java library), while some other stuff may need right version for the OS. Generally, this applies to software that uses dynamic libraries, that aren't written in Java. In this case, versions for different OSes are packed with libraries for this OS.

In case there's version only for one OS - it could be because required libraries don't exists for other OSes or developer didn't port it. There is still possibility that it's actually cross-platform, but developer tested it only under one OS.

CubiX
  • 119
  • 1
  • 3
0

Your question is more based on platform dependency/independency.

Java is a programming language which is platform independent which means the code which you write will produce the same output on all machines running Windows, Linus, Unix, etc... without any changes to the code. To run a Java program you need to have JVM (Java Virtual Machine) installed. Now what does JVM do.? Well, it translates your code into Machine code which the Operating System could understand. Therefore JVM is platform dependent since every OS has a different Machine code.

So, basically you write a Java Program only once and can be used/run everywhere.

  • Just like standards-conforming C is platform-independent, just recompile. But in practice you are almost forced to use platform-specific features, specific support libraries that aren't available everywhere, there are differences from one version to the next, ... – vonbrand Jun 22 '14 at 10:20
0

you can write source code in one platform and run it any where.some times you can encounter problem if a new java version is available but still current version runs the code.

dinu1389
  • 138
  • 2
  • 9