aspose file tools*
The moose likes Beginning Java and the fly likes Compiling with packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiling with packages" Watch "Compiling with packages" New topic
Author

Compiling with packages

Dan Mortimer
Greenhorn

Joined: Jun 01, 2004
Posts: 18
I have a class (in a package) to open and close database connections:

package com.rexam.sem.db.semDB;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.lang.ClassNotFoundException;

public class SEMConnector {

public Connection getConnection() {
Connection conn = null;

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String connUrl = "jdbc dbc:sem_1.0";
conn = DriverManager.getConnection(connUrl, "", "");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Class Not Found Exception:");
System.out.println(cnfe);
}
catch(SQLException sqle)
{
System.out.println("SQL Exception:");
System.out.println(sqle);
}

return conn;
}

public void closeConnection(Connection c) {
try
{
c.close();
System.out.println("Connection closed successfully");
}
catch(SQLException sqle)
{
System.out.println("SQL Exception:");
System.out.println(sqle);
}
}

}



I have complied this class from the command line without any problem:
javac SEMConnector.java

I also have a second class (in the same package) where I have a simple query:

package com.rexam.sem.db.semDB;

import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;

public class SEMQuery {

public ResultSet execute () {
ResultSet results = null;

SEMConnector semCon = new SEMConnector();
Connection conn = semCon.getConnection();

try
{
Statement dbStatement = conn.createStatement();
results = dbStatement.executeQuery("select * from semServer");

while (results.next())
{
System.out.println ("uID : " + results.getInt("uID"));
}

semCon.closeConnection(conn);
}
catch(SQLException sqle)
{
System.out.println("SQL Exception:");
System.out.println(sqle);
}

return results;
}
}



I have tried to compile this class from the command line:
javac SEMQuery.java

...but the compiler complains that it cannot resolve symbol: SEMConnector

I have noticed that if I comment out the package statement from both of my classes, I can get them both to compile just fine.

I really need to compile these classes in the packaage though so I'd be very grateful if someone could point out the mistake I'm making.

Thanks!
Julia Reynolds
Ranch Hand

Joined: May 31, 2001
Posts: 123
Dan,

When compiling the second class, you need to make sure that the classpath of SEMConnector.class is visible. If the SEMConnector.class is in the same directory as the source, try this at the command line:

set classpath=%classpath%;.
javac *.java

the "." tells the compiler that everything in the current directory is in the classpath. If the SEMConnector.class is in a different directory, put that directory in the classpath, something like so:

set classpath=%classpath%;C:\ConnectorCode\classes
javac *.java

Julia
Mark Patrick
Ranch Hand

Joined: Feb 22, 2004
Posts: 51
Also, make sure that you put SEMConnector.class in a directory that matches your package 'com.rexam.sem.db.semDB', ie... \com\rexam\sem\db\semDB. That's basically what a package does, it specifies the directory where the class can be found.


Mark Patrick<br />SCJP 1.4
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The CLASSPATH needs to contain the directory that contains the com directory. For information on CLASSPATH, you should visit this page in our FAQ.

HTH

Layne


Java API Documentation
The Java Tutorial
Dan Mortimer
Greenhorn

Joined: Jun 01, 2004
Posts: 18
Hi Guys,

Thanks very much for your help. Based on your advice, I managed to get both classes to compile using set classpath=%classpath%;. at the command line.

Before compiling the classes though, I checked my classpath and found that it was set to the following:

.;C:\SQLServerDriver\lib\msbase.jar;C:\SQLServerDriver\lib\mssqlserver.jar;C:\SQLServerDriver\lib\msutil.jar

Notice the ".;" at the beginning...since both my classes are in the same directory (and correct package structure) I shouldn't have had a problem with compilation in the first place, should I...?

If you have any thoughts on this I'd be glad to hear them, becuase although both my classes compile now, I'm still scratching my head about why the ".;" entry in my classpath seemed not to have any effect.

Thanks again for your help,



Dan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compiling with packages
 
Similar Threads
Strange Problem with servlet output
Please Help! I Have Worked On IT For Days.
sql exception : user property is missing ???
I am not able to create DSN
consuquences of not closing ResultSet?