我是EJB新手。
我正在努力学习开发无状态bean。我创建了jar文件并将其部署在Weblogic服务器上,然后我执行了客户端代码。我已经为 api.jar 和 weblogic.jar 设置了类路径。但是在运行客户端代码时,我遇到了以下错误:
C:\Users\Asad\Desktop\EJB>java EjbClient1
Exception in thread "main" javax.naming.NameNotFoundException: Unable to resolve 'myAdder#Adder'. Resolved '' [Root exception is javax.naming.NameNotFoundException: Unable to resolve 'myAdder#Adder'. Resolved '']; remaining name 'myAdder#Adder'
at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1224)
at weblogic.jndi.internal.BasicNamingNode.lookupHere(BasicNamingNode.java:273)
at weblogic.jndi.internal.ServerNamingNode.lookupHere(ServerNamingNode.java:217)
at weblogic.jndi.internal.BasicNamingNode.lookupIgnorePartition(BasicNamingNode.java:1503)
at weblogic.jndi.internal.PartitionHandler.lookupSharable(PartitionHandler.java:88)
at weblogic.jndi.internal.ServerNamingNode.lookup(ServerNamingNode.java:584)
at weblogic.jndi.internal.RootNamingNode.lookup(RootNamingNode.java:81)
at weblogic.jndi.internal.RootNamingNode_WLSkel.invoke(Unknown Source)
at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:645)
at weblogic.rmi.cluster.ClusterableServerRef.invoke(ClusterableServerRef.java:248)
at weblogic.rmi.internal.BasicServerRef$2.run(BasicServerRef.java:534)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:368)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:163)
at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:531)
at weblogic.rmi.internal.wls.WLSExecuteRequest.run(WLSExecuteRequest.java:137)
at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:348)
at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:333)
at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:54)
at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:617)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:397)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:346)
Caused by: javax.naming.NameNotFoundException: Unable to resolve 'myAdder#Adder'. Resolved ''
at weblogic.utils.StackTraceDisabled.unknownMethod()
以下是代码。
客户端EjbClient1.java:
import javax.naming.*;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
public class EjbClient1 {
public static void main(String s[])throws Exception {
Properties parm=new Properties();
parm.setProperty("java.naming.factory.initial","weblogic.jndi.WLInitialContextFactory");
parm.setProperty("java.naming.provider.url","t3://localhost:7001");
parm.setProperty("java.naming.security.principal","weblogic");
parm.setProperty("java.naming.security.credentials","asad9711");
// parm.setProperty("java.naming.security.credentials","weblogic12");
InitialContext ctx=new InitialContext(parm);
Adder stub=(Adder)ctx.lookup("myAdder#Adder");
int c=stub.add(10,20);
System.out.println(c);
}
}
Bean类AdderBean.java:
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
@Stateless(mappedName="myAdder")
class AdderBean implements Adder {
@PostConstruct
public void init()
{
System.out.println("post create");
}
@PreDestroy
public void destroy()
{
System.out.println("destroy");
}
public int add(int x,int y)
{
return x+y;
}
}
远程接口Adder.java:
import javax.ejb.Remote;
@Remote
public interface Adder
{
int add(int x,int y);
}
答案 0 :(得分:1)
在改变一些事情之后,你的代码对我有用。
我将会话bean的访问修饰符更改为 public 。
检查你的jar文件是否包含类文件。它必须包含您的Interface和SessionBean。
使用命令行
1)编译项目
Project_DIR>javac -d bin -sourcepath src -cp lib\.m2\repo
sitory\javax\javaee-api\6.0\javaee-api-6.0.jar;C:\Oracle\Middleware\wlserver_10.
3\server\lib\weblogic.jar src\main\java\Adder.java src\main\java\AdderBean.java
src\main\java\EjbClient1.java
2)建立jar
Project_DIR\bin>jar cf myejbapp.jar Adder.class AdderBean
.class EjbClient1.class
3)将myejbapp.jar部署到weblogic并运行EJBClient1
答案 1 :(得分:0)
尝试使用完全限定名称进行查找 以(mappedName#packageName.interfaceName)的形式,即使它们在同一个包
中