Eclipse的InitialContext问题

时间:2014-11-10 18:58:59

标签: 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错误。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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