Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to connect to oracle db table through java program using unix shell script?

 
prasanna ganesh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a small task where i need to connect to oracle table through a java program, where i'm calling the java program from a unix shell script.

My java program compiling and running fine if i run it individually, however when i'm trying to run through unix shell script i'm getting the following error,

"Unable to connect to database: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver"

This is my scirpt which calling the java program TestDBOracle.class

#!/bin/ksh/

JAVA_HOME=/usr/bin:/usr/ucb:/etc:.
CLASSPATH=/www/a/downloads/
export JAVA_HOME
echo $CLASSPATH
java -cp .:CLASSPATH TestDBOracle

I kept my odbc14.jar and TestDBOracle.class file in the CLASSPATH =/www/a/downloads/


And this is my TestDBOracle.java file which connects to Oracle table and exporting the table into a csv file,



Can someone please help me to fix this problem?

Thanks a lot for your help

 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your classpath must specifically identify the jars which you want to be in it. Just pointing to a directory containing the jars isn't enough.
 
prasanna ganesh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

As you mentioned now i'm explicitly mentioning my ojdbc14.jar file while calling my java program like below,

java -classpath ojdbc14.jar TestDBOracle
and as well as like this
java -classpath ojdbc14.jar .;TestDBOracle

but now i'm getting different ClassNotFoundException like below,

Exception in thread "main" java.lang.NoClassDefFoundError: TestDBOracle

If i ran this program alone its executing fine,

Could you please let me know where i'm going wrong and please correct my mistake and once again thanks for your help.
 
Tim Holloway
Saloon Keeper
Posts: 18303
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need 2 separate classpath items

1. The path where the application classes reside.
2. The path where the Oracle code resides.

So, if you have your class in subdirectory com/mycorp/myapp/TestDBOracle.class, the command line would be something like this:


Notice the ".:ojbbc.jar". "." is shorthand for the current directory. ":" is the classpath separator for Unix/Linux. Note that this is not the same in Windows!!!

In Unix, ";" (semicolon) is a command separator, used to stack multiple commands on the same line, so the ":" (colon) is used to separate components within a command.

In Windows ":" (colon) is part of the pathname "language" - for example "C:\myclasses", so they use the semicolon (";"), instead.

Actually, it's probably better to put the class file under a "classes" subdirectory instead of the actual current directory:


Or in a jar:



You can't use the "java -jar" option when you have 2 separate jars, since the default classloading scheme doesn't support that.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic