Is Java really platform-independant?


 
Thread Tools Search this Thread
Top Forums Programming Is Java really platform-independant?
# 1  
Old 03-12-2012
Is Java really platform-independant?

Hi friends,
I hope everybody is fine and doing well. I had a previous thread quering about system calls. Unfortunately, you got the impression that I was discussing some kind of homework with you guys, and you got the thread closed. Infact, there was no homework involved there, it was just an idea that came to mind, when I was writting a c program using the read() system call. I hope this question of mine is not mistaken for a homework.

I am in the process of learning Java, with the help of a very good book titiled, "Java The complete Reference". There it is mentioned that java is a platform-independant language, and it can be interpreted anywhere regardless of the underlying hardware or operating system. About Java they say, "Write once, run anywhere" unlike in C/C++ which says, "Write once, compile anywhere". Java programs are compiled into an intermediary form, "the byte code" with .class extension, which can be interpreted by the Java Virtual Machine.

Here is my query, and a small java program which I want to run on a different platform.


Code:
 
// File name Java_Port.java
// Compiled on x64 architecture running Windows 7
// To be run on Solaris 10 , SPARC architecture running in 32-bit mode
 
public class Java_Port
{
        public static void main(String args[])
        {
                  System.out.println("Java is portable!");
        }
}

I compiled it by

Code:
javac Java_Port.java

I got the the file Java_Port.class

Interpreting it on the same computer where it has been compiled, it runs successfully.

But when I transfer this class file to a SPARC machine, with the latest version of JDK, it doesn't run there.

It says,

Code:
 
$ java Java_Port
Exception in thread "main" java.lang.UnsupportedClassVersionError: Java_Port : Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Java_Port.  Program will exit.

The same problem persists whenever I run the same .class file on different versions of Microsoft Windows.

I am a novice as far as java is concerned, so if I have stated any thing silly, I beg your pardon for that.

Looking forward to your wonderful and helpful replies!

Thanks in advance!
# 2  
Old 03-12-2012
Any program COMPILED on one architecture becomes architecture specific, the resulting binary will only be able to run on that platform...
Do not mix a language with result of compilation, Java is portable because you can get java for almost any architecture and so you can then use your code... and compile again Smilie
# 3  
Old 03-12-2012
So what is the difference between Java and C/C++ then? You can also get C/C++ compilers for any plaform you want. I don't figure how Java is more protable or "Write once, run any where". According to you Java should also say, "Write once, compile and run any where". So what's the different. And where does the bytecode thing fit in the equation, I don't really get it dear!
Thanks for your wonderful reply anyway!
# 4  
Old 03-12-2012
Quote:
So what is the difference between Java and C/C++ then?
The ease at reading the code you did not write in a language you dont know but have to debug... Smilie

Quote:
According to you Java should also say, "Write once, compile and run any where". So what's the different.
Yes, You just have to "copy" your code (no rewrite then! )... true compiling is generating a machine language and so is processor dependant...(plus libraries... etc...)


All the best
# 5  
Old 03-12-2012
My dear friend,
Could you please have a look at wikipedia
Java class file - Wikipedia, the free encyclopedia
which clearly says
Code:
 
In the Java programming language, source files (.java files) are compiled into (virtual) machine-readable class files which have a .class extension. 
Since Java is a platform-independent language, source code is compiled into an output file known as bytecode, which it stores in a .class file. 
If a source file has more than one class, each class is compiled into a separate .class file. These .class files can be loaded by any Java Virtual Machine (JVM). 
JVMs are available for many platforms, and the .class file compiled in one platform will execute in a JVM of another platform. 
This makes Java platform-independent.

What do you say about that?
# 6  
Old 03-12-2012
I believe the error you're seeing is related to JDK version mismatch.
I suppose that the latest version on the SPARC machine is older than the one used for the compilation on Win 7.
So:

1. Check the version on the Solaris machine: java -version.
2. Use the same version to compile on Windows
(I have very limited experience with Java, but I believe you need to use at least the same major version:
if you compile the code with JDK 7, it won't work with 6,
if you use 6 for compilation, it will work with both 6 and 7).
3. Try to execute the .class file that you've compiled on Windows on the SPARC machine again.

The JVM will hide the specific platform dependencies.
This User Gave Thanks to radoulov For This Post:
# 7  
Old 03-12-2012
Yes true but the java virtual machine is still architecture dependant... you will have difficulties running a 64 bit binary on a 32 bit JVM... and java is very "version dependant" ... have you checked?
Why if it were so miraculous that big vendors (e.g. oracle) when giving installation software using java distribute their version in it?
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question