-1

I have scenario below I have all my files (HW2.java and 3 jar files for JDBC/ODBC) in folder src. I am running the following commands but still getting errors but the above code runs fine in eclipse, the problem comes only when code in run via command line:

src> javac -cp ".:*.jar" HW2.java


src>java -cp ".:*.jar" HW2 window building 10 20 300 400

Error: Could not find or load main class HW2

src> java HW2 window building 10 20 300 400

java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:
1521:orcl
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at HW2.DBconnect(HW2.java:22)

Could anyone tell me how to run correctly?

user3251915
  • 11
  • 1
  • 3
  • What makes you think that you can use a * in the -cp argument? Perhaps you need to learn to use ant or maven? – bmargulies Apr 02 '14 at 23:30
  • @bmargulies The problem is more that he uses quotes around the classpath. AFAIK Java (especially on Linux) does support expansion of *, see http://stackoverflow.com/questions/1237093/using-wildcard-for-classpath – Mark Rotteveel Apr 03 '14 at 08:14
  • I think you have the quotes backwards. If you don't quote, the shell will expand, and that won't trigger the feature you've linked to. – bmargulies Apr 03 '14 at 10:21

2 Answers2

0

Try to add the code:

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

before you get the connection.

This will make certain that the Oracle driver has been loaded by the class loader.

Adam F
  • 11
  • 2
0

You should not put quotes around the classpath, it should work when you do:

java -cp .:*.jar HW2 window building 10 20 300 400

I am assuming you are on linux, otherwise you need to use a semi-colon as a classpath entry separator.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197