0

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?

hmatar
  • 2,437
  • 2
  • 17
  • 27
user1527739
  • 71
  • 1
  • 9
  • Rather use a dependency management system such as Maven. Users of your library will then get the dependency when building their system with Maven. – sorencito Jan 14 '13 at 19:22

4 Answers4

2

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.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
  • Ondra can you be any more specific? Is this code to be put inside a file somewhere? is this command line stuff? what are the equivalents of your syntax, what is meant by jar1, jar2 main.jar or com.my.Class? – user1527739 Jan 14 '13 at 21:20
1

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/

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
  • Yes, but I added an explanation as to why it worked. That was my contribution to the conversation over what @Ondra provided so I feel your being a little harsh by knocking it down. – chubbsondubs Jan 14 '13 at 19:56
  • I appreciate the explanation Doorknob. Unfortunately I would like to ask for even more clarification though. Can you either link me to a resource or provide detail as to where this line of code goes, and what is meant by the syntax? I can figure out most of it but do not understand "com.my.Class" – user1527739 Jan 14 '13 at 20:10
  • This isn't Java code. This is the command you would use to run the JVM (ie java) on the command line. This is somewhat operating system dependent because how you execute commands on a command line vary among OSes. But what this command is saying is execute java (first word), add the following jars to the classpath (-cp jar1.jar;jar2.jar) and execute the main() method in the fully qualified class (com.my.Class). This is the name of the Java class where public static void main(String[] args) method lives. com.my is the package that class lives within. – chubbsondubs Jan 14 '13 at 21:47
  • You would execute this from a terminal application like cmd on Windows or terminal on Mac OS or Linux. You may want to find some tutorials about executing Java code on the internet because they can give you much more information than a SO topic like this will. – chubbsondubs Jan 14 '13 at 21:49
0

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
Community
  • 1
  • 1
Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
0

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.

Community
  • 1
  • 1
Jeff Johnston
  • 2,076
  • 1
  • 14
  • 27