Class.forName fails after a while (but runs at the start...)

Posted on 2006-05-19
Last Modified: 2010-03-31
Hello ,
I have a strange trouble on a very old application.
The context: (don't laught...)
-Webpshere Application server 2.03, jdk 1.1.8.

I have the following code :
    try {
      Class c = Class.forName(className);
      obj = (Object) c.newInstance();
    } catch (ClassNotFoundException ex) {
      Debug.logln("Class not found Exception: " + className);

It can runs a full week (24/7), a full month, and sometimes, the forName throw the ClassNotFoundException...
And this for a single class, which IS in the class path because it runs well...
Any idea ?

(Please don't troll with "upgrade the app server, change the jdk, ... " ;). I have to stick with this.)

Question by:Jaycee
    LVL 24

    Expert Comment

    Class.forName() follows the current classloader. Does it change underway?


    Author Comment

    Nope ...
    It's a standart webapp, that runs 24/7, the server (software & hardware) and app server is used only for this application, and no human acivity on the config is done.
    and I guess the classloader wouldn't change by itself ...
    LVL 24

    Expert Comment

    That depends, may be some part of the application sets a different classloader.

    Many stable and reliable applications contain a part that is seldom used, but that spoils the whole run.
    I speak from 35 years of automation: computers don't make mistakes and you can't be sure of anything.
    I've seen programs that suddenly dump 750Mb of waste, because there was this little subroutine that never had failed until ..........................

    Go and check all sources and (!!!!) all added jar's or packages; open source is open, not unfailing .........................


    Author Comment

    The app uses some dll for SAP calls...
    the majority of jar used is proprietary, and not opened. (and jad all class is not a solution ;)).
    Do you know a way to detect a classlaoder / classpath /  ..  modification ?
    I can add a check before each forName.
    Can I programmatically store it and reset it if it's changed ? (I never play with classloader modification on runtime ... )

    Thanks for your help so far...
    LVL 24

    Expert Comment

    I too, never played with toys I didn't understand.

    Now, the Class.forName() fails for a class that it should load.
    Is it invalid or 'pointing' at a wrong place?
    I guess that the classloader initially 'points' at the area defined by CLASSPATH.
    So, could you try to load another class (after the failure) that just got to be there?

    Here I rest my case, only "objects" and "CEHJ" might help you further.


    ;JOOP! (only wizard)
    LVL 30

    Expert Comment

    Are you using System.getProperties () or setProperties () anywhere?

    Author Comment

    LVL 13

    Expert Comment

    Hi Jaycee,

    You may use the same classloader as initially, by using a ClassLoader reference :

    ClassLoader loader;

    // When you application start :

    // Instead of Class.forName, use :
    Class c = loader.loadClass(className);
    LVL 13

    Accepted Solution

    I guess my comment may get the points.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now