eclipse 在(引用)Web 项目中找不到引用的 EJB 项目的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7591056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Classes of referenced EJB project not found in (the referencing) Web project
提问by ars-longa-vita-brevis
I'm learning J2EE and EJB and I've run into a strange problem. This is my project setup:
我正在学习 J2EE 和 EJB,但遇到了一个奇怪的问题。这是我的项目设置:
I'm using Glassfish 3.1 and Eclipse 3.7. I have two projects in Eclipse:
我使用的是 Glassfish 3.1 和 Eclipse 3.7。我在 Eclipse 中有两个项目:
- An EJB 3.0 compliant project,
myEjbProject - A Dynamic Web Project,
myWebProject
- 一个符合 EJB 3.0 的项目,
myEjbProject - 一个动态的网络项目,
myWebProject
I'm injecting an EJB session bean mySessionBeanPackage.mySessionBeanfrom myEjbProjectinto a servlet myServletPackage.myServletin myWebProjectand then (in the servlet) calling a simple method defined in the session bean.
我注入一个EJB会话beanmySessionBeanPackage.mySessionBean从myEjbProject成servletmyServletPackage.myServlet中myWebProject,然后(在servlet)调用会话bean定义的简单方法。
I've provided myWebProjectwith a reference to myEjbProject(using the Properties -> Project References dialog). However, for some reason, the server is unableto find the session bean. The exact error I get is:
我已经提供myWebProject了一个参考myEjbProject(使用“属性”->“项目引用”对话框)。但是,由于某种原因,服务器无法找到会话 bean。我得到的确切错误是:
SEVERE: Class [ LmySessionBeanPackage/mySessionBean ] not found. Error while loading [ class myServletPackage.myServlet ]
严重:找不到类 [ LmySessionBeanPackage/mySessionBean ]。加载 [ class myServletPackage.myServlet ] 时出错
Note that boththe EJB project and the web project have been published to the server and I've double-checked that the class file generated for the bean is, in fact, present on the server (so I can't think of a reason why the server isn't able to find them).
请注意,这两个EJB项目和Web项目已发布到服务器,我已经反复检查为bean生成的类文件,其实存在于服务器上(所以我不能想出一个理由为什么服务器无法找到它们)。
Can anyone tell me what I'm doing wrong?
谁能告诉我我做错了什么?
I've also tried
我也试过
adding the classes folder of
myEjbProjectexplicitly to the Java Build Path -> Libraries list ofmyWebProject. This is in addition to the project reference (although I think adding a project reference should have sufficed in the first place). Anyway, it makes no difference (same error message as before).creating a JAR file from
myEjbProjectusing the Export -> EJB Jar File option (I don't want to get into writing an Ant script at the moment). I then put this JAR in the Java Build Path -> Libraries section ofmyWebProject. This approach does not solve the problem either (same error message as before).modifying the session bean to have
- no interface
- a local interface
- a remote interface
and modifying the injection accordingly. E.g., for a bean with only a local interface, I used:
@EJB mySessionBeanPackage.mySessionBeanLocal myBeanThis approach did not solve the problem either (same error message as before).
doing away with
myEjbProjectaltogether and adding the session bean tomyWebProjectasmyServletPackage.mySessionBean(same package as the servlet). In this case, I can inject the session bean successfully into the servlet using@EJB mySessionBeanPackage.mySessionBean myBeandoing away with
myEjbProjectaltogether and adding the session bean tomyWebProjectasmySessionBeanPackage.mySessionBean(a new package). In this case also, I can inject the session bean successfully into the servlet using@EJB mySessionBeanPackage.mySessionBean myBean
将 的类文件夹
myEjbProject显式添加到 Java 构建路径 ->myWebProject. 这是对项目参考的补充(尽管我认为首先添加项目参考应该就足够了)。无论如何,它没有区别(与以前相同的错误消息)。myEjbProject使用“导出”->“EJB Jar 文件”选项创建 JAR 文件(我目前不想编写 Ant 脚本)。然后我将这个 JAR 放在 .java 的 Java 构建路径 -> 库部分myWebProject。这种方法也不能解决问题(与以前相同的错误消息)。修改会话 bean 以具有
- 没有界面
- 本地接口
- 远程接口
并相应地修改注入。例如,对于只有本地接口的 bean,我使用了:
@EJB mySessionBeanPackage.mySessionBeanLocal myBean这种方法也没有解决问题(与以前相同的错误消息)。
废除
myEjbProject共以及将所述会话beanmyWebProject作为myServletPackage.mySessionBean(相同的包作为servlet)。在这种情况下,我可以使用以下命令将会话 bean 成功注入到 servlet 中@EJB mySessionBeanPackage.mySessionBean myBean废除
myEjbProject干脆加入会话beanmyWebProject作为mySessionBeanPackage.mySessionBean(新包装)。在这种情况下,我也可以使用以下方法将会话 bean 成功注入到 servlet 中@EJB mySessionBeanPackage.mySessionBean myBean
回答by Ross
I would recommend taking a look at the actual EAR that Eclipse packages and deploys to the server. You can do this by exporting the EAR project (right click on EAR project -> export -> Java EE -> EAR file)
我建议查看 Eclipse 打包并部署到服务器的实际 EAR。您可以通过导出 EAR 项目来完成此操作(右键单击 EAR 项目 -> 导出 -> Java EE -> EAR 文件)
I think the step you are missing is to add the EJB project to the WEB project's MANIFEST file. But just in case you are missing a few more steps I would check the following:
我认为您缺少的步骤是将 EJB 项目添加到 WEB 项目的 MANIFEST 文件中。但以防万一你错过了更多的步骤,我会检查以下内容:
- EJB and WEB projects are modules of the EAR project. Right click the EAR project -> properties -> Deployment Assembly -> Add -> Project -> select the EJB and the WEB project. If you export your EAR project now you will notice that the EJB and the WEB project is packaged with the EAR.
- Add EJB project to MANIFEST of WEB project. Right click the WEB project -> properties -> Deployment Assembly -> Manifest entries -> Add -> select the EJB project. You can open the MANIFEST.MF file and see that it now includes the EJB project.
- EJB 和WEB 项目是EAR 项目的模块。右键单击EAR 项目-> 属性-> 部署程序集-> 添加-> 项目-> 选择EJB 和WEB 项目。如果您现在导出 EAR 项目,您会注意到 EJB 和 WEB 项目与 EAR 一起打包。
- 将 EJB 项目添加到 WEB 项目的 MANIFEST。右键单击WEB项目->属性->部署程序集->清单条目->添加->选择EJB项目。您可以打开 MANIFEST.MF 文件并看到它现在包含 EJB 项目。
Also I like adding the EJB project to the WEB project build path just so that it is included in the WEB's classpath: Right click WEB project -> properties -> Java Build Path -> Projects -> Add -> Select the EJB.
另外,我喜欢将 EJB 项目添加到 WEB 项目构建路径中,以便将其包含在 WEB 的类路径中:右键单击 WEB 项目 -> 属性 -> Java 构建路径 -> 项目 -> 添加 -> 选择 EJB。

