gpt4 book ai didi

Java Axis2 ExceptionInInitializerError Web 服务

转载 作者:行者123 更新时间:2023-11-30 00:20:29 25 4
gpt4 key购买 nike

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

到目前为止我做了什么:

  • 创建了 MySQL 数据库
  • 创建了一个“单独的”(用于测试连接)工作类(作为一个简单的 java 程序,而不是 Web 服务)来尝试通过我的连接进行一些简单的 SQL 查询。这一切都像预计。
  • 接下来我关注了THIS为我的网络服务设置教程项目。

这就是我的问题开始的地方。所以我已经创建了该服务,我现在正在做的是右键单击我的项目并选择 RUN AS > Run On Server...顺便说一句,我正在使用 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

我不太确定我可能做对或做错了什么。服务器本身启动,我可以转到“服务”链接,它只是显示为带有该消息的错误服务。

我研究并尝试了一些不同的事情。其中之一是确保我的 MySQL 连接器 JAR 位于 WEB-INF 文件夹中并相应地更改了我的构建路径,但无济于事 - 同样的错误。

我的项目当前有两个类。实际的服务类是 DBHelper 类,我将用它来与数据库交互。 PropLoader 类只是加载我的属性文件,其中包含数据库连接信息。我将采取的下一个逻辑步骤是尝试在没有属性文件的情况下使其全部正常工作,并将连接信息硬编码到实际的服务类中。但我认为问题是我的 JDBC 没有被读取?难道我错了吗?

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

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;
}

}

最佳答案

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

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

在您的类(class)中,您可以检查该行是否发生任何异常:

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

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

关于Java Axis2 ExceptionInInitializerError Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23283704/

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