2
SEVERE: Exception starting filter struts2
java.lang.UnsupportedClassVersionError: 
Unsupported major.minor version 51.0 (unable to load class com.directv.actions.indexAction)
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2922)
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1174)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1669)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
    at com.opensymphony.xwork2.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:139)
    at com.opensymphony.xwork2.ObjectFactory.getClassInstance(ObjectFactory.java:100)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:366)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:329)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:429)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:239)
    at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:152)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452)
    at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:201)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4775)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5452)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:983)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1660)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

I am getting the above error when I try to start my application using Tomcat7 on a Linux server, but on Windows 7 using Tomcat7, JDK 7, it works fine.

java version "1.6.0_22"
OpenJDK Runtime Environment (IcedTea6 1.10.8) (fedora-65.1.10.8.fc15-i386)
OpenJDK Client VM (build 20.0-b11, mixed mode)

I can easily replicate this error on my Local machine simply by changing the Installed JRE, so I tried to compile the classes using 1.6 but am still getting the same error. How can I resolve this issue?

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • version 51.0 means that the compiler is jdk1.7! so you need to configure the same version for the web server. If you whant to run it in fedora, you need to install jdk 7 and put this line in .bashrc 'export JAVA_HOME=path to jdk1.7' – Riadh Oct 22 '14 at 21:21
  • Thanks Riadh for responding , however I can not change the Java version on web server , so I tried changing the compliance level 1.6 in eclipse and then compiled again and tried to deploy but to no avail. I even tried totally changing the jdk to version 1.6 as well on my Local machine – Julie D'Mello Oct 22 '14 at 22:07

6 Answers6

1

While you can run any .class file compiled with a Java version lower than or equal to the version you are using, the opposite is impossible.

For example, you can compile with JDK 6 and run into both JRE 6 and JRE 7, but you can NOT compile with JDK 7 and run into JRE 6.

This error:

Unsupported major.minor version 51.0

means that you are trying to run a Java .class file with a Java version lower than the Java version used to compile that file.

That number (51.0) is the

major version number of the class file format being used.

      J2SE 8 = 52
      J2SE 7 = 51 <---------
    J2SE 6.0 = 50 
    J2SE 5.0 = 49 
     JDK 1.4 = 48 
     JDK 1.3 = 47 
     JDK 1.2 = 46 
     JDK 1.1 = 45 

and in your specific case it means you are compiling with JDK 7, and trying to run it with a lower Java version.

What to do then ?

If you are using Java 7 code (constructs that aren't available in Java 6), you have no choice but to run your code into JRE 7 (or to rewrite your code in Java 6...).

If instead your code is Java 6 compliant, you can compile it with retrocompatibility to Java 6, by one of the following ways:

  1. Use JDK 6 to compile (you can have it installed along with JDK 7);

  2. Use JDK 7 to compile, but specify the target version is Java 6:

    • With JAVAC:

      javac -target 1.6
      
    • With ANT:

      <javac srcdir="${src}"
            destdir="${build}"
           compiler="javac1.7"                 
             source="1.6"
             target="1.6"
               fork="true"
              debug="true" />
      
  3. Use Eclipse to compile (that will use its internal compiler, not the installed JDK), and specify in the compiler settings that you are targeting Java 1.6:

    Eclipse compiler settings

  4. Use Maven and one of the following hacks.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thanks Much Andrea , I am trying to make changes as suggested by you. I will update here if the issue resolves. – Julie D'Mello Oct 23 '14 at 13:52
  • @JulieD'Mello, please consider up-voting (arrow up) and accepting (mark the white V symbol in top left corner of) the answer if it helped – Andrea Ligios Oct 23 '14 at 21:27
1

Thank you very much for you help in resolving this issue.

Here is what I did to resolve this issue : My webserver was using java version 1.6.0_22-b22 , on my Local machine however I had JDK 1.7 , even though I tried changing compliance level ,I was getting the same error. So I changed my ant build XML file :

 <javac srcdir="${src.dir}"
          destdir="${build.dir}/WEB-INF/classes"
          compiler="javac1.7"                 
          source="1.6"
          target="1.6"
          fork="true"
          debug="${compile.debug}"  
          debuglevel="${compile.debuglevel}"
          deprecation="${compile.deprecation}"
          optimize="${compile.optimize}"
          includeantruntime="false"
            >       
           <classpath refid="compile.classpath"/>
     </javac>

Not only this :I changed the installed JRE to 1.6 as well .

The issue is now resolved .

0

UnsupportedClassVersionError means that you are running the webserver and compiling using different java versions, make them match and your problem will be gone :)

Schroed
  • 194
  • 10
  • Thanks Jose , but there are other application sitting on the same Web server so I can not change the Java version there , I changed the Jdk version on my machine but the same error . Application gets deployed but doesn't start. – Julie D'Mello Oct 22 '14 at 22:08
  • What if you try making a soft link from your java 1.7 pointing to your 1.6 path? maybe you have set 1.7 somewhere you can't see – Schroed Oct 22 '14 at 22:36
0

To change the Java compiler in eclipse, you should change both:

  • The JRE library in buildpath (remove 1.7, and then add 1.6)
  • the Java Compiler (1.6 instead of 1.7)
Riadh
  • 1,088
  • 2
  • 12
  • 25
0

You compiled the code using JDK 7 but you are running it with JDK 6 or less.

panagdu
  • 2,133
  • 1
  • 21
  • 36
0

This often happens if a library you depend on is compiled for a newer version of java. Replace such libraries with versions that are compiled for your version of java. Usually you can find an older version of the library in question.

morten
  • 392
  • 1
  • 11
  • Sorry, it was meant as an answer, I should probably rephrase it though. But I see now that the question has been solved already, though none of the answers have been accepted. – morten Oct 20 '16 at 08:16
  • I edited it now. I think it might add some value for someone who had the same problem that I often have. – morten Oct 20 '16 at 08:21
  • right, looks good. – Magisch Oct 20 '16 at 08:22