Tomcat指定JDK版本运行

业务需求:一台服务器centos默认jdk版本是1.6,跑了两个服务tomcat服务:
业务一:占用8080端口,需要jdk6运行,是事先部署好的;
业务二:占用8180端口,需要jdk7运行,是将要部署的。
现在需要部署业务二,就会出现报错,怎么办?

报错

业务二的项目部署好之后,启动项目,出现如下报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
java.lang.UnsupportedClassVersionError: org/springframework/web/filter/DelegatingFilterProxy : Unsupported major.minor version 51.0 (unable to load class org.springframework.web.filter.DelegatingFilterProxy)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2858)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1159)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1647)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at org.apache.catalina.startup.WebAnnotationSet.loadClassAnnotation(WebAnnotationSet.java:145)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationFilterAnnotations(WebAnnotationSet.java:84)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:57)
at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:294)
at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:1047)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:265)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4616)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:583)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:822)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
2016-8-29 9:55:13 org.apache.coyote.http11.Http11AprProtocol start

问题原因

jdk的版本问题,如上描述,业务二需要最低jdk版本为7

解决方法

  1. 下载centos使用的 jdk7,我下载出来的文件名为:
    jdk-7u45-linux-x64.tar.gz
  2. 解压到某个目录下面并把源包删除

    1
    2
    $ tar xvf jdk-7u45-linux-x64.tar.gz
    $ rm jdk-7u45-linux-x64.tar.gz

    此时,进入到jdk7目录下的bin目录

    1
    2
    $ java -version #返回的版本号是1.6,因为这是全局的jdk
    $ ./java -version #返回的版本号是1.7,因为这是当前目录下的jdk
  3. 配置8180端口的tomcat
    在catalina.sh文件和setclasspath.sh文件的头部都加上指定jdk的地址(jdk路径根据自己的配置来变更):

    1
    2
    export JAVA_HOME=/usr/local/app/jdk7
    export JRE_HOME=/usr/local/app/jdk7/jre
  4. 如果出现端口冲突问题,请配置好自己tomcat下的server.xml文件,以下是一个示例:

    1
    2
    3
    4
    5
    6
    7
    <Server port="8005" shutdown="SHUTDOWN"> ----> <Server port="8195" shutdown="SHUTDOWN">
    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> ----> <Connector port="8180" protocol="HTTP/1.1" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> ----> <Connector port="8199" protocol="AJP/1.3" redirectPort="8443" />
    <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> ----> <Host name="10.10.3.54" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false" ><Context path="" docBase="ROOT" reloadable="true"></Context></Host>
  5. 启动tomcat,成功

扩展:Eclipse中tomcat指定jdk来启动

首先打开 window–》Preferences

  1. 查看eclipse中现有的jdk版本,如下图,如果没有自己想要的版本,请在安装好之后,点击add添加进来:
    查看eclipse中现有的jdk版本
  2. 搜索tomcat,出现如下图,更改JDK的版本即可,记得保存
    为tomcat选择固定的jdk版本