32

Possible Duplicate:
Exception in thread “main” java.lang.UnsupportedClassVersionError: a (Unsupporte d major.minor version 51.0)

i have developed an web application in java using jdk 1.7.0 ,Eclipse IDE (Indigo) and is running that application on the tomcat(apache-tomcat-7.0.23) configured in eclipse ide.

When i tried to run my application through IDE it runs fine but when i created its war and placed it in apache deployment folder(webapps) and run it from outside the IDE the start page gets successfully loaded but when i tried to do any operation over it it gives me an error

UnsupportedClassVersionError unsupported major.minor version 51.0 unable to load class bean.myclassname

i have checked the java version outside ide its jdk 1.7.0 and also the JAVA_HOME environment variable is set to C:\Program Files\Java\jdk1.7.0_01 .

Community
  • 1
  • 1
Nishit Jain
  • 1,549
  • 8
  • 21
  • 33
  • 10
    I would argue as a Java beginner that these questions don't appear to be duplicates. There are minor differences in the problem and solution that make a world of difference in clarity for Java newbies. Glad this one was here, the other one wouldn't have helped me fix my problem, but this one did. – Slobaum Jun 13 '13 at 19:16

4 Answers4

34

Well presumably it's not using the same version of Java when running it externally. Look through the startup scripts carefully to find where it picks up the version of Java to run. You should also check the startup logs to see whether they indicate which version is running.

Alternatively, unless you need the Java 7 features, you could always change your compiler preferences in Eclipse to target 1.6 instead.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • yes initially it was 1.6 but then i have changed it to 1.7 and that require rebuild the application so i rebuild it again but same problem exists – Nishit Jain Jan 24 '12 at 07:05
  • @NishitJain: If you were building with a target of 1.6 before, then it must be some other jar file which is causing the problem. Does the log contain any more information about what it was trying to load? – Jon Skeet Jan 24 '12 at 07:11
  • INFO: Deploying web application archive C:\Program Files\Apache Software Foundat ion\apache-tomcat-7.0.23\webapps\DemoWebApp.war Jan 24, 2012 12:40:54 PM org.apache.catalina.util.SessionIdGenerator createSecur eRandom it is trying to load only sing war file which is my web application war file which i have replaced with that of war build with version 1.7 – Nishit Jain Jan 24 '12 at 07:22
  • @NishitJain: Please could you post the full log section in your question - including the log entry with the error message itself. Have you looked through the logs to see what it says about which version of Java it's using? – Jon Skeet Jan 24 '12 at 07:27
  • yes john u were right it was loading the jdk 1.6.29 version first than jdk 1.7.0 version which was also specified in the path variable i just removed that jdk 1.6.29 from variable which also need a computer restart beacause even after deleting the jdk 1.6.29 path tomcat was trying to first load the same so after restarting the system and again initializing the tomcat it loads only jdk 1.7.0 version from the path variable and then i run my application it runs....... Thanks Jon – Nishit Jain Jan 24 '12 at 07:34
  • I also found that for JDK 1.6 I had to use Microsfot JDBC Driver 4.0. The 4.1 and 4.2 drivers threw this error, despite sections of the MS site saying those drivers are compatible with 1.6. – rwblackburn Jan 12 '16 at 17:34
16

Even though your JDK in eclipse is 1.7, you need to make sure eclipse compilance level also set to 1.7. You can check compilance level--> Window-->Preferences--> Java--Compiler--compilance level.

Unsupported major minor error happens in cases where compilance level doesn't match with runtime.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • yes initially it was 1.6 but then i have changed it to 1.7 and that require rebuild the application so i rebuild it again but same problem exists – Nishit Jain Jan 24 '12 at 07:11
  • I solved the problem, Tomcat was pointed to jre6, I just installed jre7 and pointer JAVA_HOME & JRE_HOME to it instead of JE6,I ran the Apache again & deployed my web app, it worked fine. – Bassel Kh Feb 23 '12 at 08:22
  • 1
    Your answer is much more clear and descriptive for a Java beginner than the chosen answer. Thank you. – Slobaum Jun 13 '13 at 19:15
2

java_home environment variable should point to the location of the proper version of java installation directory, so that tomcat starts with the right version. for example it you built the project with java 1.7 , then make sure that JAVA_HOME environment variable points to the jdk 1.7 installation directory in your machine.

I had same problem , when i deploy the war in tomcat and run, the link throws the error. But pointing the variable - JAVA_HOME to jdk 1.7 resolved the issue, as my war file was built in java 1.7 environment.

0

Try adding the following to your eclipse.ini file:

-vm
C:\Program Files\Java\jdk1.7.0_01\bin\java.exe

You might also have to change the Dosgi.requiredJavaVersion to 1.7 in the same file.

Justin Buser
  • 2,813
  • 25
  • 32