The moose likes Linux / UNIX and the fly likes how to connect to oracle db table through java program using unix shell script? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Reply Bookmark "how to connect to oracle db table through java program using unix shell script?" Watch "how to connect to oracle db table through java program using unix shell script?" New topic
Author

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

prasanna ganesh
Greenhorn

Joined: Jul 04, 2011
Posts: 9
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
Bartender

Joined: Oct 14, 2005
Posts: 13842

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

Joined: Jul 04, 2011
Posts: 9
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

Joined: Jun 25, 2001
Posts: 12513

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.


One of the most odious afflictions that Business has inflicted on the modern English language is "pro-active". Most of the time it's simply redundantly used in place of the simple old word "active". And a good deal of the rest of the time it means "You're not overworked enough yet, so go out and find more!"
 
 
subject: how to connect to oracle db table through java program using unix shell script?
 
Threads others viewed
How to write contents of oracle table into csv file?
offtopic for cert but related to java basics
BufferedWriter error
Stored procedures in JDBC?
Memory issue with Result set
developer file tools