Java中的import语句似乎很奇怪,我已经用Java The Complete Reference检查了,我看不出为什么有些东西不起作用。
我在first / mysql / MySQLAccess.java中有两个文件,在first / mysql / test / Main.java中有两个文件,而后者中的import语句为first.mysql.MySQLAccess无效。
package first.mysql.test;
import first.mysql.MySQLAccess;
MySQLAccess.java以
开头package first.mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
public class MySQLAccess {
我无法理解为什么这不起作用,除非我错过了什么,否则它应该完美无缺。我编译了MySQLAccess.java并生成了MySQLAccess.class
Main.java:1: error: package first.mysql does not exist
import first.mysql.MySQLAccess;
^
Main.java:5: error: cannot find symbol
MySQLAccess dao = new MySQLAccess();
^
symbol: class MySQLAccess
location: class Main
Main.java:5: error: cannot find symbol
MySQLAccess dao = new MySQLAccess();
^
symbol: class MySQLAccess
location: class Main
3 errors
我编译了:
john@fekete:~/devel/java/mysqlexample$ javac first/mysql/test/Main.java
源代码: first / mysql / MySQLAccess.java: http://pastebin.com/7WkewTEf first / mysql / test / Main.java: http://pastebin.com/aSsn34fX
我找到了一个奇怪行为的线索,java似乎不像MySQLAccess.class ......
john@fekete:~/devel/java/mysqlexample$ java first/mysql/MySQLAccess.class
Error: Could not find or load main class first.mysql.MySQLAccess.class
答案 0 :(得分:0)
最可能的原因 - first.mysql.MySQLAccess.java没有成功编译开始,或者直接在/ mysql下编译 - 否定了包结构。源可能在那里,但就first.mysql.test.Main.java而言,它只会查找first.mysql.MySQLAccess.class。
首先,确保你有这个结构:
mysqlexample
|--first
|--mysql
|--test
|-- Main.java
MySQLAccess.java
然后发出以下命令:
john@fekete:~/devel/java/mysqlexample$ javac first/mysql/MySQLAccess.java
john@fekete:~/devel/java/mysqlexample$ javac first/mysql/test/Main.java