在开发Hibernate的Web项目应用时使用tomcat时运行很好,但实际生产环境中要部署到WebLogic就会出现一些ClassNotFoundException异常。
org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken异常
这是使用Hibernate 3.0以后版本均会出现的异常;
exception is java.lang.NoSuchMethodError: javax.persistence.UniqueConstraint.name()Ljava/lang/String,如果使用Hibernate 3.5,在实体中使用了唯一约束会出现这个异常,因为在Hibernate 3.5使用了最新的Jpa 2.0,WebLogic中还是Jpa 1.0, UniqueConstraint.name在Jpa 2.0才有。
exception is java.lang.NoSuchMethodError: org.apache.commons.lang.ArrayUtils.isNotEmpty,我在项目使用了比较新版本的commons-lang-2.5.jar在webLogic 10.3会报这个异常;
在网search一下,解决ClassNotFoundException异常最常用的办法就是将出现异常类所在的jar包入到weblogic环境中的classpath最前面加载,将相应的antlr-2.7.6.jar、hibernate-jpa-2.0-api-1.0.0.Final.jar、commons-lang-2.5.jar copy到${WL_HOME/}server/lib/或其他目录,可以修改${DOMAIN_HOME}/bin/startWebLogic.sh,在CLASSPATH之前加上下面一句:
- PRE_CLASSPATH=”${WL_HOME/}server/lib/antlr-2.7.6.jar:${WL_HOME/}server/lib/hibernate-jpa-2.0-api-1.0.0.Final.jar:${WL_HOME/}server/lib/commons-lang-2.5.jar”
PRE_CLASSPATH=”${WL_HOME/}server/lib/antlr-2.7.6.jar:${WL_HOME/}server/lib/hibernate-jpa-2.0-api-1.0.0.Final.jar:${WL_HOME/}server/lib/commons-lang-2.5.jar”
并将修改为CLASSPATH="${SAVE_CLASSPATH}"
- CLASSPATH="${PRE_CLASSPATH}:${SAVE_CLASSPATH}"
CLASSPATH="${PRE_CLASSPATH}:${SAVE_CLASSPATH}"
或者修改${DOMAIN_HOME}/bin/setDomainEnv.sh,在setDomainEnv.sh前面加入一句:
- EXT_PRE_CLASSPATH=”${WL_HOME/}server/lib/antlr-2.7.6.jar:${WL_HOME/}server/lib/hibernate-jpa-2.0-api-1.0.0.Final.jar:${WL_HOME/}server/lib/commons-lang-2.5.jar”
- export EXT_PRE_CLASSPATH
EXT_PRE_CLASSPATH=”${WL_HOME/}server/lib/antlr-2.7.6.jar:${WL_HOME/}server/lib/hibernate-jpa-2.0-api-1.0.0.Final.jar:${WL_HOME/}server/lib/commons-lang-2.5.jar”
export EXT_PRE_CLASSPATH
在我项目实践中发现加入antlr-2.7.6.jar、hibernate-jpa-2.0-api-1.0.0.Final.jar、commons-lang-2.5.jar后,前两个起作用,异常消除,commons-lang-2.5.jar没有作用,exception is java.lang.NoSuchMethodError: org.apache.commons.lang.ArrayUtils.isNotEmpty异常依旧,当时没有解决方法,只好在项目中去掉ArrayUtils.isNotEmpty,杯具!!!
后来在网上找到一种更有效解决ClassNotFoundException的,可以解决所有WebLogic中的ClassNotFoundException。改解决办法是将web项目包含进ear包中。将web项目打包成war文件,在war文件相同目录下建立META-INF目录,目录建立application.xml、weblogic-application.xml两个文件,文件内容很简单,开发过weblogic EJB应用的应该很清楚。
application.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN' 'http://java.sun.com/dtd/application_1_3.dtd'>
- <application>
- <display-name>cckzzx</display-name>
- <description>cckzzx</description>
- <module>
- <web>
- <web-uri>cckzzx.war</web-uri>
- <context-root>cckzzx</context-root>
- </web>
- </module>
- </application>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN' 'http://java.sun.com/dtd/application_1_3.dtd'>
<application>
<display-name>cckzzx</display-name>
<description>cckzzx</description>
<module>
<web>
<web-uri>cckzzx.war</web-uri>
<context-root>cckzzx</context-root>
</web>
</module>
</application>
weblogic-application.xml
- <?xml version="1.0"?>
- <weblogic-application xmlns="http://www.bea.com/ns/weblogic/90"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <application-param>
- <param-name>webapp.encoding.default</param-name>
- <param-value>UTF-8</param-value>
- </application-param>
- <prefer-application-packages>
- <package-name>org.apache.commons.*</package-name>
- <package-name>antlr.*</package-name>
- <package-name>javax.persistence.*</package-name>
- </prefer-application-packages>
- </weblogic-application>
<?xml version="1.0"?>
<weblogic-application xmlns="http://www.bea.com/ns/weblogic/90"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<application-param>
<param-name>webapp.encoding.default</param-name>
<param-value>UTF-8</param-value>
</application-param>
<prefer-application-packages>
<package-name>org.apache.commons.*</package-name>
<package-name>antlr.*</package-name>
<package-name>javax.persistence.*</package-name>
</prefer-application-packages>
</weblogic-application>
<prefer-application-packages>
表示在weblogic中优先使用当前应用中的jar包
最后将war文件和META-INF目录打包成ear包、部署。在这里我参考网上资料写了一个ant脚本build.xml对项目进行ear打包
build.xml
- <?xml version="1.0"?>
- <project name="cckzzx" default="ear" basedir=".">
- <property name="project.name" value="cckzzx" />
- <property name="app.dir" value="${basedir}" />
- <property name="src.java.dir" value="${app.dir}/src/main/java" />
- <property name="src.resources.dir" value="${app.dir}/src/main/resources" />
- <property name="weblogic.home" value="/opt/Oracle/Middleware/wlserver_10.3" />
- <property name="wls.username" value="weblogic" />
- <property name="wls.password" value="weblogic123" />
- <property name="wls.hostname" value="localhost" />
- <property name="wls.port" value="7001" />
- <property name="wls.server.name" value="AdminServer" />
- <property name="build.dir" value="${app.dir}/build" />
- <property name="build.classes.dir" value="${build.dir}/classes" />
- <property name="build.tmp.dir" value="${build.dir}/tmp" />
- <path id="build.classpath" description="编译时的classpath路径">
- <fileset dir="${basedir}/WebRoot/WEB-INF/lib">
-
- <include name="*.jar" />
- </fileset>
- <fileset dir="${weblogic.home}/server/lib">
- <include name="weblogic.jar" />
- </fileset>
- <pathelement location="${build.classes.dir}" />
- </path>
- <taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy" classpathref="build.classpath" />
- <target name="clean" description="删除编译目录">
- <delete dir="${build.dir}" />
- </target>
- <target name="prepare" depends="clean" description="创建编译目录">
- <mkdir dir="${build.dir}" />
- <mkdir dir="${build.classes.dir}" />
- <mkdir dir="${build.tmp.dir}" />
- </target>
- <target name="compile" depends="prepare" description="编译">
- <javac srcdir="${src.java.dir}" destdir="${build.classes.dir}" compiler="org.eclipse.jdt.core.JDTCompilerAdapter" encoding="UTF-8" source="1.6" target="1.6" debug="on" deprecation="on" optimize="off" includes="**">
- <classpath refid="build.classpath" />
- </javac>
- </target>
- <target name="ear" depends="compile" description="打包EAR包">
- <jar jarfile="${build.dir}/${project.name}.jar">
- <fileset dir="${build.classes.dir}">
- <include name="com/**/*.class" />
- <include name="**/*.xml" />
- </fileset>
- </jar>
- <mkdir dir="${build.tmp.dir}/WebRoot" />
- <copy todir="${build.tmp.dir}/WebRoot">
- <fileset dir="${app.dir}/WebRoot">
- <exclude name="WEB-INF/classes/**" />
- </fileset>
- </copy>
- <copy todir="${build.tmp.dir}/WebRoot/WEB-INF/lib">
- <fileset file="${build.dir}/${project.name}.jar" />
- </copy>
- <mkdir dir="${build.tmp.dir}/WebRoot/WEB-INF/classes" />
- <copy todir="${build.tmp.dir}/WebRoot/WEB-INF/classes">
- <fileset dir="${src.resources.dir}" />
- </copy>
- <war warfile="${build.dir}/${project.name}.war" webxml="${build.tmp.dir}/WebRoot/WEB-INF/web.xml">
- <fileset dir="${build.tmp.dir}/WebRoot">
- <exclude name="WEB-INF/web.xml" />
- </fileset>
- </war>
- <ear earfile="${build.dir}/${project.name}.ear" appxml="${app.dir}/src/weblogic/META-INF/application.xml">
- <fileset dir="${app.dir}/src/weblogic">
- <include name="META-INF/weblogic-application.xml" />
- </fileset>
- <fileset dir="${build.dir}">
- <include name="${project.name}.war" />
- </fileset>
- </ear>
-
- <delete file="${build.dir}/${project.name}.jar" />
- <delete file="${build.dir}/${project.name}.war" />
- <delete dir="${build.classes.dir}" />
- <delete dir="${build.tmp.dir}" />
- </target>
-
- <target name="deploy" depends="ear,undeploy" description="布署EAR服务">
- <wldeploy action="deploy" name="${project.name}" source="${build.dir}/${project.name}.ear" targets="${wls.server.name}" user="${wls.username}" password="${wls.password}" adminurl="t3://${wls.hostname}:${wls.port}" debug="true" verbose="true" failonerror="true" />
- <echo message="come here......" /> <echo message="deploy success......." />
- </target>
- <target name="undeploy" description="删除EAR服务">
- <wldeploy action="undeploy" name="${project.name}" targets="${wls.server.name}" user="${wls.username}" password="${wls.password}" adminurl="t3://${wls.hostname}:${wls.port}" debug="false" verbose="false" failonerror="false" />
- </target>
- </project>
<?xml version="1.0"?>
<project name="cckzzx" default="ear" basedir=".">
<property name="project.name" value="cckzzx" />
<property name="app.dir" value="${basedir}" />
<property name="src.java.dir" value="${app.dir}/src/main/java" />
<property name="src.resources.dir" value="${app.dir}/src/main/resources" />
<property name="weblogic.home" value="/opt/Oracle/Middleware/wlserver_10.3" />
<property name="wls.username" value="weblogic" />
<property name="wls.password" value="weblogic123" />
<property name="wls.hostname" value="localhost" />
<property name="wls.port" value="7001" />
<property name="wls.server.name" value="AdminServer" />
<property name="build.dir" value="${app.dir}/build" />
<property name="build.classes.dir" value="${build.dir}/classes" />
<property name="build.tmp.dir" value="${build.dir}/tmp" />
<path id="build.classpath" description="编译时的classpath路径">
<fileset dir="${basedir}/WebRoot/WEB-INF/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${weblogic.home}/server/lib">
<include name="weblogic.jar" />
</fileset>
<pathelement location="${build.classes.dir}" />
</path>
<taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy" classpathref="build.classpath" />
<target name="clean" description="删除编译目录">
<delete dir="${build.dir}" />
</target>
<target name="prepare" depends="clean" description="创建编译目录">
<mkdir dir="${build.dir}" />
<mkdir dir="${build.classes.dir}" />
<mkdir dir="${build.tmp.dir}" />
</target>
<target name="compile" depends="prepare" description="编译">
<javac srcdir="${src.java.dir}" destdir="${build.classes.dir}" compiler="org.eclipse.jdt.core.JDTCompilerAdapter" encoding="UTF-8" source="1.6" target="1.6" debug="on" deprecation="on" optimize="off" includes="**">
<classpath refid="build.classpath" />
</javac>
</target>
<target name="ear" depends="compile" description="打包EAR包">
<jar jarfile="${build.dir}/${project.name}.jar">
<fileset dir="${build.classes.dir}">
<include name="com/**/*.class" />
<include name="**/*.xml" />
</fileset>
</jar>
<mkdir dir="${build.tmp.dir}/WebRoot" />
<copy todir="${build.tmp.dir}/WebRoot">
<fileset dir="${app.dir}/WebRoot">
<exclude name="WEB-INF/classes/**" />
</fileset>
</copy>
<copy todir="${build.tmp.dir}/WebRoot/WEB-INF/lib">
<fileset file="${build.dir}/${project.name}.jar" />
</copy>
<mkdir dir="${build.tmp.dir}/WebRoot/WEB-INF/classes" />
<copy todir="${build.tmp.dir}/WebRoot/WEB-INF/classes">
<fileset dir="${src.resources.dir}" />
</copy>
<war warfile="${build.dir}/${project.name}.war" webxml="${build.tmp.dir}/WebRoot/WEB-INF/web.xml">
<fileset dir="${build.tmp.dir}/WebRoot">
<exclude name="WEB-INF/web.xml" />
</fileset>
</war>
<ear earfile="${build.dir}/${project.name}.ear" appxml="${app.dir}/src/weblogic/META-INF/application.xml">
<fileset dir="${app.dir}/src/weblogic">
<include name="META-INF/weblogic-application.xml" />
</fileset>
<fileset dir="${build.dir}">
<include name="${project.name}.war" />
</fileset>
</ear>
<delete file="${build.dir}/${project.name}.jar" />
<delete file="${build.dir}/${project.name}.war" />
<delete dir="${build.classes.dir}" />
<delete dir="${build.tmp.dir}" />
</target>
<target name="deploy" depends="ear,undeploy" description="布署EAR服务">
<wldeploy action="deploy" name="${project.name}" source="${build.dir}/${project.name}.ear" targets="${wls.server.name}" user="${wls.username}" password="${wls.password}" adminurl="t3://${wls.hostname}:${wls.port}" debug="true" verbose="true" failonerror="true" />
<echo message="come here......" /> <echo message="deploy success......." />
</target>
<target name="undeploy" description="删除EAR服务">
<wldeploy action="undeploy" name="${project.name}" targets="${wls.server.name}" user="${wls.username}" password="${wls.password}" adminurl="t3://${wls.hostname}:${wls.port}" debug="false" verbose="false" failonerror="false" />
</target>
</project>
如果项目中使用了泛型,使用该脚本如果抛出无法确定 T 的类型参数;对于上限为 T ,java.lang.Object 的类型变量 T,不存在唯一最大实例异常,在eclipse中可以编译运行,为什么用ant就不行了呢?
解决方法是在classpath加入${eclipse_home}/plugins/org.eclipse.jdt.core_3.6.1.v_A68_R36x.jar和从org.eclipse.jdt.core_3.6.1.v_A68_R36x.jar解决出来的jdtCompilerAdapter.jar两个包。