gpt4 book ai didi

java - 无法在 Java 上找到或加载主类

转载 作者:行者123 更新时间:2023-11-28 22:55:12 25 4
gpt4 key购买 nike

我有以下代码,我正在尝试创建到数据库的连接。当我运行以下命令时,出现错误“无法在 java 上找到或加载主类”:java -cp。 java

我可以在 java.java 上运行 javac -classpath "C:\CATALINA_HOME\lib*" 命令我的 .class 和 .java 文件都在同一个目录 WEB-INF\classes\com\onjava

onjava.java

package com.onjava;
import java.sql.*;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.*;

import javax.naming.Context;

public class onjava extends HttpServlet {

private DataSource datasource;

public void init(ServletConfig config) throws ServletException {
try {
// Look up the JNDI data source only once at init time
Context envCtx = (Context) new InitialContext().lookup("java:comp/env");
datasource = (DataSource) ((InitialContext) envCtx).lookup("jdbc/testdb");
}
catch (NamingException e) {
e.printStackTrace();
}
}

private Connection getConnection() throws SQLException {
return datasource.getConnection();
}

public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException {
Connection connection=null;
try {
connection = getConnection();
//..<do JDBC work>..
if (connection != null) {

String message = "You are connected!";
System.out.println(message);


} else {
System.out.println("Failed to make connection!");
}
}
catch (SQLException sqlException) {
sqlException.printStackTrace();
}
finally {
if (connection != null)
try {connection.close();} catch (SQLException e) {}
}
}
}

编辑:

我更改了代码并添加了公共(public)类。但是我在运行时遇到了同样的错误!

package src;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Wrapper;
import java.util.Hashtable;
import java.util.Properties;
import java.io.*;
import javax.activation.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import oracle.jdbc.pool.OracleDataSource;

public class onjava {

public static void main(String[] argv) throws SQLException, NamingException {

Properties prop = new Properties();
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.naming.java.javaURLContextFactory");
Context initialContext = new InitialContext(prop);
if ( initialContext == null){
System.out.print("JNDI problem. Cannot get InitialContext.");
} else {System.out.print("NO JNDI problemb ");}

// Get DataSource
Context envContext = (Context)initialContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/testdb");



System.out.println("\n -------- Oracle JDBC Connection Testing ------");

try {



Connection jdbcConnection = ((Statement) ds).getConnection();
OracleDataSource ods = ((Wrapper) ds).unwrap(OracleDataSource.class);
jdbcConnection.close();

} catch (SQLException e) {

System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;

}

String message = "You are connected!";
System.out.println(message);

}

}

最佳答案

像这样运行java时,需要显式声明主类。您的命令应该看起来更像 java -cp 。 com.onjava.onjava.但是,我也没有在此类中看到主要方法。您需要指向主要方法(即签名 public static void main())。

顺便说一句,以大写字母开头的类名是正确的约定 (Onjava)。

关于java - 无法在 Java 上找到或加载主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28991501/

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