Reputation: 71
I have created a class that will connect to an oracle database, with three simple functions connect()
, executeStatement()
, and disconnect()
. This class requires an oracle "thin" JDBC Jar in order for it to work, so it is part of the 'referenced libraries'.
What I want to do now is to export my class that I have mentioned above as a JAR file so the other programs can make use of it. However when I attempt to do just this, I will get the following issue.
Exception in thread "main" java.lang.NoClassDefFoundError: oracle/jdbc/driver/OracleDriver
Caused by: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
What method can I use to make it so that MY Jar file will have with it the oracle Jar file?
Upvotes: 0
Views: 322
Reputation: 46796
To load classes from multiple jars, java uses so-called classpath. That is a list of jars, but also directories with same structure as jar's content.
This list is defined by the parameters to java
following -cp
.
See the docs, e.g. here.
java -cp jar1.jar;jar2.jar com.my.Class
or
java -cp jar1.jar;jar2.jar -jar main.jar
Also, you can study about MANIFEST.MF which can list it's dependency jars.
Upvotes: 2
Reputation: 38696
You have to add all of the jars that are required by your program (including your code) to the classpath. For example:
java -cp jar1.jar;jar2.jar com.my.Class
The classpath defines where the JVM will look for code when its loading it. Here is a quick but simple tutorial about running Java programs from the command line:
http://www.sergiy.ca/how-to-compile-and-launch-java-code-from-command-line/
Upvotes: 1
Reputation: 2246
If you mean that you want your jar file to be completely self contained and have no dependencies that are exposed to the client code then you can try Jar Jar Links which will rename the dependencies and include them in your jar.
Other possibilities are mentioned in the answers to this similar question.
Upvotes: 0
Reputation: 16060
Take a look at this answer: Is it possible to create an "uber" jar containing the project classes and the project dependencies as jars with a custom manifest file?
You also can google for "uberjar". This describes, how to create a jar, with all embedded classes.
You still can use the classpath:
java -cp jar1.jar;jar2.jar com.my.Class
Upvotes: 0