Java Axis2 ExceptionInInitializerError Web服务

时间:2014-04-25 03:37:05

标签: java mysql eclipse web-services axis2

所以我正在开发一个项目,我需要使用Web服务连接到MySQL。我最终会在Android上编写一个客户端,但我还没有那么远。

到目前为止我做了什么:

  • 创建了一个MySQL数据库
  • 创建一个“单独的”(用于测试连接)工作类(作为一个简单的java程序,而不是Web服务)来尝试 用我的连接输出一些简单的SQL查询。这一切都很有效 预期。
  • 接下来,我按照THIS教程为我设置了一个Web服务 项目

这是我的问题开始的地方。所以我已经创建了这项服务,我现在正在做的是对我的项目进行正确点击并选择RUN AS>在服务器上运行......顺便说一句,我正在使用Eclipse Kepler。

这是我在控制台中看到的:

[INFO] The DBHelper service, which is not valid, caused java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at org.apache.axis2.description.java2wsdl.DefaultSchemaGenerator.<init>(DefaultSchemaGenerator.java:140)
at org.apache.axis2.deployment.util.Utils.fillAxisService(Utils.java:453)
at org.apache.axis2.deployment.ServiceBuilder.populateService(ServiceBuilder.java:388)
at org.apache.axis2.deployment.repository.util.ArchiveReader.buildServiceGroup(ArchiveReader.java:101)
at org.apache.axis2.deployment.repository.util.ArchiveReader.processServiceGroup(ArchiveReader.java:178)
at org.apache.axis2.deployment.ServiceDeployer.deploy(ServiceDeployer.java:82)
at org.apache.axis2.deployment.repository.util.DeploymentFileData.deploy(DeploymentFileData.java:136)
at org.apache.axis2.deployment.DeploymentEngine.doDeploy(DeploymentEngine.java:813)
at org.apache.axis2.deployment.repository.util.WSInfoList.update(WSInfoList.java:144)
at org.apache.axis2.deployment.RepositoryListener.update(RepositoryListener.java:377)
at org.apache.axis2.deployment.RepositoryListener.checkServices(RepositoryListener.java:254)
at org.apache.axis2.deployment.DeploymentEngine.loadServices(DeploymentEngine.java:142)
at org.apache.axis2.deployment.WarBasedAxisConfigurator.loadServices(WarBasedAxisConfigurator.java:283)
at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContext(ConfigurationContextFactory.java:95)
at org.apache.axis2.transport.http.AxisServlet.initConfigContext(AxisServlet.java:584)
at org.apache.axis2.transport.http.AxisServlet.init(AxisServlet.java:454)
at org.apache.axis2.webapp.AxisAdminServlet.init(AxisAdminServlet.java:60)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at com.dylanlegendre.notepal1.PropLoader.<init>(PropLoader.java:35)
at com.dylanlegendre.notepal1.DBHelper.<clinit>(DBHelper.java:17)
... 31 more

我不确定我可能会做什么,也可能做得不好。服务器本身启动,我可以转到SERVICES链接,它只是显示为该消息的错误服务。

我研究并尝试了一些不同的东西。其中一个是确保我的MySQL Connector JAR在WEB-INF文件夹中并相应地更改了我的构建路径,但无效 - 相同的错误。

我的项目目前有两个课程。实际的Service Class是DBHelper类,我将用它与数据库进行交互。 PropLoader类只是在我的属性文件中加载,其中包含数据库连接信息。我将采取的下一个逻辑步骤是尝试使所有工作都没有属性文件,只需将连接信息硬编码到实际的服务类中。但从我认为的问题是我的JDBC没有被读取?我错了吗?

修改 所以我尝试了上面的解决方案并且有效。现在我只需要知道为什么和/或如何解决这种情况。我删除了我的PropLoader实例,只是将实际字符串放入变量中,它没有错误。那么也许你不能在Web服务中拥有另一个类的实例就是我在想什么?如果是这样,我如何从我的属性文件加载而不创建它的实例?

Anywho,这是我的两个类的源代码:

public class DBHelper {

    private static PropLoader loader = new PropLoader("/WEB-INF/lib/info.cfg");

    private final static String DRIVER_PATH = "com.mysql.jdbc.Driver";
    private final static String DB_URL = loader.getDbConnection();
    private final static String USER = loader.getDbUser();
    private final static String PASSWORD = loader.getDbPass();

    private static Connection conn; 
    private static Statement stmt;
    private static String query;
    private static ResultSet rs;
    private static String msg; // the result of the query as a string

    public static String getMsg() {
        return msg;
    }

    public static void setMsg(String MESS) {
        msg = MESS;
    }

    public static void openConnection() {
        try {
            Class.forName( DRIVER_PATH ).newInstance();
            conn = DriverManager.getConnection( DB_URL, USER, PASSWORD );
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void closeConnection() {
        try {
            conn.close();
        } catch (SQLException e) {

            e.printStackTrace();
        }
    }

    public static String executeSingleQuery(String QUERY1, String COLUMN_NAME) {
        try {
            stmt = conn.createStatement();
            query = QUERY1;
            rs = stmt.executeQuery(query);

            while (rs.next()) {
                msg = rs.getString(COLUMN_NAME);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msg;

    }

    public static void executeQuery(String QUERY2, String COLUMN_NAME1) {
        try {
            stmt = conn.createStatement();
            query = QUERY2;
            rs = stmt.executeQuery(query);

            while (rs.next()) {
                msg = rs.getString(COLUMN_NAME1);
                System.out.println(msg);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }



}

public class PropLoader {

    private Properties prop;
    private String path;

    private String dbConnection;
    private String dbUser;
    private String dbPass;

    public PropLoader(String PATH) {
        this.path = PATH;
        this.prop = new Properties();

        try {
            InputStream fs = getClass().getResourceAsStream(path);
            prop.load(fs);

            // retrieve properties
            this.dbConnection = prop.getProperty("dbConnection");
            this.dbUser = prop.getProperty("dbUser");
            this.dbPass = prop.getProperty("dbPass");

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

    public String getDbConnection() {
        return dbConnection;
    }

    public void setDbConnection(String dbConnection) {
        this.dbConnection = dbConnection;
    }

    public String getDbUser() {
        return dbUser;
    }

    public void setDbUser(String dbUser) {
        this.dbUser = dbUser;
    }

    public String getDbPass() {
        return dbPass;
    }

    public void setDbPass(String dbPass) {
        this.dbPass = dbPass;
    }

}

1 个答案:

答案 0 :(得分:0)

如果在类ClassLoader中加载类时发生错误或异常,则会发生ExceptionInInitializerError。

如果在初始化类中存在的任何静态变量时出现任何错误,或者存在类似的静态块,则可能会发生这种情况:

在您的课程中,您可以检查该行是否发生任何异常:

private static PropLoader loader = new PropLoader("/WEB-INF/lib/info.cfg");

除了捕获IOException之外,您还可以捕获Exception以检查您的PropLoader构造函数是否成功完成而没有任何错误。如果那里存在错误或异常,则可能导致文件"/WEB-INF/lib/info.cfg"是否存在于所述位置的战争中。