You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Chris Picton <to...@tangent.co.za> on 2004/09/04 15:08:36 UTC
URLClassLoader problems
Hi
I am trying to use the following code to load a class from a jar file
which is outside of my webapp directory. The classes are loaded on
demand and dynamically.
...
public class JarClassLoader extends URLClassLoader {
public JarClassLoader(File jar) throws MalformedURLException {
super(new URL[] { jar.toURL() });
}
public Object instantiate(String className) throws
ClassNotFoundException, InstantiationException, IllegalAccessException {
System.out.println("Looking for class: " + className);
Class c = findClass(className);
return c.newInstance();
}
}
...
JarClassLoader loader = new JarClassLoader(new File(jarPath));
Object o = loader.instantiate(myClassName);
...
If I run the code from an app, it loads without any problems. If I use
it within a tomcat Servlet, the 'instantiate' call throws a
java.lang.NoClassDefFoundError.
I have checked that the tomcat user can access the jar file, so it does
not seem to be a permission problem.
Does the tomcat container limit access to classes outside of its
classpath? Is there something simple I am missing?
Regards
Chris
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: URLClassLoader problems
Posted by John Gentilin <jo...@gentilin.org>.
I use the URLClassloader with out problem, unless Tomcat was started
with the -security option, then I get
security failures. I have been using 4.1.30 and earlier. Could it be a
relative directory thing, have you tried to
open the jar in your constructor to make sure you have access to the file ??
HTH
John G
Chris Picton wrote:
>Hi
>
>I am trying to use the following code to load a class from a jar file
>which is outside of my webapp directory. The classes are loaded on
>demand and dynamically.
>
>...
>public class JarClassLoader extends URLClassLoader {
>
> public JarClassLoader(File jar) throws MalformedURLException {
> super(new URL[] { jar.toURL() });
> }
>
> public Object instantiate(String className) throws
>ClassNotFoundException, InstantiationException, IllegalAccessException {
> System.out.println("Looking for class: " + className);
> Class c = findClass(className);
> return c.newInstance();
> }
>}
>...
>JarClassLoader loader = new JarClassLoader(new File(jarPath));
>Object o = loader.instantiate(myClassName);
>...
>
>
>If I run the code from an app, it loads without any problems. If I use
>it within a tomcat Servlet, the 'instantiate' call throws a
>java.lang.NoClassDefFoundError.
>
>I have checked that the tomcat user can access the jar file, so it does
>not seem to be a permission problem.
>
>Does the tomcat container limit access to classes outside of its
>classpath? Is there something simple I am missing?
>
>Regards
>Chris
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org