Is Java really platform-independant?


 
Thread Tools Search this Thread
Top Forums Programming Is Java really platform-independant?
# 8  
Old 03-12-2012
I missed your post Radoulov... sorry repeating things... and Thanks!
# 9  
Old 03-12-2012
Yes,
sure - the JVM itself *IS* platform specific.
# 10  
Old 03-12-2012
Code:
 
I believe the error you're seeing is related to JDK version mismatch.

Thanks radoulov buddy! You are absolutely right, there was a mismatch in JDK version. Java is really platform-independant, I think I've confirmed it. Thanks for your help.
Problem solved!
# 11  
Old 03-12-2012
Hi.

Googling java Unsupported major.minor version produces essentially the answer provided by radoulov. The second of about 86K hits contained:
Quote:
It means that you compiled your classes under a specific JDK, but then try to run them under older version of JDK. So, you can't run classes compiled with JDK 6.0 under JDK 5.0. The same with classes compiled under JDK 7.0 when you try to run them under JDK 6.0.

-- Unsupported major.minor version 51.0 | Java.net
Google is your friend.

Best wishes ... cheers, drl

---------- Post updated at 11:18 ---------- Previous update was at 09:51 ----------

Hi.

Here is a script that compiles a java code on a 64-bit box with Sun java:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate translation of java code.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
edges() { local _f _n _l;: ${1?"edges: need file"}; _f=$1;_l=$(wc -l $_f);
  head -${_n:=3} $_f ; pe "--- ( $_l: lines total )" ; tail -$_n $_f ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C java

FILE=${1-hello.java}

pl " Input data file $FILE:"
cat $FILE

pl " Results, compile and execute:"
javac $FILE
ls -lgG hello*
java -verbose hello |
specimen 3

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
java version "1.6.0_22"

-----
 Input data file hello.java:
public class hello
{
  public static void main( String[] args )
  {
    System.out.println( "Hello, world from java." );
  }
}

-----
 Results, compile and execute:
-rw-r--r-- 1 427 Mar 12 11:03 hello.class
-rw-r--r-- 1 127 Mar 12 10:34 hello.java
Edges: 3:0:3 of 308 lines in file "-"
[Opened /usr/lib/jvm/java-6-sun-1.6.0.22/jre/lib/rt.jar]
[Loaded java.lang.Object from /usr/lib/jvm/java-6-sun-1.6.0.22/jre/lib/rt.jar]
[Loaded java.io.Serializable from /usr/lib/jvm/java-6-sun-1.6.0.22/jre/lib/rt.jar]
   ---
Hello, world from java.
[Loaded java.lang.Shutdown from /usr/lib/jvm/java-6-sun-1.6.0.22/jre/lib/rt.jar]
[Loaded java.lang.Shutdown$Lock from /usr/lib/jvm/java-6-sun-1.6.0.22/jre/lib/rt.jar]

Now the code will be transferred to a 32-bit box running GNU java, and only executed with this script:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate execution of 64-compiled java on 32-bit platform.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
edges() { local _f _n _l;: ${1?"edges: need file"}; _f=$1;_l=$(wc -l $_f);
  head -${_n:=3} $_f ; pe "--- ( $_l: lines total )" ; tail -$_n $_f ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C java

FILE=${1-hello.class}

pl " Input data file $FILE:"
file $FILE

pl " Results, execute:"
ls -lgG hello*
pe
java -verbose hello 2>&1 |
specimen 3

exit 0

producing:
Code:
./s1

Environment: LC_ALL = C, LANG = en_US.UTF-8
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-686, i686
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
java version "1.5.0" ( gij (GNU libgcj) version 4.3.2 )

-----
 Input data file hello.class:
hello.class: data

-----
 Results, execute:
-rw-r--r-- 1 427 Mar 12 11:03 hello.class
-rw-r--r-- 1 127 Sep 17  2009 hello.java
-rw-r----- 1 108 Jan  6  2008 hello.java.orig

Edges: 3:0:3 of 256 lines in file "-"
[Loaded (pre-compiled) java.lang.ClassLoader from <no code source>]
[Loaded (pre-compiled) java.lang.Object from <no code source>]
[Loaded (pre-compiled) java.lang.Class from <no code source>]
   ---
[Loaded (pre-compiled) java.util.Vector$1 from <no code source>]
[Loaded (bytecode) hello from (file:/home/drl/try/java/hello/ <no certificates>)]
Hello, world from java.

My conclusion is that for simple codes, java is platform independent. Whether or not this holds for complex codes is left as an exercise.

Best wishes ... cheers, drl
Login or Register to Ask a Question

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