Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What differs between "writing a specific JRE for each platform" for Java developers and "writing a C++ compiler for each platform" for C++ ones?

share|improve this question

migrated from stackoverflow.com Sep 27 '11 at 15:42

This question came from our site for professional and enthusiast programmers.

10 Answers 10

Java is compile once run anywhere. C++ is write once compile anywhere.

share|improve this answer
Well you've obviously not had much experience with GUI development. (Waiting for Qt...) –  Glenn Nelson Sep 27 '11 at 15:58
Anyone who claims that C++ is "write once compile anywhere" has never had to port a C++ program... –  BlueRaja - Danny Pflughoeft Sep 27 '11 at 18:03
I agree with BlueRaja. Porting a C++ program means so much more than porting the compiler. C++ is most widely present in critical environments where things like the size of an int or the implementation of a file system can make such a big difference. Porting does NOT simply mean recompiling. –  rahmu Sep 27 '11 at 18:20
@Pubby8 no, portability issues also come from pretty standard code that makes nonportable assumptions, like assumptions about endianness (which mind you, you can also suffer from in Java), alignment, and sizeof fundamental types. –  R. Martinho Fernandes Sep 27 '11 at 21:20
Write once, debug everywhere. –  bhagyas Sep 28 '11 at 4:19

"writing a specific JRE for each platform" is not something you do everytime. Porting the JRE to a new platform is something you need doing only once. This task is generally done by the core maintainer/developers of the program and/or the platform. A lot of factors may come into play when deciding who and how the JRE would be ported. Among other things, it depends on the licensed it's published under (I hear Java is Open Source, so I guess anyone could do it). Funny anecdote, Steve Jobs made a big deal about not wanting to take care of the porting of Java on Mac, around a year ago.

The point is not how or who ports the JRE, but the fact that once it is ported, every Java application should now theoretically run easily on the new machine. In that sense, the JRE forms an abstraction layer, completely hiding the machine, allowing easy porting.

However, reality is not always pretty like this. I won't go as far as calling portability a "myth", but it's true that it's not so perfect. For instance, Java has a package called JNI that allows sending native calls, bypassing the JRE, thus preventing the perfect seamless portability, what Java fans like to call "Write once run everywhere".

As mentioned in the comments, C++'s approach to portability is different. On the one hand, it's a compiled language, and those binaries are almost always platform specific. So c++ executables will never be portable (unlike Java). On the other hand, porting the compiler can sometimes be enough. The community has found that by porting the compiler as well as some core libraries of the language, source codes (and not binaries) could be portable.

However, C++ is widely used in critical systems like compilers, kernels, real-time systems,embedded systems, ... There's a "low level" aspect of C++ that cannot be overlooked, when talking about portability.

share|improve this answer
Portability is not a myth. Perfect portability is. –  Malcolm Sep 27 '11 at 20:02
"his is where Java is very different from C++, where each application is "machine-specific", and cannot be ported without modifying the code." That's not true, it depends on the dependencies. If you only use the standard library and libraries with sources portable to your targets, you code once and compile on each target. If you code something that can be ported without modifying the code in C++ then the only thing you might need to modify are the build scripts. That's not even true in all cases. –  Klaim Sep 27 '11 at 20:13
Let's not forget that C++ can be used as both a high-level and a low level language. A lot of C++ code is used in programs where a 32bit int is very different from a 64-bit int. High-level will always be portable, of course. But it's far from a C++ generalization –  rahmu Sep 27 '11 at 20:25
I think you might have misunderstood what I'm saying : you can write correct C++ with int or any standard type without bothering with low-level stuff. Just take a look at boost libraries, most are only high level code, and that's true for a lot of C++ open source project. You "can" go low level, and if you do you can also avoid any specific code until you need to use platform specific API. But if you don't need to, then you can write C++ that works everywhere. Dependency to platform can be avoided and often is in library code. –  Klaim Sep 27 '11 at 22:03
Just to be sure I'm clear : I don't say that all C++ code is portable, I'm saying that as is, your statement is wrong. You might want to fix it with something like : "This is where Java is very different from C++, where each application is "machine-specific", and cannot be ported without modifying the code or, if the code really is portable and the build scripts manage the new target, without at least one compilation." –  Klaim Sep 27 '11 at 22:08

It's not just the language -- it's the libraries.

Both Java and C++ provide cross-platform libraries. Java provides a richer set.

share|improve this answer
Java provides a richer set by default. The same libraries can be found for C++ they are just not part of the standard libraries and you just have to decide which ones to use (which is not trivial especially if they are not installed). –  Loki Astari Sep 27 '11 at 15:51
Comparing the standard libraries of Java to the universe of libraries for C++ is not really a valid comparison. Java provides a richer set, whether you're comparing the standard libraries of each, or you're comparing the universe of libraries of each. –  Andy Thomas-Cramer Sep 27 '11 at 16:18
Definitely disagree with that. Anything in the Java library is available is already available for use by C++ in some library. I like the fact that Java has it all in one place but to say it is richer is just not true. Maybe the adjective you are looking for is `more integrated' –  Loki Astari Sep 27 '11 at 17:23
+1 would upvote again. I think the libraries are the biggest factor in portability. If you're working in C/C++ and doing anything other than pure computation, there will be libraries (in particular, parts of the system library) that are radically different between Windows and Unix, and subtly different between different flavours of Unix. That makes porting hard. Java basically doesn't have that problem. –  Tom Anderson Sep 27 '11 at 17:33
@Andy Thomas-Cramer: I am not comparing anything (you seem to be). I am saying your statement is inaccurate. One of the advantages Java has (and we all love it for that) is all the standard libraries in one place. Saying they are richer is just not accurate. –  Loki Astari Sep 27 '11 at 20:00

The difference is that Java will run on any platform without recompiling. Having a C++ compiler for each platform isn't the same at all.

share|improve this answer

All the answers starting with "The difference is...", or anything very similar, are basically wrong (sorry, but such is life). There are really two separate differences between the two.

One (that's been mentioned a lot) is that a compiled Java program can (or least should) run on any conforming implementation of Java, so even after being compiled, you can still move a Java program from one platform to another without re-compiling. C++ (at least normally) requires re-compilation for each target platform.

The other is that Java (at least attempts to) assure that all correctly written Java will be portable. At least in theory, you shouldn't be able to write any code that isn't portable.

C++ allows you to do quite a few things that aren't portable. The C++ standard contains "warnings" about a lot of things that aren't portable (e.g., telling you that you'll get implementation defined behavior or undefined behavior), but it doesn't necessarily try to stop you from doing them at all. Just for example, if you want to write an operating system for hardware that uses a PCI bus, you're probably going to need to read/write the PCI configuration memory. This obviously won't be portable to systems without a PCI bus, but if you're writing an operating system for hardware with a PCI bus, it's pretty much necessary. C++ allows it even though it obviously won't be portable.

share|improve this answer
C++ doesn't know anything about PCI bus nor hardware though. –  Nikko Sep 27 '11 at 18:36
of course it doesn't, those are platform specific things that will have to be included in platform specific libraries. Just as Java can have platform specific libraries if needed. –  jwenting Sep 28 '11 at 8:05
@Nikko: It knows nothing anything about it, but it allows you to use what you know about it. –  Jerry Coffin Sep 28 '11 at 13:04
@jwenting: The difference being that you can write the platform-specific libraries in C++, but you generally can't write them in Java. –  Jerry Coffin Sep 28 '11 at 13:05

You have misunderstood the premises. Java programs are very portable, because the JVM provides a standard behaviour guaranteed to be the same. C++ programs have a less standardized environment closer to the actual hardware, so the program needs to be able to handle the various platform specific details - like size of an int, word alignment etc etc etc.

The JVM itself is not very portable. It is a herculean task to port a high-performant JVM to another platform or CPU architecture.

share|improve this answer
+1 for that last sentence! –  rahmu Oct 26 '11 at 12:39

The difference is that Java (it's not an acronym) programs can be distributed in a form that can be run on any computer with a JVM installed, but C++ is normally distributed as either source code, which is very user unfriendly, or as a bunch of different binaries for different platforms.

share|improve this answer
Huh? There are C++ compilers which target the JVM and there are Java compilers which target native code. Can you cite the specific section of the C++ language specification which says that C++ programs must be distributed as either source code or platform-specific binaries? –  Jörg W Mittag Sep 28 '11 at 0:37
There, I edited my answer to use less definitive language –  Colin Sep 28 '11 at 0:57

The amount of work for the supporting tools is indeed similar, the difference lies elsewhere. Once a C++ program has been compiled for a platform, you have to compile it again if you want to use it on a different platform. However, when a java program has been compiled, you can move it to any other platform with a runtime environment without having to recompile it.

share|improve this answer

Answering the title "Is portability a myth ?", instead of "Which is better at portability, Java or C++", I'll say that partial portability is possible, but full portability it's does a myth.

Something that I insist to write, and it applies to this question, is that developers are not longer working with just programming languages, but, with full programming frameworks.

And those frameworks, include libraries, databases, graphical interfaces.

Which programming language, or which programming framework is more portable ?

Well, it depends, on what your app. is trying to achieve.

share|improve this answer

The thing is "you" don't write the JRE you write the Java code which runs on any JRE. "You" do write the C++ code which can require changes by you before it will compile on another platform.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.