NoClassDefFoundError in custom ClassLoader

I've written a ClassLoader class which performs classloading via a serial
communications port using our own propriety protocol.
The ClassLoader works well and dynamically loads classes as expected.
However, the server software can tell the ClassLoader via the serial port to
instantiate a class, the ClassLoader itself then calls a method such as:
Class.forName("testClass") which then causes the java runtime system to try
to load the "testClass" class through the same ClassLoader. This all works
fine until the ClassLoader tries to define the class
(ClassLoader.defineClass()) in which the NoClassDefFoundError exception is
thrown.
I'm not sure how this is possible since I'm trying to define the class and
it tell's me at the same time that the class definition could not be found.
What am I doing wrong?
Best Regards,
Don Schoeman

It is probably because the byte array containing the class data is not complete or corrupted.
Robin.

Similar Messages

  • The struggle of creating a Custom ClassLoader for Native libraries

    Hello Everyone,
    I'm having a really hard time writing and using my own ClassLoader in a Java Applet.
    Context :
    As the this link shows - http://codethesis.com/tutorial.php?id=1 - loading and especially unloading native libraries through Java requires defining our own ClassLoader, and use it to instantiate a class loading a library. When the class using native libraries has finished execution, setting all references to the classloader and calling the garbage collector will cause the native library to be unloaded. The class to load within the custom classloader is thus read byte after byte from the jar and defined using the Classloader.defineClass(..) function. So that's what I did. But I've got two problems.
    Problem 1 :
    On one single machine over 15 tested, the magic number of a given class read from the Jar using Applet.class.getResourceAsStream(classname) takes a value different from CAFEBABE and the defineClass function then throws an "Incompatible magic value" exception (see below). The workaround I found is to force the first 4 bytes of the byte array read from the class with CAFEBABE. But I still would like to understand why it takes a different value on this machine.
    Exception in thread "thread applet-MyApplet.class-1" java.lang.ClassFormatError: Incompatible magic value 409165630 in class file Reader
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at NativeClassLoader.findClass(Unknown Source)
    Problem 2 :
    On windows, the NativeClassLoader works perfectly, but on Linux, I'm getting a java.lang.VerifyError (see below).
    Code is compiled with java 1.6.0_06 on windows XP. I tried to remove everything related to native code (remove .so load), the same error is raised.
    java.lang.VerifyError: (class: Reader, method: <clinit> signature: ()V) Illegal instruction found at offset 1
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
    at java.lang.Class.getConstructor0(Class.java:2699)
    at java.lang.Class.newInstance0(Class.java:326)
    at java.lang.Class.newInstance(Class.java:308)
    Code :
    NativeClassReader (custom) :
    public class NativeClassLoader extends ClassLoader {
        //the unique instance of the NativeClassLoader
        private static NativeClassLoader instance;
        private NativeClassLoader () {
            super(NativeClassLoader.class.getClassLoader());
         * Get the Singleton instance of the class
        public static NativeClassLoader getInstance () {
            if (instance == null)
                instance = new NativeClassLoader();
            return instance;
        public static void dispose () {
            instance = null;
         * Load a class using its full java name (prefixed with package)
        public Class findClass (String theName) {
            byte[] b = null;
            try {
                b = loadClassDataFromJar(theName);
                Class clazz = defineClass(theName, b, 0, b.length);
                resolveClass(clazz);
                return clazz;
            } catch (Exception e) {
                return null;
         * Gets the bytes of a class file stored in the current jar using
         * its full class name
        public byte[] loadClassDataFromJar (String theName)
                                     throws Exception {
            String filename = "/" + theName.replace('.', '/') + ".class";
            InputStream is = SawsApplet.class.getResourceAsStream(filename);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            //compute file size
            Vector vChars = new Vector();
            int c;
            while ((c = br.read()) != -1)
                vChars.add(new Byte((byte) c));
            //fill in byte array with chars read from the buffer
            byte[] buff = new byte[vChars.size()];
            //workaround for a bug on one (some) Vista machine(s)
            //force magic number to CAFEBABE instead of 18635F3E
            if (vChars.size() > 3) {
                buff[0] = (byte) 0xCA;
                buff[1] = (byte) 0xFE;
                buff[2] = (byte) 0xBA;
                buff[3] = (byte) 0xBE;
            for (int i = 4; i < vChars.size(); ++i)
                buff[i] = ((Byte) vChars.get(i)).byteValue();
            return buff;
    }Reader (loading native libary) :
    public class Reader {
       static {
         System.loadLibrary("myLib");
        public static native String getData();
    }Main :
        NativeClassLoader cLoader = NativeClassLoader.getInstance();
        Class clazz = cLoader.findClass("Reader"); // ClassFormatError thrown here
        Object reader = clazz.newInstance(); // VerifyError thrown here
        Method m = clazz.getMethod("getData");
        String s = m.invoke(reader);
        print(s);
        s = null;
        m = null;
        reader = null;
        clazz = null;
        cLoader = null;
        NativeClassLoader.dispose();
        System.gcAny ideas would be really appreciated :-)
    Guillaume

    Are you using the executable exe file and the filename as a parameter in the custom task?
    Andreas Baumgarten | H&D International Group

  • XMLEncoder stackoverflow problem with custom classloader

    Hi, All
    I'm tring encoding my object using XMLEncoder, the objects are from the custom classloader like URLClassLoader which is importing several jar files at run-time.
    source code is like this:
    XMLEncoder e = new XMLEncoder(new BufferedOutputStream(os));
    e.writeObject( sourceObj);
    When I run it, I experienced this error:
    XMLEncoder has "ClassNotFoundException" or "InstantiationException" problem.
    so I found very similar posting for this problem and modified it so that the XMLEncoder can find my classes from custom classloader:
    ClassLoader cl = this.getClass().getClassLoader();
    URLClassLoader urlClassLoader = new URLClassLoader(urls, cl);
    Thread.currentThread().setContextClassLoader(urlClassLoader);
    XMLEncoder e = new XMLEncoder(new BufferedOutputStream(os));
    e.writeObject( sourceObj);
    The result was slightly changed. However, still disappointing:
    java.lang.StackOverflowError
         at java.lang.Class.getMethod0(Class.java:1734)
         at java.lang.Class.getMethod(Class.java:951)
         at java.beans.Statement.findPublicMethod(Statement.java:230)
         at java.beans.Statement.findMethod(Statement.java:270)
         at java.beans.Statement.getMethod(Statement.java:366)
         at java.beans.Statement.invoke(Statement.java:439)
         at java.beans.Expression.getValue(Expression.java:101)
         at java.beans.Encoder.getValue(Encoder.java:84)
         at java.beans.Encoder.get(Encoder.java:186)
         at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:97)
         at java.beans.Encoder.writeObject(Encoder.java:55)
         at java.beans.XMLEncoder.writeObject(XMLEncoder.java:250)
         at java.beans.Encoder.writeExpression(Encoder.java:260)
         at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:351)
         at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
         at java.beans.Encoder.writeObject(Encoder.java:55)
         at java.beans.XMLEncoder.writeObject(XMLEncoder.java:250)
         at java.beans.Encoder.writeExpression(Encoder.java:260)
         at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:351)
         at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
         at java.beans.Encoder.writeObject(Encoder.java:55)
         at java.beans.XMLEncoder.writeObject(XMLEncoder.java:250)
         at java.beans.Encoder.writeExpression(Encoder.java:260)
         at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:351)
         at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
         at java.beans.Encoder.writeObject(Encoder.java:55)
    When I finally include the jar files into the classpath so that the default classloader can reference them, the encoder worked properly.
    Anyone can help me?

    StackOverFlow is quite a gotcha in XMLEncoder it is often because of the following code in java.beans.PersistenceDelegate:
    public void writeObject(Object oldInstance, Encoder out) {
    Object newInstance = out.get(oldInstance);
    if (!mutatesTo(oldInstance, newInstance)) {
    out.remove(oldInstance);
    out.writeExpression(instantiate(oldInstance, out));
    else {
    initialize(oldInstance.getClass(), oldInstance, newInstance, out);
    and the following code in java.beans.DefaultPersistenceDelegate:
    protected boolean mutatesTo(Object oldInstance, Object newInstance) {
    // Assume the instance is either mutable or a singleton
    // if it has a nullary constructor.
    return (constructor.length == 0) || !definesEquals(oldInstance) ?
    super.mutatesTo(oldInstance, newInstance) :
    oldInstance.equals(newInstance);
    What this means is if you have a class (you want to persist) that has a constructor that takes arguments AND defines an equals method AND you have overridden initialize in DefaultPersistenceDelegate to do some extra work after it instantiates newInstance, then while the PersistenceDelegate is trying to generate you newInstance, oldInstance.equals(newInstance) will return false, and PersistenceDelegate will keep trying to instantiate a newInstance that does equal the oldInstance.
    So if you do use DefaultPersistenceDelegate then ensure that you override mutatesTo so that it is:
    protected boolean mutatesTo(Object oldInstance, Object newInstance) {
    return (newInstance != null && oldInstance.getClass() == newInstance.getClass());
    Here is an example PersistenceDelegate that persistence a class called PersistentBeanCollection, it calls its constructor with the property "type" and then calls the method add() on the newInstance.
    new DefaultPersistenceDelegate(new String[]{"type"}) {
    protected void initialize(Class type, Object oldInstance, Object newInstance, Encoder out) {
    PersistentBeanCollection oldBeanCollection = (PersistentBeanCollection) oldInstance;
    for (Iterator iterator = oldBeanCollection.iterator(); iterator.hasNext();) {
    out.writeStatement(new Statement(oldInstance, "add", new Object[]{iterator.next()}));
    protected boolean mutatesTo(Object oldInstance, Object newInstance) {
    return (newInstance != null && oldInstance.getClass() == newInstance.getClass());
    Cheers
    Parwy

  • Best way to call custom classloader

    I have created a custom classloader to perform hot deployment for application server. How do I let JVM to use my class loader instead of system class loader?
    a. Using the command line argument -Djava.system.class.loader
    b. Using -javaagent and setting System.setProperty("java.system.class.loader", myclass) in premain method.
    c. Is there any other alternative?
    If I am using the command line approach, I will need to have my jar file in the classpath. What is the best way to do this?
    Thanks for your help

    Or you could write a small program which sets up the classloader and then loads the target program with it.

  • Customer ClassLoader:  keeping JWS from loading jars

    I really like the ability of JWS to be able to download jars required for my application to run, but there are several jars that I need the ability to load up via a custom ClassLoader. But because JWS includes all of these jars in the classpath upon application start, the system class loader will take the first class it can find. Because I need to load classes from these jars based upon something a user selects, I need to have control over the codesource for these classes. Does anyone have any ideas on how to tackle this problem? Not include the jars in the jnlp and then download if not cached locally, if that's the case what's the best way to do that? Include in jnlp and be able to load with custom classloader(I dont think this will work)? Any ideas on this would greatly be appreciated.

    Easiest solution to your problem and still being able to use the update mechanism of JWS is to include your JAR-file in another JAR-file that JWS loads, and then write up your own classloader, which tries to fetch your JAR-file from the downloaded JAR-file (if JWS already downloaded it, otherwise JWS will download it).
    The contents of the jws-stub.jar would be something
    of the following ;).
    jws-stub.jar
    --> myjar1.jar
    --> myjar2.jar
    org.myjar.MyJarClassloader
    Then based on your user selecting something you
    can decide which JAR-file you want to load, and thus
    which class.
    Hope this helped,
    Manfred.

  • Custom ClassLoader - trying to use different version of SNMP library than WebLogic Server 8.1 uses

    Problem: my J2EE ear file uses the AdventNet third-party library to do
    SNMP work. WebLogic Server 8.1 also uses this AdventNet third-party
    library to do its own SNMP work. The problem is the version used by
    WebLogic 8.1 is older than the version I use, so my code tries to run
    and finds the wrong version of the library that WebLogic 8.1 has supplied.
    Possible solution: I plan to have a custom classloader (derived from
    java.lang.ClassLoader) that gets hooked in to my threads using
    Thread.setContextClassLoader(). This custom classloader would look for
    the AdventNet library jars in a spot I specify so it would find the
    correct ones. Since I want to change the usual classloader behavior of
    "look in parent classloaders first, then child classloader" to "look in
    child classloader first, then in parent classloaders" I can't just
    override ClassLoader.findClass(). I, at a minimum, have to override
    ClassLoader.loadClass() to look in my classloader first. I have
    questions about this:
    1. What other methods do I have to override? For example, which of the
    resource-related methods do I need to override so that resources are
    searched for first in my classloader then in parent classloaders?
    2. I was thinking of using a URLClassLoader as a helper to my
    classloader, invoking its method(s) from my custom classloader's
    method(s) to actually load the classes from URLs that are not on the
    standard classpath. I was planning to set it up with a custom parent
    classloader that can't find anything, so that the helper URLClassLoader
    would only ever find classes/resources in the URLs I provide to it. Does
    this approach make sense? Have you seen anything like this done before?
    3. Is there any way around this problem besides a custom ClassLoader? A
    buggy custom ClassLoader would have problems which AFAIK would be
    difficult to track down as ClassLoader problems.
    Thanks in advance for any help you can provide.

    Alvin wrote:
    Hi,
    I am experiencing the same problme and
    even I tried to put the AdventNet jar files
    before weblogic.jar I still cannot get it
    to work
    Would you help me out here.
    Thanks,
    -AlvinI finally figured out a way to do it using a custom classloader that
    looked in the directory where I kept the version of AdventNet I wanted
    before looking in the normal places classloaders look.
    How it works is I hook two classloaders at the bottom of the chain of
    classloaders. My custom BlockingClassLoader is hooked as a child of the
    normal chain of classloaders, then a URLClassLoader is hooked under
    that. The BlockingClassLoader's findClass method checks if the class
    starts with a package prefix I want to control (like "com.adventnet.").
    If it does, then it acts like it can't find the class. This makes its
    child, the URLClassLoader, try to find the class. The URLClassLoader
    is given the URL of places to look for classes that match the directory
    I keep the version of AdventNet I want in. When I load a class, I
    directly tell the URLClassLoader to load it so that it is loaded as I want.
    There's more to it than this. I had to override some other methods in
    my BlockingClassLoader and do some other stuff. I'm not sure if I can
    share the source code, as it was developed on company time and thus is
    owned by the company. Feel free to ask questions though.
    I haven't tried this solution very long (basically just unit tested it)
    but it looks promising.

  • Custom ClassLoader

    I do not understand much how to create a simple custom class loader. I found many explaination about it. I don't really get what it means.
    Can anyone explain to me in simple code? My custom classloader doesn't work at all.
    I want to load a class in another package. How can I do it?
    Thanks

    What do you mean by "package"?

  • Custom classloader in Applet?

    Hi
    I have for some time tried to load an Applet from within an Applet by means of custom classloader.
    However when reading various specifications for classloader they state it is not possible for an Applet to do so.
    Does anyone know otherwise?

    Hi
    To be more specific.. I have 2 Applets yes. In the first signed Applet I run as a jar I have created a custom classloader that extends ClassLoader hence the java bytecode I wish to load (also an Applet) must be (a) class file(s). As far as I know the ClassLoader is not able to define classes from jars right?
    It is only the loading Applet that is packed as a jar and there is no nested jars. However the loaded Applet is now loaded as a class file but I would like to load is a jar for speeding up loading time.
    Quote from http://www.javaworld.com/javaworld/jw-10-1996/jw-10-indepth.html:
    There is a cost, however, because the class loader is so powerful (for example, it can replace java.lang.Object with its own version), Java classes like applets are not allowed to instantiate their own loaders. (This is enforced by the class loader, by the way.) This column will not be useful if you are trying to do this stuff with an applet, only with an application running from the trusted class repository (such as local files).

  • Customized ClassLoader

    Can anyone tell me how to write a customized Classloader which can create an instance of any class other than the one stated in the code.
    like, in
    Socket s = new Socket ();
    rather than creating an instance of Socket class, if I want to create instance of any other class, how can I do that?
    -Rohit

    Can anyone tell me how to write a customized
    Classloader which can create an instance of any class
    other than the one stated in the code.Sorry, this can't be done just by writing your own class loader. You would need to tweak the JVM or bytecode.
    It also depends on what you mean by "any other class". If you want to load a modified version of the same class, then along the bytecode route, AspectJ might be able to do the job. There may be other bytecode post-processors that can help as well.
    Exactly what class do you plan on substituting? If you don't know already, any program will fail miserably if the classes loaded behave substantially differently than what was expected.

  • Implementing a custom ClassLoader

    Hi. I have found plenty of tutorials online about how to create your own custom ClassLoader but how do you actually go about indicating to the JVM that it should be using a custom ClassLoader for a given application? I can't figure it out.

    It's quite interesting. Often I find that inquiries on ClassLoader are related to the poster's misunderstanding something, but yes, sometimes interesting things come up.
    I want
    to contrive some sort of mechanism for embedding java
    code or some custom templating language in HTML. Hmm, like JSP?
    I want to avoid having to recompile my whole
    application each time i or someone else updates an
    HTML template. It seems that a custom classloader is
    the answer here where i could write a custom loader
    that would detect recently modified HTML templates
    and re-compile their associated .class file on the
    fly before re-loading the class.I wrote one of these along time ago. Its a bit tricky, but its been done.
    Why reinvent the wheel? I believe you could find one by doing a google for
    DynamicClassLoader.
    Take a look at janino, too.
    Good luck.

  • 5.1 SP8 Custom ClassLoader ClassFormatError messages

    We use a custom ClassLoader that unscrambles classfiles not understood by its parent.
    When the Weblogic parent ClassLoader attempts discovers it can't decipher the
    file, it writes a ClassFormatError message and stack trace to the console (though
    not to the log) before delegating to its child (our code) which proceeds to load
    the class with no trouble. The result is that our product runs fine, but appears
    to be full of unloadable classes. How can we get the Weblogic ClassLoader to suppress
    these misleading error messages? (IMHO, it shouldn't report errors until and unless
    both it and its child have failed, but given that it does, is there some way to
    make it shut up?)

    You can use class preprocessor in WebLogic for that sort of thing (products
    like Wily and Tangosol use this feature to instrument or customize classes when
    they are loaded by WebLogic classloaders) without adding your own classloaders,
    for example, in 6.1:
    public class ExamplePreprocessor implements weblogic.utils.classloaders.ClassPreProcessor {
    public void initialize(java.util.Hashtable ht) {}
    public byte[] preProcess(String classname, byte[] classbytes) {
         ... unscramble or do whatever is that you do with classbytes ...
    return classbytes;
    and add this to the WebLogic command line :
    -Dweblogic.classloader.preprocessor=preprocessorclassname
    Jonathan Shultis <[email protected]> wrote:
    We use a custom ClassLoader that unscrambles classfiles not understood by its parent.
    When the Weblogic parent ClassLoader attempts discovers it can't decipher the
    file, it writes a ClassFormatError message and stack trace to the console (though
    not to the log) before delegating to its child (our code) which proceeds to load
    the class with no trouble. The result is that our product runs fine, but appears
    to be full of unloadable classes. How can we get the Weblogic ClassLoader to suppress
    these misleading error messages? (IMHO, it shouldn't report errors until and unless
    both it and its child have failed, but given that it does, is there some way to
    make it shut up?)--
    Dimitri

  • How to load an applet using custom classloader ?

    Hi All,
    How to load an applet using custom classloader rather than using default browsers classloader i.e AppletClassLoader usually ?
    Regards,
    Kumar.

    I would guess that that would require two applets.
    The first does nothing but create the custom class loader and then load the second applet.

  • Loading EAR or WAR using a Custom Classloader

    Hi, everybody.
    First of all I'm aware of http://download.oracle.com/docs/cd/E12840_01/wls/docs103/programming/classloading.html
    Is is possible to "insert" a custom classloader in this hierarchy? I need a custom classloader to load special resources for my webapp. This custom classloader can be added right after the system classloader, acting as the parent classloader of my app (ear or war) classloader. Is it doable? How?
    Thanks in advance.
    Best regards,
    Daniel.

    My workaround won't work. The app classloader is accessible but it isn't an URLClassLoader, so i am unable to change its classpath.
    <16/05/2011 18h01min11s BRT> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to STANDBY>
    <16/05/2011 18h01min11s BRT> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to STARTING>
    preStart
    thread cl = weblogic.utils.classloaders.GenericClassLoader@1d5176d finder: weblogic.utils.classloaders.CodeGenClassFinder@330301 annotation: app@
    this cl = weblogic.utils.classloaders.GenericClassLoader@1d5176d finder: weblogic.utils.classloaders.CodeGenClassFinder@330301 annotation: app@
    thread cl instanceof URLClassLoader = false
    this cl instanceof URLClassLoader = false
    === Starting app ===
    sysCl = sun.misc.Launcher$AppClassLoader@13f5d07
    threadCl = weblogic.utils.classloaders.ChangeAwareClassLoader@17df9ec finder: weblogic.utils.classloaders.CodeGenClassFinder@af0e38 annotation: app@test_war
    sysCl = weblogic.utils.classloaders.ChangeAwareClassLoader@17df9ec finder: weblogic.utils.classloaders.CodeGenClassFinder@af0e38 annotation: app@test_war -> weblogic.utils.classloaders.GenericClassLoader@1d5176d finder: weblogic.utils.classloaders.CodeGenClassFinder@330301 annotation: app@ -> weblogic.utils.classloaders.FilteringClassLoader@2096d7 finder: weblogic.utils.classloaders.CodeGenClassFinder@18f12dc annotation: -> weblogic.utils.classloaders.GenericClassLoader@fbf51d finder: weblogic.utils.classloaders.CodeGenClassFinder@1f4b24 annotation: -> sun.misc.Launcher$AppClassLoader@13f5d07 -> sun.misc.Launcher$ExtClassLoader@f4a24a
    threadCl = weblogic.utils.classloaders.ChangeAwareClassLoader@17df9ec finder: weblogic.utils.classloaders.CodeGenClassFinder@af0e38 annotation: app@test_war ->weblogic.utils.classloaders.GenericClassLoader@1d5176d finder: weblogic.utils.classloaders.CodeGenClassFinder@330301 annotation: app@ -> weblogic.utils.classloaders.FilteringClassLoader@2096d7 finder: weblogic.utils.classloaders.CodeGenClassFinder@18f12dc annotation: -> weblogic.utils.classloaders.GenericClassLoader@fbf51d finder: weblogic.utils.classloaders.CodeGenClassFinder@1f4b24 annotation: -> sun.misc.Launcher$AppClassLoader@13f5d07 -> sun.misc.Launcher$ExtClassLoader@f4a24a
    postStart
    thread cl = weblogic.utils.classloaders.GenericClassLoader@1d5176d finder: weblogic.utils.classloaders.CodeGenClassFinder@330301 annotation: app@
    this cl = weblogic.utils.classloaders.GenericClassLoader@1d5176d finder: weblogic.utils.classloaders.CodeGenClassFinder@330301 annotation: app@
    thread cl instanceof URLClassLoader = false
    this cl instanceof URLClassLoader = false
    <16/05/2011 18h01min46s BRT> <Notice> <Log Management> <BEA-170027> <The Serverhas established connection with the Domain level Diagnostic Service successfully.>
    =====================================================================
    http://pastie.org/1912944
    package abc;
    import java.net.URLClassLoader;
    import weblogic.application.ApplicationException;
    import weblogic.application.ApplicationLifecycleListener;
    import weblogic.application.ApplicationLifecycleEvent;
    public class MyAppListener extends ApplicationLifecycleListener {
         @Override
         public void postStart(ApplicationLifecycleEvent evt)
                   throws ApplicationException {
              System.out.println("postStart");
              printClassLoaders();
         @Override
         public void preStart(ApplicationLifecycleEvent evt)
                   throws ApplicationException {
              System.out.println("preStart");
              printClassLoaders();
         protected void printClassLoaders() {
              ClassLoader[] cls = new ClassLoader[] {
                        Thread.currentThread().getContextClassLoader(),
                        getClass().getClassLoader() };
              System.out.println("thread cl = " + cls[0]);
              System.out.println("this cl = " + cls[1]);
              System.out.println("thread cl instanceof URLClassLoader = "
                        + (cls[0] instanceof URLClassLoader));
              System.out.println("this cl instanceof URLClassLoader = "
                        + (cls[1] instanceof URLClassLoader));
    }

  • Jar files not in WEB-INF/lib (custom ClassLoader?)

    I need to use classes in my JSP that do not come from a JAR file in the standard location. Sounds like I "simply" need to extend the ClassLoader used by the app server for loading the JSP. Is there any way of doing that?
    Example: my.jsp
    <h1>Hi</h1>
    <%
    FOO x = new FOO(); // code from a non-standard place
    ClassLoader cl = getClass().getClassLoader();
    %>
    I cannot put FOO.class into WEB-INF/lib or any of the standard places. Ideally I'd like to give the ClassLoader (cl above) a different (custom) parent ClassLoader which would know how to resolve FOO.class.
    I've poured over docs and Google and WebSphere and the Tomcat source code. Is this request of mine really so outrageous? I'm about the give up. What good are custom classloaders if one can't use them?

    You are essentially making the argument "All that Java
    needs is a URLClassLoader, and no one has any need for
    any custom ClassLoaders". (After all, that's all that
    I seem to have in a JSP context, with a list of
    hard-coded URLs into that URLClassLoader ...)
    Do a Google search on "custom ClassLoader" and you
    will find lots of reasons why people want to have
    custom ClassLoaders, such as ones that don't use any
    JAR or .class files ... which would qualify as why I
    can't put them into a certain place ...To be fair to duffymo, that is not how I read his argument. I read it as:
    "Sometimes we try to do things the hard way. If we sit back and rethink the problem, an easier solution might come about."
    I think this is a good opportunity to explain why you can not use the normal class loader heirarchy. You have no JARs and no .class files? Then where are you getting the class definitions? If you explain enough so that we might understand the problem:
    1) we might be able to come up with a solution or
    2) you might see a different approach before you are even done explaining
    #2 has happened to me more times then I can count. I sit down formulating a question for the forums, make sure I include enough detail to explain my problem. And by the time I am done I have a new (usually simpler) approach to solving the problem.
    Or you could sit and defend your first approach without providing any context and accomplish nothing. So its up to you.

  • Complie time using custom classloader for decryption.

    I need to decrypt several class during compile time, but my custom classloader could load everything except my encrypted classes.
    Here is my sample code and how I test it.
    Test.java:
    public class Test {
         public Test(){
              System.out.println("test");
         public void hello(){
              System.out.println("hello");
    Test1.java
    public class Test1 extends java.lang.Test{
         public Test1(){
              System.out.println("test1");
         public static void main(String[] args){
              new Test1().hello();
    #EncryptedClassLoader will print out the class name it trying to load, for example: "load class: java.lang.System"
    1. Compile Test.java file and have it encrypted, make it into a jar file - test.jar.
    2. Compile Test1.java file and try to run Test1.class with encrypted Test.class file using :
    C:\lib>java -Djava.system.class.loader=EncryptedClassLoader -classpath c:\lib;c:\lib\tools.jar;c:\lib\test.jar Test1
    Gives the output:
    load class: java.lang.System
    load class: java.nio.charset.Charset
    load class: java.lang.String
    load class: Test1
    load class: Test
    load class: sun.security.provider.Sun
    load class: sun.security.rsa.SunRsaSign
    load class: com.sun.net.ssl.internal.ssl.Provider
    load class: com.sun.crypto.provider.SunJCE
    load class: java.lang.Object
    load class: java.io.PrintStream
    test
    test1
    hello
    So now I'm sure there's no problem with my EncryptedClassLoader
    3. Try to compile Test1.java with the encrypted Test.class in test.jar(These's no Test.java file in the same folder) by using:
    C:\lib>javac -J-Djava.system.class.loader=EncryptedClassLoader -J-classpath -J.;c:\lib;c:\lib\tools.jar;c:\lib\test.jar Test1.java
    Give the output:
    .............. (loading several system classes)
    load class: com.sun.tools.javac.util.Position
    Test1.java:4: cannot find symbol
    symbol: class Test
    public class Test1 extends Test{
    ^
    load class: java.lang.Long
    load class: java.io.DataInputStream
    load class: com.sun.tools.javac.jvm.ClassReader$AnnotationCompleter
    load class: com.sun.tools.javac.jvm.ClassReader$AnnotationDeproxy
    load class: com.sun.tools.javac.jvm.ClassReader$ProxyVisitor
    load class: com.sun.tools.javac.code.Types$SubstFcn
    load class: com.sun.tools.javac.code.BoundKind
    load class: java.lang.StringBuffer
    Test1.java:12: cannot find symbol
    symbol : method hello()
    location: class Test1
    new Test1().hello();
    ^
    2 errors
    ---> from the output I found that Test.class never visited/loaded by my EncryptedClassLoader
    Though I set EncryptedClassLoader as the default system classloader but it seems to load everything except my encrypted class.
    Does anyone know why it works like this??
    Thank you in advanced,
    Alex.
    null

    I don't know why he was specifying the javac classpath like that instead of using javac's -classpath option.
    But this whole business of decrypting class loades is pretty much a waste of time. It adds a layer of comfort but it's not really what I would call secure. At some point the unencrypted bytes have to appear in memory and from that point the classes are no longer secure.
    The best protection against software piracy is not anti-decompilation techniques but time-to-market and pricing your product so that buying it is cheaper than reverse-engineering it.

Maybe you are looking for

  • Unified Messaging Exchange 2010 with Jabber

    Is anyone aware of a way to have Jabber integrate with Exchange 2010 for voice-mail? I have CUCM trunked to the server, the calls are going to the server and you can leave a voice mail and phone MWI lights up. I have the voice-mail icon on the jabber

  • How do I burn a data disc of my camcorder movies that can play on an HP?

    Very frustrated, first I have no iDVD on my new Mac.  Very disappointing.  So now, I have my home movies on the Mac and need to burn them off to a disc, when I burn to a data disc, it wont play on another computer at all.  Please help so that I can s

  • Profile for Canon "Pro Premium Matte paper"

    I will receive a package of new Canon "Pro Premium Matte paper" to use with my Pro-100 on Friday. I see that "Print Studio Pro" has a profile for this paper; however, in Lightroom a profile does not exist. Where would I find this profile? Nikon D600;

  • Third party integration

    Hi, We have a requirement wherein we want to open header of contract document directly from a third party application by generating the dynamic URL of the header. Any idea how we can achieve this? Regards, Puneet

  • Help with view stack, PLEASE.

    I have a ViewStack controled by a ToggleButtonBar inside a ApplicationControlBar the data provider for the ToggleButtonBar is my viewstack, ok it all works like a charm when i run it from flex builder 2 but when i try to execute the aplication from a