You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Shimon Crown <sh...@equipnet.co.il> on 2002/08/08 07:27:49 UTC

REPOST (+more comments) - Classloading problems with JNI

I asked this question a couple of days ago but only got 1 reply which
unfortunately didn't solve the problem. Hence the repost.

Tomcat 4.0.  J2SDK1.4 (Windows NT4).

I have a couple of jars that use JNI. Originally I put these jars into the
applications web-inf directory. Presumably these classes were only loaded
once as everything went okay as far as calling from java to C++ code goes.
The native code performs a callback into classes defined in these jars and
has to create java classes defined by me. Unfortunately the native dll is
unable to locate these classes and I fail in the native line

     jclass variantClass = env->FindClass("MYCLASS");

 The same thing happens when I put the JNI jars in %CATALINA_HOME%/lib which
I believe is the correct place for them.

I have tried other directories including  %CATALINA_HOME%\server\lib. When
using these other directories it seems that the JNI classes are loaded twice
as Tomcat just hangs and fails to initialize the servlets that I have setup
to load at startup.

 I tried the same thing with jetty web server and found that JNI is unable
to find classes in the web-inf/lib directory. However if I included these
JNI containing jars as part of the classpath that I gave when running the
Jetty server then it could find them.

As an interim solution I have found that I can split the problem causing
jars into Java->C++ calling and other classes. The other classes which are
called from C++ can safely be put in a common CATALINA directory where the
classloader used by JNI will find them.

Can anyone provide me with a definitive explanation of the classloading
behaviour I am seeing. Is there a way to solve this problem without breaking
up the jars ?

Shimon Crown




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: REPOST (+more comments) - Classloading problems with JNI

Posted by Shimon Crown <sh...@equipnet.co.il>.
Thanks Michael while this is not the solution it does give me some clues as
to where to look.

Looking at my over two year old JNI code I see that I do pass a reference to
the JVM as initialization parameter to the C++ code.

In order to find the classes for the callback I do the following

 JNIEnv* env;
 m_jvm->GetEnv(reinterpret_cast<void**>(&env),JNI_VERSION_1_2); // m_jvm is
the cached JVM

Clearly the Java environment I am getting is not the one I thought it was.
And I thought DLL hell was a problem !

Shimon


> I've done no work with JNI, but have read a little on it.
>
> My understanding is that when making calls from native
> to java, the native code often instantiates a new JVM. If
> your are doing this then I would suspect that this is your problem.
>
> Your newly instantied JVM would not have access to the classes
> loaded in the Tomcat JVM instance. Even if you included these in
> your CLASSPATH (as you mentioned with Jetty) then you'd be
> accessing java classes in a JVM seperate from the one that Tomcat
> is using (which is not what I think you want to do).
>
> It seems that you'd somehow need to, first make a java->C++ call
> to your native code to give it a handle to the Tomcat JVM instance,
> and from there use this handle in your native code to access or invoke
> the java classes/objects that you so desire.
>
> Michael LeValle
>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: REPOST (+more comments) - Classloading problems with JNI

Posted by Michael LeValle <to...@levalle.net>.
Hi Shimon,

I've done no work with JNI, but have read a little on it.

My understanding is that when making calls from native
to java, the native code often instantiates a new JVM. If
your are doing this then I would suspect that this is your problem.

Your newly instantied JVM would not have access to the classes
loaded in the Tomcat JVM instance. Even if you included these in
your CLASSPATH (as you mentioned with Jetty) then you'd be
accessing java classes in a JVM seperate from the one that Tomcat
is using (which is not what I think you want to do).

It seems that you'd somehow need to, first make a java->C++ call
to your native code to give it a handle to the Tomcat JVM instance,
and from there use this handle in your native code to access or invoke
the java classes/objects that you so desire.

Michael LeValle


Shimon Crown wrote:

>I asked this question a couple of days ago but only got 1 reply which
>unfortunately didn't solve the problem. Hence the repost.
>
>Tomcat 4.0.  J2SDK1.4 (Windows NT4).
>
>I have a couple of jars that use JNI. Originally I put these jars into the
>applications web-inf directory. Presumably these classes were only loaded
>once as everything went okay as far as calling from java to C++ code goes.
>The native code performs a callback into classes defined in these jars and
>has to create java classes defined by me. Unfortunately the native dll is
>unable to locate these classes and I fail in the native line
>
>     jclass variantClass = env->FindClass("MYCLASS");
>
> The same thing happens when I put the JNI jars in %CATALINA_HOME%/lib which
>I believe is the correct place for them.
>
>I have tried other directories including  %CATALINA_HOME%\server\lib. When
>using these other directories it seems that the JNI classes are loaded twice
>as Tomcat just hangs and fails to initialize the servlets that I have setup
>to load at startup.
>
> I tried the same thing with jetty web server and found that JNI is unable
>to find classes in the web-inf/lib directory. However if I included these
>JNI containing jars as part of the classpath that I gave when running the
>Jetty server then it could find them.
>
>As an interim solution I have found that I can split the problem causing
>jars into Java->C++ calling and other classes. The other classes which are
>called from C++ can safely be put in a common CATALINA directory where the
>classloader used by JNI will find them.
>
>Can anyone provide me with a definitive explanation of the classloading
>behaviour I am seeing. Is there a way to solve this problem without breaking
>up the jars ?
>
>Shimon Crown
>
>
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
>
>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: REPOST (+more comments) - Classloading problems with JNI

Posted by Shimon Crown <sh...@equipnet.co.il>.
Hi Andreas,

Thanks but I was just being careless and wrote WEB-INF in the wrong case in
the post. Of course in the actual Tomcat hierarchy it is WEB-INF.
In any case I don't actually ever refer to WEB-INF in my code as it is
implicit that Tomcat can find classes there.

Curiously Windows Explorer "corrects" names like WEB-INF to Web-inf in its
displays so I have an excuse for my carelessness.

Shimon

----- Original Message -----
From: Andreas Probst <an...@gmx.net>
To: Shimon Crown <sh...@equipnet.co.il>; Tomcat Users List
<to...@jakarta.apache.org>
Sent: Thursday, August 08, 2002 10:37 AM
Subject: Re: REPOST (+more comments) - Classloading problems with JNI


> Hi Shimon,
>
> I don't know about JNI, but maybe it only has to do with the spelling of
WEB-INF. It
> must be written in upper case.
>
> Andreas
>
>
> > I asked this question a couple of days ago but only got 1 reply which
> > unfortunately didn't solve the problem. Hence the repost.
> >
> > Tomcat 4.0.  J2SDK1.4 (Windows NT4).
> >
> > I have a couple of jars that use JNI. Originally I put these jars into
the
> > applications web-inf directory. Presumably these classes were only
loaded
> > once as everything went okay as far as calling from java to C++ code
goes.
> > The native code performs a callback into classes defined in these jars
and
> > has to create java classes defined by me. Unfortunately the native dll
is
> > unable to locate these classes and I fail in the native line
> >
> >      jclass variantClass = env->FindClass("MYCLASS");
> >
> >  The same thing happens when I put the JNI jars in %CATALINA_HOME%/lib
which
> > I believe is the correct place for them.
> >
> > I have tried other directories including  %CATALINA_HOME%\server\lib.
When
> > using these other directories it seems that the JNI classes are loaded
twice
> > as Tomcat just hangs and fails to initialize the servlets that I have
setup
> > to load at startup.
> >
> >  I tried the same thing with jetty web server and found that JNI is
unable
> > to find classes in the web-inf/lib directory. However if I included
these
> > JNI containing jars as part of the classpath that I gave when running
the
> > Jetty server then it could find them.
> >
> > As an interim solution I have found that I can split the problem causing
> > jars into Java->C++ calling and other classes. The other classes which
are
> > called from C++ can safely be put in a common CATALINA directory where
the
> > classloader used by JNI will find them.
> >
> > Can anyone provide me with a definitive explanation of the classloading
> > behaviour I am seeing. Is there a way to solve this problem without
breaking
> > up the jars ?
> >
> > Shimon Crown
> >
> >
> >
> >
> > --
> > To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> > For additional commands, e-mail:
<ma...@jakarta.apache.org>
> >
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: REPOST (+more comments) - Classloading problems with JNI

Posted by Andreas Probst <an...@gmx.net>.
Hi Shimon,

I don't know about JNI, but maybe it only has to do with the spelling of WEB-INF. It 
must be written in upper case.

Andreas


> I asked this question a couple of days ago but only got 1 reply which
> unfortunately didn't solve the problem. Hence the repost.
> 
> Tomcat 4.0.  J2SDK1.4 (Windows NT4).
> 
> I have a couple of jars that use JNI. Originally I put these jars into the
> applications web-inf directory. Presumably these classes were only loaded
> once as everything went okay as far as calling from java to C++ code goes.
> The native code performs a callback into classes defined in these jars and
> has to create java classes defined by me. Unfortunately the native dll is
> unable to locate these classes and I fail in the native line
> 
>      jclass variantClass = env->FindClass("MYCLASS");
> 
>  The same thing happens when I put the JNI jars in %CATALINA_HOME%/lib which
> I believe is the correct place for them.
> 
> I have tried other directories including  %CATALINA_HOME%\server\lib. When
> using these other directories it seems that the JNI classes are loaded twice
> as Tomcat just hangs and fails to initialize the servlets that I have setup
> to load at startup.
> 
>  I tried the same thing with jetty web server and found that JNI is unable
> to find classes in the web-inf/lib directory. However if I included these
> JNI containing jars as part of the classpath that I gave when running the
> Jetty server then it could find them.
> 
> As an interim solution I have found that I can split the problem causing
> jars into Java->C++ calling and other classes. The other classes which are
> called from C++ can safely be put in a common CATALINA directory where the
> classloader used by JNI will find them.
> 
> Can anyone provide me with a definitive explanation of the classloading
> behaviour I am seeing. Is there a way to solve this problem without breaking
> up the jars ?
> 
> Shimon Crown
> 
> 
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>