线程“main”中的异常java.lang.NoClassDefFoundError启动错误

时间:2015-03-22 18:49:02

标签: java classnotfoundexception

我在Java中遇到了典型的错误。我有下一个结构:

bin/
lib/
src/
   junior/
         databases/
                  homework/Main.java

我的 Main.java 代码是:

package junior.databases.homework;

import java.sql.*;

public class Main {
    private static Connection connection = null;

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        initDatabase();
        System.out.println("Done");
    }

    private static void initDatabase() throws SQLException, ClassNotFoundException {
        Class.forName("org.postgresql.Driver");

        connection = DriverManager.getConnection(
                    "jdbc:postgresql://192.168.136.129:5432/postgres", "postgres", "xxxx");
    }
}

当我这样推出时:

root@debian:/python_codes/Junior/Level1/DB1/ORM/java/java/src/junior/databases/homework# javac Main.java
root@debian:/python_codes/Junior/Level1/DB1/ORM/java/java/src/junior/databases/homework#java Main

我收到了错误:

Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: junior/databases/homework/Main)

我在这篇文章中找到了解决方案 Exception in thread "main" java.lang.NoClassDefFoundError: Hello

我的尝试:

root@debian:/python_codes/Junior/Level1/DB1/ORM/java/java/src# javac junior/databases/homework/Main.java
root@debian:/python_codes/Junior/Level1/DB1/ORM/java/java/src# java junior.databases.homework.Main

完美无缺。 问题是我只能从src /文件夹启动此代码(((有什么办法我可以从 / src / junior / databases / homework 文件夹启动它吗?我需要回到每个src我想发布代码的时间。

1 个答案:

答案 0 :(得分:0)

假设您在src/junior/databases/homework目录中

,请执行此操作
javac ../../../junior/databases/homework/Main.java
java -cp ../../../ junior.databases.homework.Main