0

Hello I am a newbie to java and i am going along alone from a book, wherein there is a code called pharseomatic, which basically selects a random words from a list of word list and prints to the output.

The is as below :

public class phraseomatic{
    public static void main( String[] agrs){

            String[] wordlistone = { "Hello ", "whats up ", "How ", " U doing " };
            String[] wordlisttwo = { "I am ", " Am Interseted ", "Learning ", "Java" };
            String[] wordlisttre = { "C sharp ", "C plus plus ", "C and Unix ", "Linux "};

            int wlen1 = wordlistone.length;
            int wlen2 = wordlisttwo.length;
            int wlen3 = wordlisttre.length;

            int rand1 = (int) ( Math.random() * wlen1 );
            int rand2 = (int) ( Math.random() * wlen2 );
            int rand3 = (int) ( Math.random() * wlen3 );

            String phrase = " " + wordlistone[rand1] + wordlisttwo[rand2] + wordlisttre[rand3] ;
            System.out.println("The Result is --->" + phrase );
    }
}

I was able to compile the code using javac phraseomatic.java command, and everything was good but when i run the code by using command java phraseomatic, I got the following output or errors, please let me know whether I am doing it right or my code is worng or both...? Thanks in advance. The errors is as below :

 root@ubuntu:~/java# java phraseomatic 


     Exception in thread "main" java.lang.UnsupportedClassVersionError: phraseomatic :       Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
        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:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Teo
  • 3,143
  • 2
  • 29
  • 59
Trilok M
  • 681
  • 7
  • 21

2 Answers2

1

The java version during compiling the code is different during running.Mean compiled with higher version than running version of JAVA.Make sure the version of running jvm is lower than compiled version.

Kick
  • 4,823
  • 3
  • 22
  • 29
1

java.lang.UnsupportedClassVersionError is occur in below case Thrown when the Java Virtual Machine attempts to read a class file and determines that the major and minor version numbers in the file are not supported.

So you need to change your compiler to running version of java... It's better go to Update one like java7

Janny
  • 681
  • 1
  • 8
  • 33
  • i checked with java -version and i got the version as java version "1.6.0_24" OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-0ubuntu1~12.04.1) OpenJDK Server VM (build 20.0-b12, mixed mode) so i guess runtime version is higher than compiler. so what do you suggest..? – Trilok M Feb 05 '14 at 07:23
  • Update the JDK to 1.7.. – Janny Feb 05 '14 at 07:24
  • Is you run time is 1.7 or 1.8? As a simple Line. You can download update version of java and install. – Janny Feb 05 '14 at 07:27
  • @Jani why to update java. wht the issue with current version – Kick Feb 05 '14 at 07:32
  • @NiksTyagi, I know there is other way to correct this issue but this is the best way to solve issue without more information. It's easy as per my understand. – Janny Feb 05 '14 at 07:37