This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes class file portability Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class file portability" Watch "class file portability" New topic

class file portability

John Robertson

Joined: Feb 12, 2007
Posts: 7
I had thought that the .class file one created after compiling a piece of java code was supposed to run under a different operating system. I tested this with some simple java code

public class HelloWorldApp {
public HelloWorldApp() {
public static void main(String[] args) {
System.out.println("Hello World!");

I compiled it to a .class file on my home machine (Win XP). It runs fine. I transfered the .class file to my office machine (Linux). It won't run. If I Compile it on my office machine, the resulting .class file runs fine.
However, I thought the .class file itself was supposed to be portable, not just the original uncompiled code. It is true that at home I have Java 1.6.0 and at work I have Java 1.5.0_02-b09, but does that small of a difference really mess up "write once, run anywhere"?


Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
at java.lang.ClassLoader.loadClassInternal(
Tom Cleal

Joined: Jan 22, 2007
Posts: 13
Yes it does matter, Sun's version naming is a little screwy. From what I understand they are trying to change that so the versions are more 'normal'. Java 1.5 = Java 5, Java 1.6 = Java 6. Code compiled under Java 5 should run under Java 6 but not the other way around.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

Class files from a newer version of Java in most cases do not run on an older version of Java. So if you compiled your code with JDK 6, and try to run it on Java 5 then you'll get an error like this. The other way around it obviously works without problems; you can run a class compiled with JDK 5 on Java 6.

Note that the Java compiler has command line switches you can use, -source and -target, that you can use to specify that you want class files compatible with a particular Java version. That way you can compile Java class files with JDK 6 that run on an older version. But note, if you are using classes or methods in your code that are new in version 6 of the standard Java API, it will ofcourse still not work on the older Java version.

Note that the operating system doesn't matter, only the Java version. Code compiled with JDK 6 will run on any JRE version 6 or higher on any operating system.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
I agree. Here's the link:
subject: class file portability
Similar Threads
Strange Problem in Running Jar!
main() & package question
Running Javaflow
Packageing a Jar does not work
truncated class file problem