Java compilation with 2 classes
This is some very simple java OOP but I haven't done this in a while...I'm getting a "symbol not found" error when referencing one java class from another
Class #1:
package toaV2;
import java.sql.Connection;
public class vehicle_model
{
public db_model DB;
public Connection conn;
public static void main(String[] args) {
vehicle_model v = new vehicle_model("system");
}
public vehicle_model(String sys) {
DB = new db_model(sys);
conn = DB.connect();
if(conn != null) {
System.err.println("Got a connection.");
}
else {
System.err.println("Couldn't get a connection...");
}
}
}
Class #2:
package toaV2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class db_model
{
private static String driver = "com.my开发者_如何学编程sql.jdbc.Driver";
private static String dbUser = "user";
private static String dbPass = "pass";
private static String dbUrl = "jdbc:mysql://url";
private static String system;
public static Connection conn;
public db_model(String sys)
{
system = sys;
}
public static Connection connect()
{
conn = null;
try
{
String dbName = system.toUpperCase();
String dbHost = dbUrl + dbName;
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPass);
}
catch(Exception e)
{
System.err.println("Exception: " + e.getMessage());
}
return conn;
}
}
And the errors I get on compilation:
$ javac vehicle_model.java vehicle_model.java:10: cannot find symbol symbol : class db_model location: class toaV2.vehicle_model public db_model DB; ^ vehicle_model.java:24: cannot find symbol symbol : class db_model location: class toaV2.vehicle_model DB = new db_model(system); ^ 2 errors
you need to provide the classpath to the other java files when you're compiling. i.e. javac -classpath path/to/class2 vehicle_model.java
You must compile your two files in the same command, like
javac vehicle_model.java db_model.java
精彩评论