当前位置: 技术问答>java相关
EJB的客户端运行出现以下错误,请指教
来源: 互联网 发布时间:2015-10-06
本文导语: 错误提示如下: 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 我想应该告诉他服务器的位置吧,比...
错误提示如下:
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
我想应该告诉他服务器的位置吧,比如JNDI在那个机子里的WebLogic中设置,书上没有,急啊… …
远程序如下:
public class HelloClient
{
public static void main(String[] args)
{
try
{
//Get System properties for JNDI initialization
Properties props = System.getProperties();
// Get a reference to the HelloHome Object - the factory for Hello EJB Objects
Context ctx = new InitialContext(props);
HelloHome home = (HelloHome) ctx.lookup("HelloWordEJB");
// Use the factory to create the Hello EJB Object
HelloRemote hello = home.create();
// Call the hello() method, and print it
System.out.println(hello.hello());
// Done with EJB Object, so remove it
hello.remove();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
这是书上的例子,其中
//Get System properties for JNDI initialization
Properties props = System.getProperties();
他是获得系统的JNDI信息,可这些信息在那里设置啊
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
我想应该告诉他服务器的位置吧,比如JNDI在那个机子里的WebLogic中设置,书上没有,急啊… …
远程序如下:
public class HelloClient
{
public static void main(String[] args)
{
try
{
//Get System properties for JNDI initialization
Properties props = System.getProperties();
// Get a reference to the HelloHome Object - the factory for Hello EJB Objects
Context ctx = new InitialContext(props);
HelloHome home = (HelloHome) ctx.lookup("HelloWordEJB");
// Use the factory to create the Hello EJB Object
HelloRemote hello = home.create();
// Call the hello() method, and print it
System.out.println(hello.hello());
// Done with EJB Object, so remove it
hello.remove();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
这是书上的例子,其中
//Get System properties for JNDI initialization
Properties props = System.getProperties();
他是获得系统的JNDI信息,可这些信息在那里设置啊
|
代码 写的不对
|
不在同一个容器里得到的上下文是不对的,请参考这段代码:
String url = "t3://127.0.0.1:41887";
String user = "system";
String password = "12345678";
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);
}
//System.out.println("1");
try {
InitialContext ctx = new InitialContext(properties);
Object home = ctx.lookup("JNDI_USER");
UserHome userBean = (UserHome) PortableRemoteObject.narrow(home, UserHome.class);
User userinfo = (User)userBean.create();
java.util.ArrayList ar = new java.util.ArrayList();
boolean al = userinfo.createUser("wfb","password",ar,ar);
} catch (NamingException ne) {
ne.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
String url = "t3://127.0.0.1:41887";
String user = "system";
String password = "12345678";
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);
}
//System.out.println("1");
try {
InitialContext ctx = new InitialContext(properties);
Object home = ctx.lookup("JNDI_USER");
UserHome userBean = (UserHome) PortableRemoteObject.narrow(home, UserHome.class);
User userinfo = (User)userBean.create();
java.util.ArrayList ar = new java.util.ArrayList();
boolean al = userinfo.createUser("wfb","password",ar,ar);
} catch (NamingException ne) {
ne.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
|
系统属性中没有放置以下属性:
INITIAL_CONTEXT_FACTORY和PROVIDER_URL
不同的J2EE server要放不同的值,参考你用的服务器手册。
INITIAL_CONTEXT_FACTORY和PROVIDER_URL
不同的J2EE server要放不同的值,参考你用的服务器手册。
|
在部署描述文件里设置
|
代码没问题。在运行程序前,要先给系统个参数才能执行。