I am using ojdbc5.jar for oracle jdbc driver in myApp.jar.
when I execute my jar file (myApp.jar) it gives the class not found exception.
java -jar myApp.jar
Exception in thread "main" java.lang.NoClassDefFoundError: oracle/jdbc/driver/OracleDriver
I don't know, whats going wrong.
Please help.
--
Nitesh
This message was edited 1 time. Last update was at by Maneesh Godbole
Nitesh,
Also try not to abuse the bold and color tags as it makes your post harder to read.
When required you can use the code and quote tags where appropriate
Probably best to delete that CLASSPATH and use the -cp option for the java and javac tools. I found this, whihc is probably out of date, but not changed that much. You can probably find a more recent editionon the net.
The -cp / -classpath option would be the way to go if the application wasn't packaged as a JAR file. When using the -jar option the -cp / -classpath and the CLASPATH environment variable settings are ignored. All non-system classes should be a part of the application packaged inside the JAR file, or specified as dependecies on the classpath definition inside the manifest.mf file. Have a look at the documentation of the java application launcher, specifically the section that describes the -jar option.
This message was edited 1 time. Last update was at by Jelle Klap
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25060
posted
0
I take your point, Jelle. I also think this is no longer a "beginning" question and I ought to move it.
Ok, let's try a little example using the jar commmand straight from the command-line (and forget Ant or IDE approaches for the sake of simplicity).
Assume the application you want to package consists of a single class nl.jekl.javaranch.MyApp that has a compile- and run-time dependency on the Oracle JDBC driver contained in a sperarate .jar file ojdbc5.jar. Let's also skip compilation and move straight on to packaging.
The Manifest.txt file would contain the following two lines, each terminated by a new line (IMPORTANT!):
Main-Class: nl.jekl.javaranch.MyApp
Class-Path: lib/ojdbc5.jar
You can package this application by executing the following command from the project-root folder:
jar cfm MyJar.jar ./etc/Manifest.txt -C classes .
This will create a new .jar file called MyJar.jar in the project root folder containing the following structure and files:
META-INF\MANIFEST.MF
nl/jekl/javaranch/MyApp.class
The MANIFEST.MF file will contain the standard manifest entries plus the ones you've specified in the Manifest.txt file:
Manifest-Version: 1.0
Class-Path: lib/log4j-1.2.15.jar
Created-By: 1.6.0_18 (Sun Microsystems Inc.)
Main-Class: nl.jekl.javaranch.MyApp
You can launch the application from the project-root folder using the following command:
java -jar MyJar.jar
The MANIFEST.MF file specifies the class contiaining the main method that should be used as the packaged application's entry point and class path entries need to run the application. Note that if you move the MyJAr.jar file out of the project-root directory and you want to launch it from a different location, that location should also contain a lib sub-directory which in turn should contain the ojdbc5.jar file. If you don't want to ship the 3rd party dependency .jar file in a lib sub-directory, you should strip the lib/ segment of the Class-Path entry in the Manifest.txt file and repackage. Now the java -jar command will look for the ojdbc5.jar file in the same location that the MyJaar.jar file is located. Right alongside of it.
There are of course many different ways to structure your project-and deployment layout's, but this was just an example. Also, it might be worth noting that you need not even use separate 3rd party .jar files and a Class-Path entry in the MANIFEST.MF file. You could also just extract 3rd party .jar files, and package all the individual .class files directly inside your own application .jar file, but that approach is best soon forgotten.