Surender Thakran
Surender Thakran

Reputation: 4054

Why oracle driver is needed in Eclipse build path?

I am new to JDBC and just made my first program in Eclipse. I am using Oracle 11g XE as database. I have added ojdbc6.jar to my classpath by copying it in the lib folder of the jdk installation. This is my code:

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

public class Test {

/**
 * @param args
 */

String url = "jdbc:oracle:thin:@Voldemort:1521:XE";
String username = "surender";
String password = "oracle";
Statement stmt;
String query;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    new Test().work();

}

void work() {
    try {
        Connection conn = DriverManager.getConnection(url, username, password);
        stmt = conn.createStatement();
        query = "SELECT * FROM employees";
        ResultSet rset = stmt.executeQuery(query);
        while(rset.next()) {
            System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getString(3) + " " + rset.getString(4));
        }
    }
    catch(SQLException se) {
        System.out.println("Exception!!");
        se.printStackTrace();
    }
}

}

Now for some reason it doesn't works in the eclipse nor when I run it outside the IDE through the command prompt (I am on windows 7). But when I added the ojdbc6.jar to the project build path in eclipse it runs fine in the eclipse but as expected still doesn't works from the prompt.

I followed a few tutorials on the net and they all mention that the driver files are needed to be added to the classpath but nothing else. (i added ojdbc6.jar to the buildpath just on a whim, kind of like kiss and tell :p).

Now, why is this?

What kind of resources are needed to be added in the build path?

What if I wanted to run my project outside the IDE, how would i do that?

Thanx in advance!

Upvotes: 0

Views: 1749

Answers (2)

Ravi Trivedi
Ravi Trivedi

Reputation: 2360

Rule of thumb >> Every dependency jar files needs to be added to classpath. ie: JDBC drivers or any other dependent jar.

In eclipse, You could run your project only after you added ojdbc6.jar to your eclipse project classpath because our rule of thumb says so.

Now if you want to run your project from command line, same rule applies. For that, you need to specify all dependent jar files while running your java program.

ie: java -classpath ojdbc6.jar . package.classname

Upvotes: 2

Mark Rotteveel
Mark Rotteveel

Reputation: 109078

To be able to use a JDBC driver (or any other Java library) it needs to be on the classpath. The Eclipse build-path is used as the classpath when the application is executed from within Eclipse.

To run it outside the IDE, you need to be sure you specify the classpath when running it. This can be done using:

java -cp yourlib.jar;ojdbc6.jar name.of.your.MainClass

(this assumes yourlib.jar and ojdbc6.jar are both in the current working directory, on linux use : instead of ;)

Or if you are using an executable jar, you need to make sure ojdbc6.jar is included in the MANIFEST.MF entry Class-Path, eg:

Class-Path: ojdbc6.jar

Now you can run the executable jar as:

java -jar yourlib.jar

(assuming that all other things - like MainClass in the MANIFEST.MF are set right)

Upvotes: 0

Related Questions