gpt4 book ai didi

java.lang.NoClassDefFoundError

转载 作者:行者123 更新时间:2023-11-29 05:27:16 25 4
gpt4 key购买 nike

我正在尝试运行这个 java 和 mysql 程序,但是当我编译时,它没有显示任何错误。

但是当我运行代码时,它显示了一些异常..

可能是什么错误?

代码:Version.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

class Version
{
public static void main(String args[])
{
Connection con = null;
Statement st = null;
String cs = "jdbc:mysql://localhost:3306/google";
String user = "root";
String password = "root";
try {
con = DriverManager.getConnection(cs, user, password);
st = con.createStatement();
String query = "SELECT * FROM user";
st.executeUpdate(query);
}
catch (SQLException ex)
{
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
finally
{
try
{
if (st != null)
{
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex)
{
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
}
}
}

当我运行代码时,我收到此错误消息

错误信息

Exception in thread "main" java.lang.NoClassDefFoundError: Version
Caused by: java.lang.ClassNotFoundException: Version
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: Version. Program will exit.

最佳答案

类需要声明为public

public class Version{
//rest of code
}

默认情况下,类是 package-private,如 Oracles Java 教程中所述:

If a class has no modifier (the default, also known as package-private), it is visible only within its own package.

当程序试图运行 Version 类时找不到,因为它不可公开访问。

关于java.lang.NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18760733/

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