奇怪,刚刚我不是回答过一次吗?怎么没有了!!!!
视乎你使用的不同的应用服务器,你必须设置
java.naming.factory.initialtialContextFactory
java.naming.provider.url
java.naming.security.principal
java.naming.security.credentials
这几个属性,至于对应的值是什么就各有不同了,可以参考应用服务器的文档
weblogic:
java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
java.naming.provider.url=t3://localhost:7001
java.naming.security.principal=system
java.naming.security.credentials=password
最好就是使用一个JNDI.Properties文件来进行配置
那么上面那段程序就什么也不用改,只要增加一个jndi.properties文件
视乎你使用的不同的应用服务器,你必须设置
java.naming.factory.initialtialContextFactory
java.naming.provider.url
java.naming.security.principal
java.naming.security.credentials
这几个属性,至于对应的值是什么就各有不同了,可以参考应用服务器的文档
weblogic:
java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
java.naming.provider.url=t3://localhost:7001
java.naming.security.principal=system
java.naming.security.credentials=password
最好就是使用一个JNDI.Properties文件来进行配置
那么上面那段程序就什么也不用改,只要增加一个jndi.properties文件
解决方案 »
- Spring security UserDetailService有什么用
- 如何在UNIX中调用Microsoft SQL Server 2000 Driver for JDBC?
- 使用struts2 hibernate spring,如果快速连续性的做一个删除操作(但不限于删除操作),然后返回的页面不能及时反映出来,这是怎么回事?
- 还有一年不到,就毕业了,很纠结!!!!!!
- No tag "datetimepicker" defined in tag library imported with prefix "s"
- weblogic启动不起来,报空指针错误啊,大家帮看看啊
- Struts怎么实现不刷新Action
- 请问下现在j2ee过时了吗?
- reference应该如何翻译?
- 发现了Tomcat和IIS结合使用后一个惊天的大BUG
- 谁能提供一个java解析xml的原码?包括插入与删除接点!
- 救命?
MyHome Home=(MyHome)ctx.lookup("MyHome");
最好通过javax.rmi.PortableRemoteObject.PortableRemoteObject去narrow得到home对象.
Properties props=System.getProperties();
Context ctx=new InitialContext(props);
Object ref = ctx.lookup("MyHome");
MyHome home = (HelloHome)javax.rmi.PortableRemoteObject.PortableRemoteObject.narrow(ref,MyHome.class);
....
你看看这么写应该没问题的。
其次需要narrow一下。home不能简单的用单括号强定义!楼上说的很对。
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial到底是什么问题,而且在编译的时候出现两个警告
1 "EjbStateless.ejbgrpx": Spaces in the temporary directory path may cause WebLogic EJBC utility to produce fatal compile errors.
2 "EjbStateless.ejbgrpx": Spaces in the classpath may cause WebLogic EJBC utility to produce fatal compile errors.
可不可能是weblogic配置有问题,为这个问题忙了两天了,大家救命了!!
import java.util.Properties;
import javax.rmi.PortableRemoteObject;public class HelloClient extends Object {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private static final int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private HelloHome helloHome = null;
private Hello hello = null; //Construct the EJB test client
public HelloClient() {
initialize();
} public void initialize() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
} try {
//get naming context
Context context = getInitialContext(); //look up jndi name
Object ref = context.lookup("hello");
//look up jndi name and cast to Home interface
helloHome = (MyHome) PortableRemoteObject.narrow(ref, MyHome.class);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded initializing local bean access through Local Home interface.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed initializing bean access.");
}
e.printStackTrace();
}
} private Context getInitialContext() throws Exception {
String url = "t3://fileserver:7001"; //Web server address and port number
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
} return new InitialContext(properties);
}
catch(Exception e) {
log("Unable to connect to WebLogic server at " + url);
log("Please make sure that the server is running.");
throw e;
}
} //----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//---------------------------------------------------------------------------- public Hello create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
hello = helloHome.create();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: create()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: create()");
}
e.printStackTrace();
} if (logging) {
log("Return value from create(): " + hello + ".");
}
return hello;
} //----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//---------------------------------------------------------------------------- public String hello() {
String returnValue = ""; if (hello == null) {
System.out.println("Error in hello(): " + ERROR_NULL_REMOTE);
return returnValue;
} long startTime = 0;
if (logging) {
log("Calling hello()");
startTime = System.currentTimeMillis();
} try {
returnValue = hello.hello();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: hello()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: hello()");
}
e.printStackTrace();
} if (logging) {
log("Return value from hello(): " + returnValue + ".");
}
return returnValue;
} //----------------------------------------------------------------------------
// Utility Methods
//---------------------------------------------------------------------------- private void log(String message) {
if (message == null) {
System.out.println("-- null");
return ;
}
if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");
}
else {
System.out.println("-- " + message);
}
}
//Main method public static void main(String[] args) throws Exception{
HelloClient client = new HelloClient();
//Properties props=System.getProperties();
//Context ctx=new InitialContext(props);
//Object obj=ctx.lookup("HelloHome");
//HelloHome Home=(HelloHome)javax.rmi.PortableRemoteObject.narrow(obj, HelloHome.class);
//Hello hello=Home.create();
//System.out.println(hello.hello());
//hello.remove();
client.create();
String retValue=client.hello();
System.out.println(retValue);
}
}
但在查找时发生了Error:
javax.naming.NameNotFoundException: Unable to resolve 'HelloHome' Resolved: '' Unresolved:'HelloHome' ; remaining name 'HelloHome'
能告诉我为什么吗?
而且我是在开启Weblogic时运行的,我的JBuilder8+weblogic 7配置有问题吗?大家帮忙就帮到底了,小弟先谢了!
CustomerHome home;
Object ref;// Obtain a reference to the CustomerHome
ref = jndiContext.lookup("java:comp/env/ejb/Customer");// Cast object returned by the JNDI lookup to the
// appropriate datatype
home = PortableRemoteObject.narrow(ref, CustomerHome.class);// Use the home interface to create a
// new instance of the Customer bean.
Customer customer = home.create(customerID);// Use a business method on the Customer.
customer.setName(someName);客户机首先使用 JNDI ENC 查找服务器 bean,以获取对本地接口的引用。在 EJB 1.1 中,Java RMI-IIOP 是指定编程模块。因而,必须支持所有 CORBA 引用类型。不能使用 Java 本地强制转型来强制转型 CORBA 引用。事实上,PortableRemoteObject.narrow() 方法必须用于明确将引用的范围从一种类型缩小到其子类型。由于 JNDI 始终返回 Object 类型,因此应该明确缩小所有 bean 引用的范围以便支持容器间的可移植性。 获取了本地接口之后,客户机使用本地接口上定义的方法来创建、查找或除去服务器 bean。调用本地接口上的一个 create() 方法将向客户机返回一个服务器 bean 的远程引用,客户机将使用该远程引用来完成任务。这是ibm一个教程里的内容