I am creating my first .jar file and having bad luck. I can execute my application normally, and it runs. When I jar it it cannot find my Oracle JDBC driver. To me, it seems if the Oracle .jar (classes12.jar) was not in the classpath I wouldn't be able to execute by just saying "java TextAreaAudit". It connects to the database and retrieves data. "java -jar appl.jar" opens the Swing window, but returns a ClassNotFoundError and mentions the Oracle JDBC driver - so that is what I assume is not found. What have I missed? I've been following Chapter 16 in "Head First Java" and I'm stumped. Should be simple (famous last words).
You will need to either place the Oracle.jar file in your classpath or unjar Oracle.jar and then jar its contents up along with your stuff. You cannot place a jar within a jar and have it work. I suspect that is what you probably did.
Joined: Oct 04, 2003
I have the Oracle.jar in the classpath. The application wouldn't work if it wasn't there - I am executing in directory C:\j2sdk1.4.2_03\demo\Test with the command "java TextAreaAudit" and the applicatioon performs just fine. I thought the classpath was the problem, hence the trial of executing from that directory.
If you run an application with "java -jar", the jar is cconsidered to be "closed." Java won't look on the CLASSPATH for any additional classes. It will look in the jar, in the system classes, and in the standard extensions directory (JAVA_HOME/jre/lib/ext) and nowhere else. You can either put the JDBC driver into the "ext" directory, or you can use "java -classpath foo.jar MyClassName" instead of "java -jar foo.jar."
"java -classpath appl.jar TextAreaAudit" returns the same error. My classpath statement is: ".;C:\oracle\ora90\jdbc\lib\classes12.jar;C:\ApacheOrg\commons-lang-2.0\commons-lang-2.0.jar" Thw Swing Panel opens, I press the button to have the database access occur and the message ClassNotFoundException: oracle.jdbc.driver.OracleDriver is displayed in the Text box by my Try-Catch.
author and iconoclast
Yes, right. Sorry, I was sloppy in my answer. If you explicitly specify the classpath, then it should include the Oracle jar too, since under these circumstances CLASSPATH won't be used, either! So use, literally, java -classpath "appl.jar;.;C:\oracle\ora90\jdbc\lib\classes12.jar;C:\ApacheOrg\commons-lang-2.0\commons-lang-2.0.jar" TextAreaAudit and it should work.
Joined: Oct 04, 2003
Worked perfectly! Thanks.
subject: running from .jar file has missing classes