import java.rmi.*;
import javax.rmi.*;
import javax.naming.*;
import java.util.*;//Äã×Ô¼ºµÄEJB½Ó¿Ú (TimeFunctions)
import sbtutorial.*;public class EjbClient{
String url = "t3://192.168.0.67:9001";
String user = null;
String password = null;
Properties h = new Properties(); public Context getInitialContext(String url,String user,String password) throws NamingException {
try{
h.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL,url);
//h.put(Context.SECURITY_PRINCIPAL,user);
//h.put(Context.SECURITY_CREDENTIALS,password);
return new InitialContext(h);
}catch(NamingException ne)
{
throw ne;
}
}
public void EjbClient(){
}
public static void main(String[] args) {
EjbClient ejbClient = new EjbClient();
ejbClient.start();
} private void start(){
try{
Context initial = getInitialContext(url,user,password);
Object objref = initial.lookup("TimeFunctions");
TimeFunctionsHome tfHome = (TimeFunctionsHome)PortableRemoteObject.narrow(objref,TimeFunctionsHome.class);
TimeFunctions tfEjb = tfHome.create();
System.out.println(tfEjb.getTime());
}catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
import javax.rmi.*;
import javax.naming.*;
import java.util.*;//Äã×Ô¼ºµÄEJB½Ó¿Ú (TimeFunctions)
import sbtutorial.*;public class EjbClient{
String url = "t3://192.168.0.67:9001";
String user = null;
String password = null;
Properties h = new Properties(); public Context getInitialContext(String url,String user,String password) throws NamingException {
try{
h.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL,url);
//h.put(Context.SECURITY_PRINCIPAL,user);
//h.put(Context.SECURITY_CREDENTIALS,password);
return new InitialContext(h);
}catch(NamingException ne)
{
throw ne;
}
}
public void EjbClient(){
}
public static void main(String[] args) {
EjbClient ejbClient = new EjbClient();
ejbClient.start();
} private void start(){
try{
Context initial = getInitialContext(url,user,password);
Object objref = initial.lookup("TimeFunctions");
TimeFunctionsHome tfHome = (TimeFunctionsHome)PortableRemoteObject.narrow(objref,TimeFunctionsHome.class);
TimeFunctions tfEjb = tfHome.create();
System.out.println(tfEjb.getTime());
}catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
解决方案 »
- java高手都上哪去了?
- JSP页面的目的是拓展作外网访问使用,是否有更好的解决方法?速联系!!
- hibernate - ExceptionInInitializerError
- 用java 对base64中文utf-8进行 base64 解码后最后一个字 变为 ? 问题 help
- 请问JAVA与.NET的区别在哪里?JAVA的 STRUTS .NET 里有什么?
- 倾家荡产求:大家谁知道Hex编辑是什么意思??有没有工具 把Hex编辑后的数据 还原回去
- LINUX环境下,在哪个配置文件中设置JAVA_HOME路径
- jfreechart在resin下运行问题?
- 有谁能告诉我这两个的作用啊!!!!
- java新手学习之路
- 用jspsmartupload上传文件,在表单中还有其他几个文本框,用另一个jsp处理,上传文件正常,但其他几个文本框的值始终为null
- 关于struts的问题?
不过编译器说class TimeFunctionsHome not found,
当然我不是用你的TimeFunctionsHome而是改成了我自己的接口。
请问如何解决,谢谢。
INITIAL_CONTEXT_FACTORY和PROVIDER_URL不同的J2EE server要放不同的值,参考你用的服务器手册。
(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
Context ctx = getInitialContext();
//look up jndi name
Object ref = ctx.lookup("Enterprise2");
//cast to Home interface
Enterprise2Home enterprise2Home = (Enterprise2Home) PortableRemoteObject.narrow(ref, Enterprise2Home.class);
Enterprise2 hello=enterprise2Home.create();
System.out.println(hello.hello());
jLabel1.setText(hello.hello());
hello.remove();
}catch(Exception e){
e.printStackTrace();
}
}
private Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
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) {
throw e;
}
}
但运行时老是出现这个异常:
java.lang.ClassCastException at com.sun.corba.se.internal.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:296) at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137) at ddd1.Frame1.jbInit(Frame1.java:108) at ddd1.Frame1.<init>(Frame1.java:43) at ddd1.Application1.<init>(Application1.java:20) at ddd1.Application1.main(Application1.java:49)
我不知道哪儿写得不对,该如何改,请指教。
//jLabel1.setText(hello.hello());
// hello.remove();