我正在尝试从this URL
实现EJB示例但客户端的EJBTester.java给出的错误是:
java.io.FileNotFoundException: jndi.properties (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at com.tutorialspoint.test.EJBTester.<init>(EJBTester.java:23)
at com.tutorialspoint.test.EJBTester.main(EJBTester.java:37)
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:674)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.init(InitialContext.java:242)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
at com.tutorialspoint.test.EJBTester.<init>(EJBTester.java:28)
at com.tutorialspoint.test.EJBTester.main(EJBTester.java:37)
Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:278)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:671)
... 5 more
java.lang.NullPointerException
at com.tutorialspoint.test.EJBTester.testStatelessEjb(EJBTester.java:51)
null
at com.tutorialspoint.test.EJBTester.main(EJBTester.java:39)
请您提供一个很好的例子,我可以从中清除我的EJB概念吗?
答案 0 :(得分:1)
您的代码(来自评论)
import javax.naming.InitialContext;
BufferedReader brConsoleReader = null;
Properties props = new Properties();
InitialContext ctx;
{
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader = new BufferedReader(new InputStreamReader(System.in));
将尝试从工作目录
加载jndi.properties
请执行以下操作:
InitialContext initialContext = new InitialContext();
这会从类路径的根目录中加载jndi.properties
作为资源。
如果你真的想要明确加载jndi.properties
(来自类路径),请使用以下
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream input = loader.getResourceAsStream("jndi.properties");
if (input == null) {
throw new IllegalStateException("Could not find jndi.properties in class path");
}
props.load(input);