VOTG
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Java in General
 
RSS feed
 
New topic
Author

problem with classpath

Nitesh Sakargayan
Greenhorn

Joined: Jan 15, 2010
Messages: 5

Hi,

I have a little problem with java classpath.

I am working on unix and have already set the CLASSPATH in env.
But I am still getting the ClassNotFoundException

echo $CLASSPATH
/oracle/oracle_11g/product/11.1.0/db_1/jdbc/lib/ojdbc5.jar

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

Jelle Klap
Ranch Hand

Joined: Mar 10, 2008
Messages: 431

Don't (ab)use the CLASSPATH environment variable to set dependencies for your application.
If the application is packaged as a JAR file, use the JAR file's manifest to define these dependencies instead.
For more information on this subject, take a look here: http://java.sun.com/docs/books/tutorial/deployment/jar/index.html or more specifically at this section: http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html

This message was edited 1 time. Last update was at by Jelle Klap


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Maneesh Godbole
Bartender

Joined: Jul 26, 2007
Messages: 4244

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

work is the scourge of the drinking class
Campbell Ritchie
Bartender

Joined: Oct 13, 2005
Messages: 14987

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.
Jelle Klap
Ranch Hand

Joined: Mar 10, 2008
Messages: 431

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


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Campbell Ritchie
Bartender

Joined: Oct 13, 2005
Messages: 14987

I take your point, Jelle. I also think this is no longer a "beginning" question and I ought to move it.
Nitesh Sakargayan
Greenhorn

Joined: Jan 15, 2010
Messages: 5

Thanks for your replies.

All non-system classes should be a part of the application packaged inside the JAR file,

Jelle can you please explain some more about the point you raised in your reply.
How can I achieve this in present scenario.

--
Nitesh


Jelle Klap
Ranch Hand

Joined: Mar 10, 2008
Messages: 431

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.

Simplified partial project layout:

<project-root>/src/nl/jekl/javaranch/MyApp.java
<project-root>/classes/nl/jekl/javaranch/MyApp.class
<project-root>/lib/ojdbc5.jar
<project-root>/etc/Manifest.txt


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.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Nitesh Sakargayan
Greenhorn

Joined: Jan 15, 2010
Messages: 5

Thanks Jelle,

Your example worked.
I am using Class-Path: ojdbc5.jar in Manifest.txt and placed ojdbc5.jar in the same directory of MyApp.jar.
Thanks again.

-
Nitesh
 
jQuery in Action
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Java in General
 
RSS feed
 
New topic
replay challenge