不知道你用的时候什么 部属服务器。要设置一些属性值
weblogic的:
private InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable();props.put(
InitialContext.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
props.put(InitialContext.PROVIDER_URL, "t3://127.0.0.1:7001");// This establishes the security for authorization/authentication
props.put(InitialContext.SECURITY_PRINCIPAL,"username");
props.put(InitialContext.SECURITY_CREDENTIALS,"password");InitialContext initialContext = new InitialContext(props);
return initialContext;
}
weblogic的:
private InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable();props.put(
InitialContext.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
props.put(InitialContext.PROVIDER_URL, "t3://127.0.0.1:7001");// This establishes the security for authorization/authentication
props.put(InitialContext.SECURITY_PRINCIPAL,"username");
props.put(InitialContext.SECURITY_CREDENTIALS,"password");InitialContext initialContext = new InitialContext(props);
return initialContext;
}
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "localhost:1099");
env.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
try
{
Context ctx = new InitialContext(env);
Object obj = ctx.lookup( "HelloWorld" );
HelloWorldHome home =(HelloWorldHome)javax.rmi.PortableRemoteObject.narrow(
obj, HelloWorldHome.class );
HelloWorld helloWorld = home.create();
System.out.println( helloWorld.hello());
helloWorld.remove();
}
catch ( Exception e )
{
e.printStackTrace();
System.out.println( "Exception: " + e.getMessage() );
}
public class ConverterClient { public static void main(String[] args) throws Exception{
try {
Context initial = new InitialContext();
Properties environment= null;
environment.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
environment.setProperty(Context.PROVIDER_URL, "t3://localhost:7001");
environment.put(InitialContext.SECURITY_PRINCIPAL,"weblogic");
environment.put(InitialContext.SECURITY_CREDENTIALS,"PASSWORD");
initial=new InitialContext(environment);
Object objref = initial.lookup("MyConverter");
HelloHome home =
(HelloHome) PortableRemoteObject.narrow(objref,
HelloHome.class); Hello currencyConverter = home.create(); double amount = currencyConverter.dollarToYen(100.00);
System.out.println(amount);
amount = currencyConverter.yenToEuro(100.00);
System.out.println(amount); currencyConverter.remove(); } catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
}
"weblogic.jndi.WLInitialContextFactory");
的时候就抛出了NullPointerException空指针的异常,我晕了
public static void main(String[] args) throws Exception{
try {
Context initial = new InitialContext();
properties = new Properties();
environment.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
environment.put(Context.PROVIDER_URL, "t3://localhost:7001");
environment.put(InitialContext.SECURITY_PRINCIPAL,"weblogic");
environment.put(InitialContext.SECURITY_CREDENTIALS,"PASSWORD");
initial=new InitialContext(environment);
public class ConverterClient { public static void main(String[] args) throws Exception {
try {
InitialContext initial= getContext();
Object objref = initial.lookup("MyConverter");
HelloHome home =
(HelloHome) PortableRemoteObject.narrow(objref,
HelloHome.class); Hello currencyConverter = home.create(); double amount = currencyConverter.dollarToYen(100.00);
System.out.println(amount);
amount = currencyConverter.yenToEuro(100.00);
System.out.println(amount); currencyConverter.remove(); } catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
} private static InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable(); props.put(
InitialContext.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
props.put(InitialContext.PROVIDER_URL, "t3://127.0.0.1:7001");// This establishes the security for authorization/authentication
props.put(InitialContext.SECURITY_PRINCIPAL, "weblogic");
props.put(InitialContext.SECURITY_CREDENTIALS, "PASSWORD"); InitialContext initialContext = new InitialContext(props);
return initialContext;
}}