0

I've been wondering why do some languages, such as Java run on Both Linux with little or no modification of the code, but other languages you have to nearly re-write all the code. Why is this? Because of the Kernel?

Sorry if this is a simple question but, I really don't have much of a clue.

Also, what's the difference between cpu architecture in terms of Linux.

TheBlueCat
  • 1,147
  • 5
  • 19
  • 38
  • possible duplicate of [what are the differences between an executable generated by windows and linux](http://stackoverflow.com/questions/2067524/what-are-the-differences-between-an-executable-generated-by-windows-and-linux) –  Apr 09 '12 at 19:00
  • possible duplicate of [Why an executable program for a specific CPU does not work on Linux and Windows?](http://stackoverflow.com/questions/2059605/why-an-executable-program-for-a-specific-cpu-does-not-work-on-linux-and-windows) – Greg Hewgill Apr 09 '12 at 19:03

5 Answers5

9

Technically you are wrong.

Java does not run on Linux. It also does not run on Windows. It does not run on Mac either, nor any other Unix like operating system. For that matter, it does not run on any operating system.

It runs directly on the machine. It only works on one type of machine - the Java Virtual Machine. Fortunately, the Java Virtual Machine has been virtualized onto Linux, Windows, Mac, etc.

emory
  • 10,725
  • 2
  • 30
  • 58
5

It is a really long story, but essentially:

  1. Binary executable files are always platform-dependent. Usually we say the OS+Architecture to denote platform. Examples: Windows x86, Linux x86-64, etc. This is because they mostly execute with the help of operating systems which provide abstractions for the available hardware.
  2. Bytecode (not executable) interpretable files can be independently executed because they are interpreted by a binary platform-dependent one that is already installed on the machine.

Java code is converted to a bytecode interpretable file (.class). It is interpreted and executed by a platform-dependent Java Virtual Machine that can be installed on Windows, Linux, Mac platforms.

However, you can put effort to make your C/C++ code very platform-independent, but since you have different OS (and sometimes different Architecture), you must compile that (same) code on every system you want to run it, in order to build proper binaries that will run in that specific machines.

OBS: To make your code more independent you should use libraries that are available on all systems.

OBS2: To know more about executable files a good "questions" with good "answers" is: What does executable file actually contain?

Community
  • 1
  • 1
Moacir Ponti
  • 591
  • 5
  • 14
1

Because Java is not compiled into a machine code, but into a byte code executed by JVM (Java Virtual Machine), which takes care of OS specifics (and provides a unified interface to OS capabilities to Java programs).

Eugene Retunsky
  • 13,009
  • 4
  • 52
  • 55
0

Java is built upon a virtual computer called the Java Virtual Machine, or JVM. As long as the JVM is properly implemented on a computer, all Java programs should run there with little effort. The caveat is if the code itself has operating-specific items in it, such as hardcoded path names (these differ between Unix and Windows, for example.)

Tony Ennis
  • 12,000
  • 7
  • 52
  • 73
  • I reading up about that, seems like a pretty cool way to do thing. Does any other language compile into intermediate code, per se. – TheBlueCat Apr 09 '12 at 19:00
  • @TheBlueCat: every language on .NET, I believe, using the CLR, its equavalent of the JVM. C# particularly. There are .NET versions of C++ and VB, which should not be confused with the non-.NET versions. Java used to be alone on the JVM, but there are now a bunch of languages that compile/interpret into its byte code. – RalphChapin Apr 13 '12 at 20:43
0

The problem is not with the language, but with the libraries used. I work on a project which easily uses 99% of the same code on both Windows and Linux. We can do this because we use libraries which have implementations for both Windows and Linux. The libraries encapsulate the platform-specific code. Java is the same way: It has a standard library which is implemented differently on every platform.

Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • You'd still have to compile for each platform and even architecture whereas Java is compiled only once. Thus, it's not only the libraries but also a language dependent problem. Besides that, the Java standard library is implemented in Java and thus would be the same on each platform. What differs is the JVM. – Thomas Apr 09 '12 at 19:17
  • You would indeed still have to compile for each architecture, but I disagree with having to compile for each platform. As long as two platforms are on the same architecture, it is entirely possible to run the same binary on both. – Dark Falcon Apr 09 '12 at 19:41
  • And the Java Standard Library is NOT implemented in Java. Some of it is, but certainly not all of it. – Dark Falcon Apr 09 '12 at 19:41
  • Well, it depends on how you define the standard library. True the OS specific things like GUI, file access etc. are normally implemented in a platform specific way but its mostly only what is _really_ needed to get things done. If you think of the JVM as the platform Java is running on one could consider those native implementations to be part of that "platform" (the JVM). Besides that, my point was: the libraries are only part of the reason that other languages have to be recompiled, another big part is the language itself. – Thomas Apr 09 '12 at 20:07