我在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我想发布代码的时间。
答案 0 :(得分:0)
假设您在src/junior/databases/homework
目录中
javac ../../../junior/databases/homework/Main.java
java -cp ../../../ junior.databases.homework.Main