Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Including jar files

Status
Not open for further replies.

titanzero

Programmer
May 3, 2007
30
EU
Hello everybody

I have a very simple question...how do include a jar file into my code? I am more used to c++ and I would just tell my code where to find the library I want and then I just use it.

I am using Ubuntu if that makes any difference.

This is really frustrrating me. I've looked on the web and found instructions that tell me to add -classpath option to the compile command line. I then use import in my Java code for the package inside the jar file i want...code compiles but I get a ClassNotFound exception.

Here is my code
Code:
import java.sql.*;
import org.hsqldb.*;
import org.hsqldb.jdbcDriver;  //Probably dont need this?

public class Database
{
	private Connection conn;

	public Database(String un, String pw, String dbf) throws SQLException, ClassNotFoundException
	{
		Class.forName("org.hsqldb.jdbcDriver");
		conn = DriverManager.getConnection("jdbc:hsqldb:file:" + dbf, un, pw);
	}
	
	public int update(String theSQL) throws SQLException, NotConnectedException
	{
		if(conn == null)
			throw new NotConnectedException();
	
		Statement stat = conn.createStatement();
		
		return stat.executeUpdate(theSQL);
	}
	
	public ResultSet query(String theSQL) throws SQLException, NotConnectedException
	{
		if(conn == null)
			throw new NotConnectedException();
	
		Statement stat = conn.createStatement();
		
		return stat.executeQuery(theSQL);
	}
	
	public void close() throws SQLException, NotConnectedException
	{
		if(conn == null)
			throw new NotConnectedException();
	
		update("SHUTDOWN");
	}
}

Code:
import java.sql.*;
import org.hsqldb.*;
import org.hsqldb.jdbcDriver;

public class DatabaseTest
{
	public static void main(String args[])
	{
		System.out.println("START OF PROG");
	
		Database db;
		ResultSet rs;
		try
		{
			//Create a connection to the database
			db = new Database("TestDB","sa","");
			
			//Try and work with the Database making sure to catch any exception
			try
			{
				//Create a table
				db.update("CREATE TABLE animals (type VARCHAR(30))");
				//Add some data
				db.update("INSERT INTO animals VALUES('dog')");
				db.update("INSERT INTO animals VALUES('cat')");
				db.update("INSERT INTO animals VALUES('fish')");
				db.update("INSERT INTO animals VALUES('bird')");
				//Retreive the data
				rs = db.query("SELECT * FROM animals");
				//Loop through rs and out put data
				while(rs.next())
				{
					String type = rs.getString(0);
					System.out.println(type);
				}
			}
			catch(NotConnectedException dbe)
			{
				System.out.println("OPPS! The application is not connected to a Database. This could mean the Database connection details are incorrect. Please confirm details and try again. If problems persist please contact you DBA.");
			}

		}
		catch(SQLException sqle)
		{
			System.out.println("OPPS! SQL Error: " + sqle.getMessage());
		}
		catch(ClassNotFoundException ce)
		{
			System.out.println("OPPS! Class not found: " + ce.getMessage());
		}
		
		System.out.println("END OF PROG");
	}
}

Code:
javac -classpath /media/ExpansionDrive/java_include/hsqldb.jar *.java

NotConnectedException is an empty class.

I tried using NetBeans to help import things but this only made matter worse (the best I could out of NetBeans was it telling me my code was uncompiled and then it would not let me compile any files :S). I just want to be able to write my code in a text editor and compile from the command line. Any help would be much appreciated.

Thank you all so very much for your time
Andrew
 
When you use Class.forName you don't need to use import.

I take it's compiling OK but giving errors at runtime. When you execute it, you need to use the classpath option to indicate where the jar files are

Cheers,
Dian
 
Thanks for the quick reply. I have added the class path option to my run command but now I get a different error. I think the problem is in the way I am phrasing the run command. Here is what I have

Code:
java -classpath /media/ExpansionDrive/java_include/hsqldb.jar DatabaseTest

This gives me an error

NoClassDefFound for DatabaseTest

Next I tried telling java where DatabaseTest is

Code:
java -classpath /media/ExpansionDrive/java_include/hsqldb.jar /media/ExpansionDrive/UNI_WORK/ICP2051/Semester2/LAB02/DatabaseTest

This gives me the same error but instead of just DatabaseTest it gives me .media.ExpansionDrive...LAB02.DatabaseTest instead of just DatabaseTest. It seems odd to me that there are '.' between the directory names.

Any ideas?

Thanks
Andrew
 
Another note...I have been reading the recent post of classpath and have hacked around with ideas suggestion in them but no luck...I am not being lazy and I am honestly stuck. Anyways thanks again everybody :)
 
I'd try

Code:
java -classpath .:/media/ExpansionDrive/java_include/hsqldb.jar DatabaseTest
from the directory where DatabaseTest.class. '.' indicates that the current directory is added to classpath.

The substitution of / with . is because directories are translated into packages when dealing with Java, and package separator is .

You can also have a look at this FAQ

Cheers,
Dian
 
Thanks...seems to be working now...on to the next problem!

Thanks again
Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top