使用MyEclipse+JBOSS开发EJB问题:
_xZb;PbFE 1.要在Eclipse中JRE类库中加入
%JBOSS_HOME%/client/jbossall-client.jar 否则会抛出异常:
Ss#{K; - Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]
- at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
- at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
- at javax.naming.InitialContext.init(InitialContext.java:223)
- at javax.naming.InitialContext.<init>(InitialContext.java:175)
- at org.wind.unit.TestCalculator.main(TestCalculator.java:18)
- Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
- at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
- at java.security.AccessController.doPrivileged(Native Method)
- at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
- at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
- at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
- at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
- at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
- at java.lang.Class.forName0(Native Method)
- at java.lang.Class.forName(Class.java:247)
- at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:46)
- at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
- ... 4 more
J$=b&$I( 2.在MyEclipse创建EJB客户端项目时,需要手动创建jndi.properties也可以在将
%JBOSS_HOME%\server\all\conf\jndi.properties复制到项目中,也可以在CODE中使用Properties类或者Hashtable类创建一个jndi,否则找不到JNDI而抛出异常:
Fn0LE~O}-8 - Exception in thread "main" 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
- at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
- at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
- at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
- at javax.naming.InitialContext.lookup(InitialContext.java:392)
- at org.wind.unit.TestCalculator.main(TestCalculator.java:30)
n<(5B|~y 3.将jndi.properties复制到项目中之后,要在
jndi.properties中加入
java.naming.provider.url=localhost:1099 (不一定是localhost:1099根据开发需要可能会更改)否则会抛出异常:
."@a1_F| - Exception in thread "main" javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]
- at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1465)
- at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1594)
- at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:634)
- at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
- at javax.naming.InitialContext.lookup(InitialContext.java:392)
- at org.wind.unit.TestCalculator.main(TestCalculator.java:30)
- Caused by: java.net.SocketTimeoutException: Receive timed out
- at java.net.PlainDatagramSocketImpl.receive0(Native Method)
- at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:136)
- at java.net.DatagramSocket.receive(DatagramSocket.java:712)
- at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1435)
- ... 5 more
!6i |$*9j""u 使用Propertie类:
HW%bx"r+4f - Properties props = new Properties();
- props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
- props.setProperty("java.naming.provider.url", "localhost:1099");
- InitialContext ict=new InitialContext(props);
}n&JZ`8<s 使用Hashtable:
Q9'V&jm - Hashtable<String, String> prop=new Hashtable<String, String>();
- prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
- prop.put(Context.PROVIDER_URL, "localhost:1099");
- InitialContext ict=new InitialContext(prop);
FO)nW:8] 4.JNDI命名规则是 EAR包名/Bean名称/remote(local) 若没有EAR包则直接是Bean名称/remote(local)。
#mFAl|O VDI S`E 5.当使用local时 要注意测试的时候一定要保证都在一个JVM中。
W&q5cz `uJ l<kHI 6.创建Session Bean时要注意自己使用的是有状态(@Stateful)还是无状态(@Stateless)
[ 此贴被ヾ1.嗰rёn在2008-07-13 17:13重新编辑 ]