0

I'm fairly inexperienced with Java and Jars and I'm just completely lost as to what I should be trying to do.

I have an oracle driver ojbc7.jar. It needs to stay where it is because I'm on a server with limited permissions.

I also have code which should use the driver. Feel free to take a look, but I don't think it will help.

import java.sql.*;
import java.math.*;

public class LoginTest{

  public static void main(String[] args){
    Connection con;

    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch(ClassNotFoundException ex) {
      System.out.println("Error: unable to load driver class!");
      System.exit(1);
    }
    try {
      con = DriverManager.getConnection("jdbc:oracle:thin:@oracle-prod:1521:OPROD", "username", "password");

      con.close();
    } catch(Exception ex) {
      System.out.println("Error: Connection Failure!");
      System.exit(1);

    }

  }
}

My understanding is that my code needs to know the path to my driver. Other suggestions I've found suggest using the following format:

Compile:

javac -cp ojdbc7.jar LoginTest.java

Run:

 java -cp ojdbc7.jar LoginTest

BUT, this returns the error:

Error: Could not find or load main class LoginTest

So now I'm stumped. Do you know what I'm trying to do? What should be doing differently?

  • *I have an oracle driver ojbc7.jar. It needs to stay where it is because I'm on a server with limited permissions.* Awesome. **Where** is it? Also, you haven't needed *`Class.forName("oracle.jdbc.driver.OracleDriver");`* since Java 6 (JDBC 4). – Elliott Frisch Oct 25 '18 at 23:01
  • And why does it have to stay *there*? If you can create files `LoginTest.java` and `LoginTest.class`, then you can also copy the `ojdbc7.jar` to the same folder. – Andreas Oct 25 '18 at 23:03
  • Summary of duplicate: You need to add current directory to the classpath: `java -cp .;ojdbc7.jar LoginTest` (if Windows) – Andreas Oct 25 '18 at 23:04
  • It's in the same folder as the code! Even as I'm compiling it! – Wolfgang.Finkbeiner Oct 25 '18 at 23:04
  • 1
    On Windows, `set "CLASSPATH=.;ojdbc7.jar"` - On most other platforms `export CLASSPATH=".:ojdbc7.jar"` - and yes those quotes are both different and correct for their respective platforms. – Elliott Frisch Oct 25 '18 at 23:06
  • Colon for unix!? I never made the connection that the semicolon wasn't anything more than a fluke! I figured it had to be since I'm on ubuntu and I tried entering it in just to have it bugger everything up in a different way! Thank you so much! – Wolfgang.Finkbeiner Oct 25 '18 at 23:13

0 Answers0