gpt4 book ai didi

java - Eclipse 的 InitialContext 问题

转载 作者:行者123 更新时间:2023-12-01 12:18:28 24 4
gpt4 key购买 nike

我收到以下错误消息:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com