0
  • Am using IntelliJ 12.0.4 Community Edition.
  • I created a Java console app called DB with main class name of DB.
  • I packaged it into an executable jar file called DB.jar.
  • In that app I connect to an Oracle DB using JDBC.
  • I packaged the necessary JDBC jar files into the DB.jar via the Intellij's Project Structure (Modules, Library)

  • When I execute the app from within IntelliJ it runs successfully

  • If I copy DB.jar to some directory and execute it via "java -jar DB.jar" I get an ClassNotFound exception on oracle.jdbc.driver.OracleDriver
  • I looked in DB.jar and the jdbc jar files (ojdbc6.jar, ojdbc14.jar) are in DB.jar
  • Any thoughts?
Pete B.
  • 3,188
  • 6
  • 25
  • 38
BobL
  • 19
  • 6

1 Answers1

0

Jars in an executable jar are not on the classpath, typically.

You can put them in the same folder as DB.jar and do:

java -cp DB.jar;ojdbc6.jar;ojdbc14.jar <MainClass goes here>

and that should run it.

You could also put the Class-Path entry in the Manifest.MF file inside your jar to reference other jars relative to the location of DB.jar in the computer's file system. Reference the two other jar files and then you can do java -jar DB.jar <MainClass goes here> (assuming the two jars are in the right place.

There is some discussion here about the general frustration about not being able to do what you want to do (and what many others have wanted for years).

Classpath including JAR within a JAR

Explain about Main Class

When you run a java application from the command line, there is some class where the execution starts. It will look something like this:

package com.mycompany.app;

public class StartHere {

    public static void main(String[] args) {
        // Your code goes here ...
    }
}

If this example was the class where your application starts you would use a command line this to start it. This assumes you have the StartHere class in the right package location in a jar on the classpath:

java -cp DB.jar;ojdbc6.jar;ojdbc14.jar com.mycompany.app.StartHere

Notice that this allows you to have multiple applications in the same jar. Just make multiple classes with a main() method and run them with different starting classes shown on the command line.

Community
  • 1
  • 1
Lee Meador
  • 12,829
  • 2
  • 36
  • 42
  • Do I need to explicitly set my classpath to point to them? Seems like that's the point of putting them in the jar. – BobL Aug 09 '13 at 18:56
  • Yep. That's an issue isn't it. – Lee Meador Aug 09 '13 at 19:01
  • Thanks, I'll give that a try. So does bundling the jars into my jar buy me anything? – BobL Aug 09 '13 at 19:04
  • If you squint really hard you can see that you have stored the exact right versions of those jars in your jar and could, in the future, extract them and put them in the folder to run. You would want a 'readme' file in the jar too to explain that. – Lee Meador Aug 09 '13 at 19:05
  • I put the jdbc jars in the dir of my jar, went to that dir, executed java -cp ojdbc6.jar;ojdbc14.jar -jar DB.jar and still get same error (grrr) – BobL Aug 09 '13 at 19:12
  • Notice the changed command. Don't use -jar. Put the name of the main class. – Lee Meador Aug 09 '13 at 19:33
  • The main class is in DB.jar along with a number of other classes. How will java.exe find this class? Do I need to extract it from the jar? – BobL Aug 09 '13 at 20:11
  • Just put the name of a class with a `main()` method where I put the words in angle brackets. – Lee Meador Aug 09 '13 at 20:13
  • Executing using -jar works fine other than the ClassNotFOundException trying to connect to the DB. Why does it matter if I use -jar as opposed to the main class? – BobL Aug 09 '13 at 20:19
  • If you use -jar it ignores the classpath. -jar takes precedence over -cp. – Lee Meador Aug 09 '13 at 20:20
  • My jar contains several classes. It sounds like I need to extract the main() class and then add the the jar without my main() class to the classpath. This whole experiment with trying to bundle all my jars in a single jar seems to have been for naught and I'm back to the usual classes, jars and classpath. Was hoping this bundling would save some file clutter ... guess not. Thanks for you help. – BobL Aug 09 '13 at 20:30
  • No. You do not need to extract your main class file. What happens when you issue the command as shown in the answer above as it appears right now? Reply with the cut and paste of your command, please. – Lee Meador Aug 09 '13 at 20:33
  • Bingo. That works, Thanks. Does java look for the main() class in the first jar in the classpath or does it go down the list and use the first place it finds it? – BobL Aug 09 '13 at 20:39
  • Java looks for the main method in the class you name on the command line. If you have two classes with the same name including package on the classpath that creates an indeterminate condition. One will run. You just don't get to pick which one. – Lee Meador Aug 12 '13 at 15:50