LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   java compile/execute error (https://www.linuxquestions.org/questions/linux-newbie-8/java-compile-execute-error-4175435130/)

PoleStar 11-01-2012 02:53 PM

java compile/execute error
 
Hello,

I am having trouble with my first :) java program.


Code:

[me@horse javahello]$ java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.5) (fedora-68.1.11.5.fc16-i386)
OpenJDK Server VM (build 20.0-b12, mixed mode)
[me@horse javahello]$



code is:
Code:

class myfirstjavaprog

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


Compile and run:
Code:

[me@horse javahello]$ javac myfirstjavaprog.java
[me@horse javahello]$
[me@horse javahello]$
[me@horse javahello]$
[me@horse javahello]$
[me@horse javahello]$ java myfirstjavaprog
Exception in thread "main" java.lang.UnsupportedClassVersionError: myfirstjavaprog : 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)
Could not find the main class: myfirstjavaprog. Program will exit.
[me@horse javahello]$


sag47 11-01-2012 11:12 PM

Not sure. I just copied your source code and pasted it into my environment. It compiled and ran with no problems.

Code:

[sam@stealth java_compile]$ javac myfirstjavaprog.java
[sam@stealth java_compile]$ java myfirstjavaprog
Hello World!
[sam@stealth java_compile]$ java -version
java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01, mixed mode)

Code:

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

That rules out the program itself. Do you have any special $JAVA_OPTS specified for the environment?
Code:

echo $JAVA_OPTS
#two different versions?
which java
which javac


PoleStar 11-02-2012 08:48 AM

Searching on the net, it say some thing about using rule from one version and compiling or executing for an other version.

I think I carelessly tried to install jdk from sun(oracle) website, when I could not find command javac.

Code:

[me@horse ~]$ echo $JAVA_OPTS

[me@horse ~]$ env | grep -i java


[me@horse ~]$ which java
/usr/bin/java

[me@horse ~]$ which javac
/usr/bin/javac

[me@horse ~]$ rpm -aq | egrep -i "jdk|jre|java"
java-1.6.0-openjdk-1.6.0.0-68.1.11.5.fc16.i686
jdk-1.7.0_09-fcs.i586
tzdata-java-2012d-1.fc16.noarch
[me@horse ~]$


Thanks for your help.

sag47 11-02-2012 10:50 AM

Remove both of them and then install one of them.

Code:

$ java -version
$ javac -version



All times are GMT -5. The time now is 05:45 PM.