客房端程序:import javax.naming.*;
import javax.rmi.PortableRemoteObject;public class TimeFunctionsTestClient1
{
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 TimeFunctionsHome timeFunctionsHome = null;
private TimeFunctions timeFunctions = null; //Construct the EJB test client
public TimeFunctionsTestClient1()
{
long startTime = 0;
if (logging)
{
log("Initializing bean access.");
startTime = System.currentTimeMillis();
} try
{
//get naming context
Context ctx = new InitialContext(); //look up jndi name
Object ref = ctx.lookup("TimeFunctions"); //cast to Home interface
timeFunctionsHome = (TimeFunctionsHome) PortableRemoteObject.narrow(ref, TimeFunctionsHome.class);
if (logging)
{
long endTime = System.currentTimeMillis();
log("Succeeded initializing bean access.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e)
{
if (logging)
{
log("Failed initializing bean access.");
}
e.printStackTrace();
}
} public TimeFunctions create()
{
long startTime = 0;
if (logging)
{
log("Calling create()");
startTime = System.currentTimeMillis();
}
try
{
timeFunctions = timeFunctionsHome.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(): " + timeFunctions + ".");
}
return timeFunctions;
} public long getTimes()
{
long returnValue = 0;
if (timeFunctions == null)
{
System.out.println("Error in getTimes(): " + ERROR_NULL_REMOTE);
return returnValue;
}
long startTime = 0;
if (logging)
{
log("Calling getTimes()");
startTime = System.currentTimeMillis();
} try
{
returnValue = timeFunctions.getTimes();
if (logging)
{
long endTime = System.currentTimeMillis();
log("Succeeded: getTimes()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e)
{
if (logging)
{
log("Failed: getTimes()");
}
e.printStackTrace();
} if (logging)
{
log("Return value from getTimes(): " + returnValue + ".");
}
return returnValue;
} 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)
{
TimeFunctionsTestClient1 client = new TimeFunctionsTestClient1();
client.create();
// client.getTimes();
}
}
import javax.rmi.PortableRemoteObject;public class TimeFunctionsTestClient1
{
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 TimeFunctionsHome timeFunctionsHome = null;
private TimeFunctions timeFunctions = null; //Construct the EJB test client
public TimeFunctionsTestClient1()
{
long startTime = 0;
if (logging)
{
log("Initializing bean access.");
startTime = System.currentTimeMillis();
} try
{
//get naming context
Context ctx = new InitialContext(); //look up jndi name
Object ref = ctx.lookup("TimeFunctions"); //cast to Home interface
timeFunctionsHome = (TimeFunctionsHome) PortableRemoteObject.narrow(ref, TimeFunctionsHome.class);
if (logging)
{
long endTime = System.currentTimeMillis();
log("Succeeded initializing bean access.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e)
{
if (logging)
{
log("Failed initializing bean access.");
}
e.printStackTrace();
}
} public TimeFunctions create()
{
long startTime = 0;
if (logging)
{
log("Calling create()");
startTime = System.currentTimeMillis();
}
try
{
timeFunctions = timeFunctionsHome.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(): " + timeFunctions + ".");
}
return timeFunctions;
} public long getTimes()
{
long returnValue = 0;
if (timeFunctions == null)
{
System.out.println("Error in getTimes(): " + ERROR_NULL_REMOTE);
return returnValue;
}
long startTime = 0;
if (logging)
{
log("Calling getTimes()");
startTime = System.currentTimeMillis();
} try
{
returnValue = timeFunctions.getTimes();
if (logging)
{
long endTime = System.currentTimeMillis();
log("Succeeded: getTimes()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e)
{
if (logging)
{
log("Failed: getTimes()");
}
e.printStackTrace();
} if (logging)
{
log("Return value from getTimes(): " + returnValue + ".");
}
return returnValue;
} 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)
{
TimeFunctionsTestClient1 client = new TimeFunctionsTestClient1();
client.create();
// client.getTimes();
}
}
的初始化方式改为:(要import java.util.*) Properties properties = null;
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://localhost:7001");//此处是你EJB服务器的位置
InitialContext context = new InitialContext(properties);
javax.naming.CommunicationException. Root exception is java.net.ConnectException: No server found at HTTP://cgserver2:7001
at weblogic.rjvm.RJVMFinder.findOrCreate(RJVMFinder.java:155)
at weblogic.rjvm.ServerURL.findOrCreateRJVM(ServerURL.java:207)
at weblogic.jndi.WLInitialContextFactoryDelegate.getInitialContext(WLInitialContextFactoryDelegate.java:261)-- Failed initializing bean access.
at weblogic.jndi.WLInitialContextFactoryDelegate.getInitialContext(WLInitialContextFactoryDelegate.java:203)
at weblogic.jndi.WLInitialContextFactory.getInitialContext(WLInitialContextFactory.java:124)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:665)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:246)
at javax.naming.InitialContext.init(InitialContext.java:222)
at javax.naming.InitialContext.<init>(InitialContext.java:198)
at sbtutorial.TimeFunctionsTestClient1.<init>(TimeFunctionsTestClient1.java:33)
at sbtutorial.TimeFunctionsTestClient1.main(TimeFunctionsTestClient1.java:163)
java.lang.NullPointerException-- Calling create()
-- Failed: create()
at sbtutorial.TimeFunctionsTestClient1.create(TimeFunctionsTestClient1.java:70)
at sbtutorial.TimeFunctionsTestClient1.main(TimeFunctionsTestClient1.java:164)
-- Return value from create(): null.
小程设置以后出现。
"SBTutorial.jpx": Invocation Error. Check your CORBA configuration using Tools | Enterprise Setup... | CORBA. Also check IDL settings on the Project | Project Properties | Build | IDL page