在 WebSphere 6.1 上使用 ant/jenkins 生成 EAR 的类转换异常
在将作为 Jenkins 上运行的 ant 构建脚本的一部分生成的 Ear 部署到 Websphere 6.1 后,我遇到了运行时类转换异常。转换类异常出现在某些 DAO 方法中,这些方法将从 SQL 查询返回的对象转换为特定类。
如果我从 Eclipse (RAD) 中生成 EAR,则不会发生类转换异常,并且将 jenkins/ant 生成的ear 中的类文件与 Eclipse 中的类文件进行比较,显示不同的文件大小和内容。
我试图让 ant 使用 Webspehre 6.1 提供的 JDK 执行 javac。所以我在 Jenkins 中设置了一个多配置项目来使用 IBM JDK。我假设这使得 ant javac 任务使用这个 jdk。
这是我的 ant javac 任务:
<javac srcdir="${src.dir}" destdir="${build.dir}" debug="true" debuglevel="vars,lines,source" target="1.5">
<classpath refid="master-classpath" />
</javac>
我现在唯一能想到的就是确保 ant 确实使用这个 jdk 运行,而不是仅仅使用这个 jdk 执行 javac。有办法检查吗?
[编辑] 我已更改 ant.bat 文件以输出 JAVA_HOME,并且它确实在我在 jenkins 中指定的文件下运行。
[edit2]
好的,终于找到了问题的原因:
在 Hibernate DAO 方法中,我们有一些像这样的(糟糕的)代码:
String sql = "select {entity.*}, {entity2.*}, ...";
SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql);
...
List<Entity> queryResult = query.list();
for (Object row : queryResult) {
Object[] arr = (Object[])row;
Entity entity - (Entity)arr[0];
Entity2 entity2 - (Entity2)arr[1];
}
你能发现小学生错误吗?
queryResult
不是 List
而是 List
- 上发生类转换异常(对象行:queryResult)
那么,现在我的问题是 - 类型擦除发生了什么?
Eclipse 使用什么编译器选项允许它在运行时忽略类转换这样的错误?
I'm having runtime class cast exceptions after deploying an ear generated as part of an ant build script running on Jenkins to Websphere 6.1. The cast class exceptions are in some DAO methods which cast Objects returned from SQL queries to specific classes.
If i generate the EAR from within Eclipse (RAD) then the class cast exceptions don't occur, and comparing the class files from the jenkins/ant generated ear with the ones from Eclipse show different file sizes and contents.
I'm trying to make ant execute javac with the JDK supplied with Webspehre 6.1. so I've set up a multi configuration project in Jenkins to use the IBM JDK. I'm assuming that this make the ant javac task use this jdk.
This is my ant javac task:
<javac srcdir="${src.dir}" destdir="${build.dir}" debug="true" debuglevel="vars,lines,source" target="1.5">
<classpath refid="master-classpath" />
</javac>
The only thing I can think of now is to make sure that ant actually runs with this jdk as opposed to just executing javac with this jdk. Is there a way to check?
[edit] I 've changed the ant.bat file to output JAVA_HOME and it does run under the one I specify in jenkins.
[edit2]
Ok, finally found the cause of the problem:
In a Hibernate DAO method we have some (poor) code like this:
String sql = "select {entity.*}, {entity2.*}, ...";
SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql);
...
List<Entity> queryResult = query.list();
for (Object row : queryResult) {
Object[] arr = (Object[])row;
Entity entity - (Entity)arr[0];
Entity2 entity2 - (Entity2)arr[1];
}
Can you spot the schoolboy error?
queryResult
is not a List<Entity>
but a List<Object[]>
- the class cast exception was occuring on for (Object row : queryResult)
So, now my question is - what happened to type erasure?
And what compiler option is Eclipse using that allows it to ignore, at runtime, class casts errors like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能不是 JDK,而是其他库不同。值得检查。
It's possible it's not the JDK, but other libraries which are different. Worth checking.