janpihlgren
Posts: 62
Joined: Thu Jul 26, 2012 4:00 am

Solved.Java.Unsupported major.minor version 51.0

Wed Jun 04, 2014 11:39 am

I hav compiled the following program:

Code: Select all

// File Name GreetingServer.java

import java.net.*;
import java.io.*;

public class GreetingServer extends Thread
{
   private ServerSocket serverSocket;
   
   public GreetingServer(int port) throws IOException
   {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(10000);
   }

   public void run()
   {
      while(true)
      {
         try
         {
            System.out.println("Waiting for client on port " +
            serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();
            System.out.println("Just connected to "
                  + server.getRemoteSocketAddress());
            DataInputStream in =
                  new DataInputStream(server.getInputStream());
            System.out.println(in.readUTF());
            DataOutputStream out =
                 new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to "
              + server.getLocalSocketAddress() + "\nGoodbye!");
            server.close();
         }catch(SocketTimeoutException s)
         {
            System.out.println("Socket timed out!");
            break;
         }catch(IOException e)
         {
            e.printStackTrace();
            break;
         }
      }
   }
   public static void main(String [] args)
   {
      int port = Integer.parseInt(args[0]);
      try
      {
         Thread t = new GreetingServer(port);
         t.start();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}
/code]
And when runnig with command:
java GreetingServer 6066
I get the following errormessage:
[code]
Exception in thread "main" java.lang.UnsupportedClassVersionError: GreetingServer : Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Could not find the main class: GreetingServer. Program will exit.
What's wrong?
Last edited by janpihlgren on Fri Jun 06, 2014 5:17 am, edited 1 time in total.

elatllat
Posts: 1337
Joined: Sat Dec 17, 2011 5:05 pm

Re: Java.Unsupported major.minor version 51.0

Wed Jun 04, 2014 12:42 pm

a 1.7 JRE can run classes compiled by 1.7 or less (1.6), it will spit out that error if you feed a 1.7 JRE a 1.8 class. When compiling you can spesify what version you want, but it defaults to the latest.
SBC with 32GB RAM: https://hardkernel.com

FAQ : https://raspberrypi.stackexchange.com

Unanswered: https://www.raspberrypi.org/forums/search.php?search_id=unanswered

janpihlgren
Posts: 62
Joined: Thu Jul 26, 2012 4:00 am

Re: Java.Unsupported major.minor version 51.0

Wed Jun 04, 2014 3:24 pm

OK.
Can you give me an example on how I can compile in openjdk-1.6 when I have both openjdk-1.6 and openjdk-1.7 installed?
I'm also using Mageia 4 and there is no problem compiling java with javac.

elatllat
Posts: 1337
Joined: Sat Dec 17, 2011 5:05 pm

Re: Java.Unsupported major.minor version 51.0

Wed Jun 04, 2014 4:38 pm

From "man javac"

javac -version
javac 1.7.0_40

Code: Select all

Cross-Compilation Options
       By default, classes are compiled against the bootstrap and extension classes of the platform that javac shipped with. But javac also supports cross-compiling, where classes are compiled against a bootstrap and extension  classes
       of a different Java platform implementation. It is important to use -bootclasspath and -extdirs when cross-compiling; see Cross-Compilation Example below.

          -target version
             Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5),
             1.6 (also 6), and 1.7 (also 7).

          The default for -target depends on the value of -source:

             o If -source is not specified, the value of -target is 1.7

             o If -source is 1.2, the value of -target is 1.4

             o If -source is 1.3, the value of -target is 1.4

             o If -source is 1.5, the value of -target is 1.7

             o If -source is 1.6, the value of -target is 1.7

             o For all other values of -source, the value of -target is the value of -source.

          -bootclasspath bootclasspath
             Cross-compile against the specified set of boot classes. As with the user class path, boot class path entries are separated by colons (:) and can be directories, JAR archives, or ZIP archives.

javac -version
javac 1.8.0_05

Code: Select all

Cross-Compilation Options
       By  default,  classes  are  compiled  against  the bootstrap and extension classes of the JDK that javac shipped with. But javac also supports cross-compiling, where classes are compiled against a bootstrap and
       extension classes of a different Java platform implementation.  It is important to use -bootclasspath and -extdirs when cross-compiling; see Cross-Compilation Example below.

       -target version
              Generates class files that will work on VMs with the specified version.  The default is to generate class files to be compatible with 1.2 VMs, with one exception. When the -source 1.4 option is used, the
              default target is 1.4.  The versions supported by javac are:

              1.1    Ensures that generated class files will be compatible with 1.1 and later.  VMs.

              1.2    Generates class files that will run on 1.2 and later VMs, but will not run on 1.1 VMs.

              1.3    Generates class files that run on VMs in the Java 2 SDK, v1.3 and later, but will not run on 1.1 or 1.2 VMs.

              1.4    Generates class files that will run on VMs in JDK 1.4 and later, but will not run on 1.1, 1.2, or 1.3 VMs.

              1.5    Generate class files that are compatible only with JDK 1.5 VMs.

              5      Synonym for 1.5

       -bootclasspath bootclasspath
              Cross-compiles against the specified set of boot classes.  As with the user class path, boot class path entries are separated by colons (:) and can be directories, JAR archives, or ZIP archives.

       -cldc1.0
              Use to compile CLDC programs. The compiler generates stack maps making the use of the preverifier unnecessary.

       -extdirs directories
              Cross-compiles against the specified extension directories.  directories are a colon-separated list of directories.  Each JAR archive in the specified directories is searched for class files.

so try
javac -target 1.6 whatever.java
SBC with 32GB RAM: https://hardkernel.com

FAQ : https://raspberrypi.stackexchange.com

Unanswered: https://www.raspberrypi.org/forums/search.php?search_id=unanswered

janpihlgren
Posts: 62
Joined: Thu Jul 26, 2012 4:00 am

Re: Java.Unsupported major.minor version 51.0

Thu Jun 05, 2014 1:50 am

Neither javac -target 1.6 whatever.java nor javac -source 1.6 whatever.java will change the result:
When runing the java program I get the same error:

Code: Select all

.....
Exception in thread "main" java.lang.UnsupportedClassVersionError: URLDemo : Unsupported major.minor version 51.0
......
javac -version gives
javac 1.7.0_25

raspberrypivictim
Posts: 30
Joined: Sun Jan 22, 2012 10:35 pm

Re: Java.Unsupported major.minor version 51.0

Thu Jun 05, 2014 10:41 am

Can you please post the output of both javac -version and java -version on your system?

I think the problem may be different versions between them.

janpihlgren
Posts: 62
Joined: Thu Jul 26, 2012 4:00 am

Re: Java.Unsupported major.minor version 51.0

Thu Jun 05, 2014 12:08 pm

Here are the versions:
i@Cisero ~/Development/Cprog/Internet $ javac -version
javac 1.7.0_25
pi@Cisero ~/Development/Cprog/Internet $ java -version
java version "1.6.0_31"
OpenJDK Runtime Environment (IcedTea6 1.13.3) (6b31-1.13.3-1~deb7u1+rpi1)
OpenJDK Zero VM (build 23.25-b01, mixed mode)
and as you suspected the versions differ.
Do you think I have to uninstall java and install it again?

User avatar
DougieLawson
Posts: 40564
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Java.Unsupported major.minor version 51.0

Thu Jun 05, 2014 12:29 pm

If I were you I'd get rid of all Java6 and Java7 versions and install Java8 runtime and JDK as a fresh clean & consistent install

sudo apt-get install oracle-java8* # should do that.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

janpihlgren
Posts: 62
Joined: Thu Jul 26, 2012 4:00 am

Re: Java.Unsupported major.minor version 51.0

Fri Jun 06, 2014 3:53 am

Doesn't change the result.
When running apt-cache search oracle-java I get the following message:
oracle-java7-jdk - Java™ Platform, Standard Edition 7 Development Kit
oracle-java8-jdk - Java™ Platform, Standard Edition 8 Development Kit
No jre!
When running apt-cache search jre I get the following message:
default-jre - Standard Java or Java compatible Runtime
default-jre-headless - Standard Java or Java compatible Runtime (headless)
gcj-4.4-jre - Java runtime environment using GIJ/classpath
gcj-4.4-jre-headless - Java runtime environment using GIJ/classpath (headless version)
gcj-4.4-jre-lib - Java runtime library for use with gcj (jar files)
gcj-4.6-jre - Java runtime environment using GIJ/classpath
gcj-4.6-jre-headless - Java runtime environment using GIJ/classpath (headless version)
gcj-4.6-jre-lib - Java runtime library for use with gcj (jar files)
gcj-4.7-jre - Java runtime environment using GIJ/classpath
gcj-4.7-jre-headless - Java runtime environment using GIJ/classpath (headless version)
gcj-4.7-jre-lib - Java runtime library for use with gcj (jar files)
gcj-jre - Java runtime environment using GIJ/classpath
gcj-jre-headless - Java runtime environment using GIJ/classpath (headless version)
icedtea-6-jre-cacao - Alternative JVM for OpenJDK, using Cacao
icedtea-6-jre-jamvm - Alternative JVM for OpenJDK, using JamVM
icedtea-7-jre-jamvm - Alternative JVM for OpenJDK, using JamVM
jvm-7-avian-jre - lightweight virtual machine using the OpenJDK class library
libjrexx-java - automaton based regular expression API for java
libreoffice - office productivity suite
openjdk-6-jre - OpenJDK Java runtime, using Hotspot Zero
openjdk-6-jre-headless - OpenJDK Java runtime, using Hotspot Zero (headless)
openjdk-6-jre-lib - OpenJDK Java runtime (architecture independent libraries)
openjdk-7-jre - OpenJDK Java runtime, using Hotspot Zero
openjdk-7-jre-headless - OpenJDK Java runtime, using Hotspot Zero (headless)
openjdk-7-jre-lib - OpenJDK Java runtime (architecture independent libraries)
No oracle-java-jre.
After reboot:
java -version gives:
java version "1.6.0_31"
OpenJDK Runtime Environment (IcedTea6 1.13.3) (6b31-1.13.3-1~deb7u1+rpi1)
OpenJDK Zero VM (build 23.25-b01, mixed mode)
So, what to do?
Solution!!
I removed later versions and installed openjdk-6-jdk.
sudo apt-get remove --purge openjdk-7-jdk

pi@Cisero ~ $ javac -version
javac 1.8.0
pi@Cisero ~ $ java -version
java version "1.6.0_31"
OpenJDK Runtime Environment (IcedTea6 1.13.3) (6b31-1.13.3-1~deb7u1+rpi1)
OpenJDK Zero VM (build 23.25-b01, mixed mode)
pi@Cisero ~ $

sudo apt-get remove --purge openjdk-8-jdk

sudo apt-get remove --purge oracle-java8-jdk

i@Cisero ~ $ java -version
java version "1.6.0_31"
OpenJDK Runtime Environment (IcedTea6 1.13.3) (6b31-1.13.3-1~deb7u1+rpi1)
OpenJDK Zero VM (build 23.25-b01, mixed mode)
pi@Cisero ~ $ javac -version
javac 1.7.0_40
pi@Cisero ~ $

sudo apt-get remove --purge oracle-java7-jdk

pi@Cisero ~ $ java -version
java version "1.6.0_31"
OpenJDK Runtime Environment (IcedTea6 1.13.3) (6b31-1.13.3-1~deb7u1+rpi1)
OpenJDK Zero VM (build 23.25-b01, mixed mode)
pi@Cisero ~ $ javac -version
-bash: /usr/bin/javac: Filen eller katalogen finns inte
pi@Cisero ~ $

sudo apt-get install openjdk-6-jdk

pi@Cisero ~ $ java -version
java version "1.6.0_31"
OpenJDK Runtime Environment (IcedTea6 1.13.3) (6b31-1.13.3-1~deb7u1+rpi1)
OpenJDK Zero VM (build 23.25-b01, mixed mode)
pi@Cisero ~ $ javac -version
javac 1.6.0_31
pi@Cisero ~ $

elatllat
Posts: 1337
Joined: Sat Dec 17, 2011 5:05 pm

Re: Solved.Java.Unsupported major.minor version 51.0

Sat Jun 07, 2014 5:22 am

The jdk has a jre in it.
SBC with 32GB RAM: https://hardkernel.com

FAQ : https://raspberrypi.stackexchange.com

Unanswered: https://www.raspberrypi.org/forums/search.php?search_id=unanswered

Return to “Java”