• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java code compiles but doesn't run  RSS feed

 
Fausto Arinos Barbuto
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day everyone,

I'm a *very* green Java programmer. In fact, I'm not yet past that "Hello World" program.
And this is my 1st message to this forum.

I'm following the steps in "Head First: Java" book, and reached that point where one is ready
to write his/her 1st Java code. Here's mine:

public class MyFirstApp
{

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

}

As simple as it should be, and compiles fine producing the correspondent class. But it doesn't
run, however, and spits the following execution-time errors:

Exception in thread "main" java.lang.UnsupportedClassVersionError: MyFirstApp : 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: MyFirstApp. Program will exit.

That's what I obtain when the following commands are issued:

$ javac -version
javac 1.7.0_51

$ java -version
java version "1.6.0_30"
OpenJDK Runtime Environment (IcedTea6 1.13.1) (6b30-1.13.1-1ubuntu2~0.12.04.1)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)
fausto@fausto-quad:~/Programs/Java$

I'm running Ubuntu 12.04 LTS 64-bit.

Any hints? Many thanks in advance.

Fausto

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

That error message is typical if you run Java code with an older JDK than you're using to compile it. The two different versions of java and javac bear that out. I advise to uninstall Java 6, and only use Java 7 for all your development.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fausto,
Welcome to CodeRanch!

$ javac -version
javac 1.7.0_51

$ java -version
java version "1.6.0_30"


The problem is that you are compiling with a later version of Java than you are running the program with. When you compile, everything is good and you produce a Java 7 program. Then you try to run it and Java 6 says "what's this Java 7 thing? I don't know what to do so I'll give an error message". The error message has a hint (once you are experienced - it tells you the version of Java used to compile using the class file numbering system.)

To fix this, you'll want to change your "path" variable to use Java 7 as well. Do you know how to do this?
 
Fausto Arinos Barbuto
Greenhorn
Posts: 2
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ulf, Jeanne;

Thanks for your replies and welcome wishes.

Your comments gave me the necessary courage and strength to remove everything that seemed to be old IcedTea
and OpenJDK stuff. Indeed, my installation was a mess, didn't know how things got to that confusing state.

Now, when I check the versions I get:


$ javac -version
javac 1.7.0_51

$ java -version
java version "1.7.0_51"
OpenJDK Runtime Environment (IcedTea 2.4.4) (7u51-2.4.4-0ubuntu0.12.04.2)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)


And now everything works as intended. I'm back to the Java business again and can proceed with the lessons.
Case closed.

Thanks a lot!

Fausto

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great to hear you are now up and running!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!