1

I'm new to java and trying to write a simple program using JDBC. It must be something obvious but I just don't see it. I've been defining the class path manually because the only two classes (at least that I think the program needs when it runs is sitting within the same directory as the program, that is, files for ojdbc_g.jar and JDBCexample.class).

The program compiles fine using:

javac -cp ./ojdbc_g.jar JDBCexample.java

As you can tell, Oracle's JDBC driver (ojdbc_g.jar) is located in the same directory as file: JDBCexample.java. After compiling, I now also have file JDBCexample.class in this same directory. When I try to run it using:

java -classpath "*" JDBCexample    or,
java -classpath ./ojdbc6_g.jar:./JDBCexample.class JDBCexample  or,
java -classpath "*":ojdbc6_g.jar JDBCexample

I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: JDBCexample
Caused by: java.lang.ClassNotFoundException: JDBCexample
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: JDBCexample.  Program will exit.

Here's the file: JDBCexample.java:

import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;

class JDBCexample {

    public static void main(String args[]) throws SQLException {
            Connection conn;
            Statement stmt;
            String query;
            String sqlString;
            ResultSet rset;

            // connect to database
            OracleDataSource ds = new OracleDataSource();
            ds.setURL("jdbc:oracle:thin:@host6.mydomain.com:1521:ORCL");
            conn = ds.getConnection(HR,HR);

            // create Oracle DatabaseMetaData object
            DatabaseMetaData meta = conn.getMetaData();
            // show JDBC driver version
            System.out.println("JDBC driver version is " + meta.getDriverVersion());
    }
3
  • Try this java -cp .:./ojdbc6_g.jar JDBCexample Commented Feb 23, 2012 at 0:34
  • Try: java -classpath . JDBCexample. Commented Feb 23, 2012 at 0:34
  • Hi sarnold, yah I tried that too but didn't work. Hi RanRag, yes this appears to work. I'm getting errors but they're at least related to JDBC, so it seems to execute. Please post as answer below. Commented Feb 23, 2012 at 0:42

1 Answer 1

3

You're compiling the class to the local directory. So when you run it, you need to include the current directory in your classpath.

If on linux machine

java -cp .:./ojdbc6_g.jar JDBCexample

if on Windows machine

java -cp .;./ojdbc6_g.jar JDBCexample
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.