1

I have a program that I run from Eclipse successfully.

However, when I want to run it from terminal, I encounter the famous error:

"java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver"

on this line:

Class drvClass = Class.forName("oracle.jdbc.driver.OracleDriver");


PS:

I have the following in CLASSPATH:

/oracle/jdbc/lib/ojdbc6.jar

Also note that I compile it successfully (javac Test2.java). Then when I run it (java Test2), I get the following error:

Error: Could not find or load main class Test2

So I run:

java -classpath ~/Desktop/JDBC2/src Test2

It runs, but I get the above "ClassNotFoundException" though.

8
  • Include the driver jar in the classpath Commented Oct 20, 2015 at 4:38
  • Did you add to the -classpath parameter? Commented Oct 20, 2015 at 4:41
  • Yes, if you mean when running: java -classpath ~/Desktop/JDBC2/src Test2 Commented Oct 20, 2015 at 4:42
  • 1
    Using -classpath is overriding your CLASSPATH variable. Commented Oct 20, 2015 at 4:43
  • 2
    So try -classpath ~/Desktop/JDBC2/src:/oracle/jdbc/lib/ojdbc6.jar, for example. You need both in the classpath, and like I said, -classpath is overriding your CLASSPATH variable. Commented Oct 20, 2015 at 4:46

2 Answers 2

1

I found this question tricky: the reason is related to semicolon after jar file address. At first I changed the directory of MySample.java to another directory (you can don't do that) like C:\ then I removed package address from the source code, at the end I run this command in cmd

java -cp path_to_oracle_driver.jar; MySample

P.S. If you want run it from terminal you have to remove package PackageAddress from the source code and compile it again.

Sign up to request clarification or add additional context in comments.

Comments

1

As @yngwietiger mentioned above in the comments, using -classpath parameter when running the .class file, overrides the original CLASSPATH and the predefined ojdbc6.jar file. So we need to mention both when running:

java -classpath ~/Desktop/JDBC2/src:/oracle/jdbc/lib/ojdbc6.jar Test2 

Or, as a better solution, we can add the current path to CLASSPATH (note the colon and dot at the end):

export CLASSPATH=$CLASSPATH:.

And, in order to run, we just need to type:

Java Test2

1 Comment

you missed ';' at the end of .jar

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.