Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

Classpath question with JARs

Thread view: 
Larry Coon - 10 Jun 2005 15:55 GMT
Sorry to be asking such an asinine question, but I can't make my
app run after I package it in a jar file, when my app refers to
library classes in a different jar file.

Code snippet:

package connectTest;

import com.sybase.jdbc2.jdbc.*;

public class ConnectTest {
 public ConnectTest() {
   try {
     DriverManager.registerDriver((Driver)
       Class.forName("com.sybase.jdbc2.jdbc.SybDriver")
       .newInstance());
   // etc...

I can make this test app work fine when I run it from Eclipse.
But when I package ConnectTest up into a jar file, it loses the
ability to find com.sybase.jdbc2.jdbc.SybDriver.  SybDriver.class
is in the jar file jconn2.jar.  I've copied both ConnectTest.jar
and jconn2.jar to c:\ (and used various other locations).  I've
done it with various settings of the classpath env variable (and
without classpath being set at all).  I know that the classpath
needs to point to the jar file itself, and not just the directory
containing the jar file. Here's an example command:

java -cp c:\;c:\jconn2.jar -jar ConnectTest.jar

If my manifest doesn't contain a Class-Path entry, eg:

Manifest-Version: 1.0
Main-Class: connectTest.ConnectTest

then the result of the above command is always a stack trace with:

java.lang.ClassNotFoundException: com.sybase.jdbc2.jdbc.SybDriver

Every time I tried setting a Class-Path line in the manifest, e.g.:

Manifest-Version: 1.0
Class-Path: c:/jconn2.jar c:/
Main-Class: connectTest.ConnectTest

(and I've tried about a dozen variations on the Class-Path line)

I get:

Exception in thread "main" java.lang.NoClassDefFoundError:
connectTest/ConnectTest

What am I missing?
Larry Coon - 10 Jun 2005 18:15 GMT
> java -cp c:\;c:\jconn2.jar -jar ConnectTest.jar
> [. . .]
[quoted text clipped - 3 lines]
> Class-Path: c:/jconn2.jar c:/
> Main-Class: connectTest.ConnectTest

Follow-up....I found a combination that works for this specific test
case.  If the two jar files are in the same directory, and my manifest
looks like:

Manifest-Version: 1.0
Class-Path: jconn2.jar
Main-Class: connectTest.ConnectTest

and I invoke my app with:

java -jar ConnectTest.jar -cp .

Then it works.  Now the follow-up question -- I physically moved the
jconn2.jar file to simplify the problem for this test case.  In real
life, this file will be in a different location -- in fact, a variable
location, resolved by an environment variable.  I want my manifest's
Class-Path line to be the equivalent of this (using Windows environment
variable specification syntax):

Class-Path: %JDBC_HOME%/classes/jconn2.jar

How does one do that?
Roland - 10 Jun 2005 19:04 GMT
>>java -cp c:\;c:\jconn2.jar -jar ConnectTest.jar
>>[. . .]
[quoted text clipped - 3 lines]
>>Class-Path: c:/jconn2.jar c:/
>>Main-Class: connectTest.ConnectTest

You can use a URL, so
 Class-Path: file:///c:/jconn2.jar file:///c:/path/to/some/other.jar
should work. But I don't know if a directory URL would work, i.e. like
 Class-Path: file:///c:/

> Follow-up....I found a combination that works for this specific test
> case.  If the two jar files are in the same directory, and my manifest
[quoted text clipped - 7 lines]
>
> java -jar ConnectTest.jar -cp .

All commandline parameters after the "-jar Your.jar" option will be
taken as program parameters. Your commandline would effectively pass two
arguments, "-cp" and ".", to the main program.

When you use the -jar option, other class path settings on the
commandline or in the CLASSPATH environment variable are ignored.
<http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html#options>

> Then it works.  Now the follow-up question -- I physically moved the
> jconn2.jar file to simplify the problem for this test case.  In real
[quoted text clipped - 6 lines]
>
> How does one do that?

AFAIK, this isn't possible. Though a environment dependent classpath
isn't possible in this way, you could use an absolute file://-URL (as
described above).
Signature

Regards,

Roland de Ruiter
` ___      ___
`/__/ w_/ /__/
/  \ /_/ /  \

Larry Coon - 10 Jun 2005 19:29 GMT
> All commandline parameters after the "-jar Your.jar" option will be
> taken as program parameters. Your commandline would effectively pass two
[quoted text clipped - 3 lines]
> commandline or in the CLASSPATH environment variable are ignored.
> <http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html#options>

Ah, thanks.  Turns out the -cp option was superfluous.  It ran
just fine with:

java -jar ConnectTest.jar

> AFAIK, this isn't possible. Though a environment dependent classpath
> isn't possible in this way, you could use an absolute file://-URL (as
> described above).

Seems odd to me that this wouldn't be a fundamental thing that's
done all the time -- install a 3rd party library (in this case,
Sybase's implementation of JDBC, which is implemented in jconn2.jar)
and deploy a Java app which requires it.  The natural thing to be
able to do (or so I thought) is install the library, point an env
variable so your app (or the JRE) can find it, and you're off &
running.  The env variable is necessary because the library might
be in different places on different clients.  Is what I'm trying to
do not as common as I think it is, or am I approaching it the wrong
way?
ninja java - 11 Jun 2005 18:07 GMT
You could look into using jnlp.  It has many capabilities for
referencing other jar file libraries.
ninja java - 11 Jun 2005 18:02 GMT
Actually you can dynamically set the classpath.  Just edit the class
path system property.

System.setProperty("java.class.path",
System.getProperty("java.class.path") + ";" + newClasspath);

this can work with jar files or directories.

I hope this helped.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.