java NoClassDefFoundError: StudentRegistrar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9937866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoClassDefFoundError: StudentRegistrar
提问by kevsteelio
I'm getting errors in my code and I've been banging my head against this for a few hours. Any thoughts.... There are two java files in this Application and a DB.properties file. Right now the program isn't supposed to do anything other than test the connection to the database. If I missed something, please forgive me as I am new to Java.
我的代码出现错误,几个小时以来我一直在努力解决这个问题。任何想法.... 此应用程序中有两个 java 文件和一个 DB.properties 文件。现在,除了测试与数据库的连接之外,该程序不应该做任何事情。如果我错过了什么,请原谅我,因为我是 Java 新手。
StudentRegistrar.java
StudentRegistrar.java
package studentregistrar;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class StudentRegistrar {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception
{
// TODO code application logic here
System.out.println(args);
System.out.println(args.length);
if (args.length == 0)
{
System.out.println("Usage: java -classpath driver_class_path"
+ File.pathSeparator
+ ". Test DB.properties");
return;
}
else
SimpleDataSource.init(args[0]);
Connection conn = SimpleDataSource.getConnection();
try
{
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE Test (Name CHAR(20))");
stat.execute("INSERT INTO Text VALUES ('Romeo')");
ResultSet result = stat.executeQuery("Select * FROM Test");
result.next();
stat.execute("DROP TABLE Test");
}
finally
{
conn.close();
}
}
}
SimpleDataSource.java
简单数据源.java
package studentregistrar;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class SimpleDataSource {
public static void init(String filename)
throws IOException, ClassNotFoundException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream(filename);
props.load(in);
String driver = "com.mysql.jdbc.Driver";
url = props.getProperty("jdbc.url");
username = props.getProperty("jdbc.username");
if (username == null) username = "";
password = props.getProperty("jdbc.password");
if (password == null) password = "";
if(driver != null)
Class.forName(driver);
}
public static Connection getConnection()
throws SQLException
{
return DriverManager.getConnection(url, username, password);
}
private static String url;
private static String username;
private static String password;
}
DB.properties
数据库属性
jdbc.url=jdbc:mysql://localhost:3306/registrar
jdbc.username=root
jdbc.password=notmypassword
jdbc.driver=com.mysql.jdbc.Driver
I compile the application in NetBeans and from what I can tell I run the command
我在 NetBeans 中编译应用程序,并根据我的说法运行命令
java -class driver_class_path:. StudentRegistrar DB.properties
And I get the Following errors:
我收到以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: StudentRegistrar
Caused by: java.lang.ClassNotFoundException: StudentRegistrar
at java.net.URLClassLoader.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: StudentRegistrar. Program will exit.
What Am I Doing WRONG!!! I'm sure it's something stupid.
我究竟做错了什么!!!我确定这是愚蠢的事情。
Thanks for your time. Kevsteelio
谢谢你的时间。凯夫斯蒂利奥
回答by Jayan
You need to adjust the path.
您需要调整路径。
First find where the StudentRegistrar.class exists. It must be some where like
首先找到StudentRegistrar.class 所在的位置。它一定是一些像
<path to somefolder>/studentregistrar/StudentRegistrar.class
Include that < folder>
into your class path. (The delimiters in classpath is platform specific. :
for unix like and ;
windows)
将其包含< folder>
到您的类路径中。(类路径中的分隔符是特定于平台的。:
对于类似 unix 和;
windows 的)
回答by Ali Ben Messaoud
Use this class to connect each time
每次都使用这个类进行连接
package pack1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @author Ali
*/
class Connect {
Connection con = null;
public Connect() {
String url = "jdbc:mysql://localhost:3306/database";
String user = "user"; // defaultnya adalah root
String pass = "****";// sesuaikan dengan konfigurasi saat install
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = (Connection) DriverManager.getConnection(url, user, pass);
System.out.print("connecté à la base!");
} catch (SQLException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
public Connection getCon() {
return con;
}
public Connection crtst() {
return con;
}
}
in a test class
在测试课上
Connection con = new Connect().getCon();
try{
Statement st = con.createStatement();
int val = st.executeUpdate("INSERT employee VALUES("+13+","+"'Aman'"+")");
System.out.println("1 row affected");
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
回答by Ravikumar
How to run in command line:
如何在命令行中运行:
Note: SimpleDataSource.java, StudentRegistrar.java and mysql driver (jar) in the same directory from where you are running the following commands
注意:SimpleDataSource.java、StudentRegistrar.java 和 mysql 驱动程序 (jar) 位于您运行以下命令的同一目录中
In Windows
compile:
javac -cp .;mysql_driver_jar SimpleDataSource.java
javac StudentRegistrar.java
run:
java -cp .;mysql_driver_jar StudentRegistrar DB.properties
In Unix
Compile:
javac -cp .:mysql_driver_jar SimpleDataSource.java
javac StudentRegistrar.java
run:
java -cp .:mysql_driver_jar StudentRegistrar DB.properties