Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

loading drivers

1 view
Skip to first unread message

Murat Tasan

unread,
Jul 14, 2003, 10:56:15 AM7/14/03
to
I'm having a very confusing problem with my application...
The problem is when I try to load JDBC drivers when my app is in a jar file.

Here is a test case:

public class Test
{
public static void main(String[] args) throws Exception
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("sweet!");
}
}

Now, my $CLASSPATH is set fine, verified by running the above and getting:

>java Test
sweet!
>

Okay, so now I'll jar this file up and add a manifest addition...
(for the main class)...

>jar -cmf MANIFEST.MF test.jar Test.class
>

now I try to run it:

>java -jar test.jar

and it starts to run (so manifest is fine), but gives me this:

Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:140)
at Test.main(Test.java:5)
>

And the $CLASSPATH has NOT changed at all while doing this.
So it worked when running from the .Class file, but not when running
from the .jar file

Any ideas? Am I just missing something here about .jar files?

Thanks much,

murat

--
Murat Tasan
mx...@po.cwru.edu
ta...@eecs.cwru.edu
murat...@cwru.edu
http://genomics.cwru.edu

Jon Skeet

unread,
Jul 14, 2003, 12:13:44 PM7/14/03
to
Murat Tasan <ta...@eecs.cwru.edu> wrote:
> And the $CLASSPATH has NOT changed at all while doing this.
> So it worked when running from the .Class file, but not when running
> from the .jar file

The classpath environment variable (and command line option) isn't used
when the -jar option is specified.

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Murat Tasan

unread,
Jul 14, 2003, 1:08:10 PM7/14/03
to
okay, well then, that sucks.

does anyone know then how to specify additional classpaths when using the
-jar option?

i assume the java sdk classes are automatically included (they have to),
and all of the classes in the executing jar file... but how do i then load
classes not in either of those two locations?

thanks for all the help!

murat

On Mon, 14 Jul 2003, Jon Skeet wrote:

> Murat Tasan <ta...@eecs.cwru.edu> wrote:
> > And the $CLASSPATH has NOT changed at all while doing this.
> > So it worked when running from the .Class file, but not when running
> > from the .jar file
>
> The classpath environment variable (and command line option) isn't used
> when the -jar option is specified.
>
>

--

Jon Skeet

unread,
Jul 15, 2003, 3:26:13 AM7/15/03
to
Murat Tasan <ta...@eecs.cwru.edu> wrote:
> okay, well then, that sucks.

No, it doesn't. It means that people can distribute jar files without
worrying about whether someone will have some other version of a
library they need on their classpath.



> does anyone know then how to specify additional classpaths when using the
> -jar option?

Either use the extensions mechanism, or specify the classpath in the
manifest.

> i assume the java sdk classes are automatically included (they have to),
> and all of the classes in the executing jar file... but how do i then load
> classes not in either of those two locations?

See above - or create a ClassLoader instance explicitly which knows how
to load the classes. In the case of a database, however, I believe it's
problematic if you end up with drivers being loaded by child
classloaders - hopefully someone here will have a solution for that if
it's an issue for you though.

Chris

unread,
Jul 22, 2003, 1:39:56 PM7/22/03
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Murat Tasan wrote:

[snip]

Hi,
I haven't actually verified this myself, but I read it on one of the
Java newsgroups a while ago: using "java -jar <something.jar>" makes
the JVM ignore the $CLASSPATH environment variable, replacing the
effective classpath with only the specified JAR file. In my personal
opinion, the best way to get around this is to put all the JARs with
the drivers and other classes, that would normally be in the
$CLASSPATH, into the jre/lib/ext directory (that way you really don't
need a $CLASSPATH and these problems are entirely avoided -- classes
in the ext directory are automatically in the classpath). If you
don't want to do this, I think (not sure) you can still get around
the problem by using the -cp flag to java, which is in turn NOT
ignored if the -jar flag is used (but, as I say, I'm not sure).

Good luck!

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/HXcRwxczzJRavJYRAlumAKDPLKZdboGCf1NIqM8k0o6i0SdFfQCfViAe
0bdCosVuU63TBe32RG+znEU=
=N1Gr
-----END PGP SIGNATURE-----

0 new messages