Java set up in Linux and Hello World
I have seen many questions about getting Hello World to work.
I have a similar problem: I used thsi code in my text editor in Ubuntu -
"public class MainTest {
public static void main(String[] args) {
System.out.println("Hello World");
}
} "
-I saved it as MainTest.java in my /home/me/ directory
-then complied it with "javac MainTest.java" to create the .class file
-then attempted to run it with "java MainTest" and also "java MainTest.java" both of which failed with error messages as follows;
Exception in thread "main" java.lang.UnsupportedClassVersionError: MainTest : 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(Secure ClassLoader.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.j ava:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 21)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 66)
Could not find the main class: MainTest. Program will exit.
-At this point there seem to be 3 files in the directory: Maintest.java , Maintest.class and Maintest.java~.
- I have tried changing the CLASSPATH to the working directory, using the CLASSPATH and -cp options in the "java" command line instruction, entering and omitting the .java file type but nothing works.
Advice required please!
Re: Java set up in Linux and Hello World
Nothing to do with CLASSPATH, indeed you do not (nor should ever use) that.
It has everything to do with you having two versions of Java on your system.
A 1.7 JDK and a 1.6 JRE.
Re: Java set up in Linux and Hello World
Hi Tolls, You are right.
I googled the "Exception in Thread" error message and realsied it was because there were multiple versions of Java. I managed to find a piece of code in Ubuntu documentation that, once installed, allowed me to choose which version of Java runs. I selected the latest version and the code ran.
Thanks for your help and all the forum users whose comments pointed me in this directiopn!