-1

我正在尝试连接到 MYSQL 数据库,并且我的程序Sync_DB_RegCheck.java在 Ecipe 中运行良好。但是我想在Ubuntu 12.04 Terminal中执行和运行相同的程序。但是在执行过程中出现错误。

Exception in thread "main" java.lang.NoClassDefFoundError: Sync_DB_RegCheck
Caused by: java.lang.ClassNotFoundException: Sync_DB_RegCheck
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: SYNC_APP.bin.Sync_DB_RegCheck. Program will exit.

我的 Java 程序Sync_DB_RegCheck.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;


public class Sync_DB_RegCheck {

    public static void main(String [] args) throws Exception {


         Connection conn = null;
         Class.forName("com.mysql.jdbc.Driver"); 
         conn = DriverManager.getConnection( "jdbc:mysql://localhost/test?user=root&password=root");
        System.out.println("Connected?");
        try{
              Statement st = (com.mysql.jdbc.Statement) conn.createStatement();
              st.executeUpdate("DROP TABLE del");
              System.out.println ("Table Deletion process is completly successfully!");
              }

        catch(SQLException s){
              System.out.println("Table is not exists!");
              }

}}

编译代码:这是编译程序没有错误

$java -cp mysql-connector-java-5.1.24-bin.jar Sync_DB_RegCheck.java

执行代码:它给出了上述错误

java -cp mysql-connector-java-5.1.24-bin.jar Sync_DB_RegCheck
4

1 回答 1

6

您的java命令包含一个-cp选项,它指定类路径:JVM 将查找要使用的类的位置。

This classpath contains the MySQL driver, which is good. But it does not contain the current directory (indicated by .), where your Sync_DB_RegCheck.class file resides. So you need to add that one too:

java -cp .:mysql-connector-java-5.1.24-bin.jar Sync_DB_RegCheck
于 2013-04-19T13:46:48.413 回答