java - Eclipse 的 InitialContext 问题

标签 java eclipse

我收到以下错误消息:

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 edu.neu.cs5200.Homework4.UserManager.getConnection(UserManager.java:31)
        at edu.neu.cs5200.Homework4.UserManager.createUser(UserManager.java:55)
        at edu.neu.cs5200.Homework4.UserManager.main(UserManager.java:95)
Exception in thread "main" java.lang.NullPointerException
        at edu.neu.cs5200.Homework4.UserManager.createUser(UserManager.java:58)
        at edu.neu.cs5200.Homework4.UserManager.main(UserManager.java:95)

在 web.xml 中(在 Web-INF 文件夹中)

我有这个:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Homework4</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <resource-ref>
        <description>Homework 4 MySQL Data Source</description>
        <res-ref-name>jdbc/DBHomework4</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

然后,在 tomcat 的 context.xml 中我有这个:

  <Resource name="jdbc/DBHomework4" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="root"
               driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:8889/Homework4"/>

所有这些都应该是正确的,但我仍然收到 InitialContext 错误。有什么想法吗?

最佳答案

您直接从 main() 运行应用程序,并且它应该在提供上下文的 Servlet 容器 (Tomcat) 中运行。您应该将应用程序部署到 Tomcat 并从 Servlet 调用 UserManager。

关于java - Eclipse 的 InitialContext 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26850942/

相关文章:

java - 如何在Java中导入源文件以在另一个项目中使用

eclipse - 使用 TomCat 7 在 eclipse 中运行我的 WAR 时出现异常

java - 在枚举中实现接口(interface)时,为什么 eclipse 会自动在被覆盖的接口(interface)方法之前放置一个分号?

java - 关于eclipse调试java apis的问题(再次)

android - 如何使用我的笔记本电脑蓝牙将 Eclipse ADT 模拟器连接到 Android 设备?

java - 在Java中总是至少有一个线程;这在 C++ 中是真的吗?

java - 使用 Mockito 模拟 @Resource spring 属性

java - Java中嵌入groovy,groovy.lang.MissingPropertyException : No such property:

java - 注册-Spring Boot-MongoDb-主键替换新数据

java - BufferedReader 返回与原始文件不匹配的文件