United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4917309 (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
JDK-4917309 : (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading

Details
Type:
Enhancement
Submit Date:
2003-09-04
Status:
Resolved
Updated Date:
2012-10-08
Project Name:
JDK
Resolved Date:
2009-09-26
Component:
core-libs
OS:
windows_xp
Sub-Component:
java.lang:class_loading
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.4.2
Fixed Versions:

Related Reports
Backport:
Relates:

Sub Tasks

Description
Name: jl125535			Date: 09/04/2003


A DESCRIPTION OF THE REQUEST :
When starting a Java program, Java needs to load lots of Java classes. java.lang.ClassLoader.findBootstrapClass() and java.net.URLClassLoader.findClass() both throw ClassNotFoundExceptions for each application class or class from third party libraries to be loaded.

I modified java.lang.Throwable.java (see attached source code and method _logThrowable() ) to print information about ClassNotFoundExceptions to stdout. Compile it and put the .class file in a newly created empty directory-tree c:\jdkpatch\java\lang.

When starting SwingSet with
    java -Xbootclasspath/p:c:\jdkpatch -jar SwingSet2.jar
285 instances of ClassNotFoundException are thrown.

In our application, consisting of thousands of classes, thousands of ClassNotFoundExceptions are thrown during startup.

JUSTIFICATION :
Throwing and catching exceptions slows down the application. In addition to that for keeping stack-traces objects need to be created and garbage-collected. This has an impact on application startup time.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I suggest using return-codes instead of exceptions wherever possible. e.g. java.lang.ClassLoader.findBootstrapClass0() could simply return null if the class was not found.

---------- BEGIN SOURCE ----------
My changes to Throwable.java:
- added new static variable "count"
- added new method "_logThrowable" (please note that the method's
  implementation is slightly different bug 4917307)
- called method "_logThrowable" from the constructor

Here is the code:

    private static int count = 0;
    private void _logThrowable()
    {
        if (getClass() == ClassNotFoundException.class)
        {
            count++;
            System.out.println("ClassNotFoundException #" + count);
            printStackTrace();
        }
    }
    public Throwable() {
        fillInStackTrace();
        _logThrowable();
    }
    public Throwable(String message) {
        fillInStackTrace();
        detailMessage = message;
        _logThrowable();
    }
    public Throwable(String message, Throwable cause) {
        fillInStackTrace();
        detailMessage = message;
        this.cause = cause;
        _logThrowable();
    }
    public Throwable(Throwable cause) {
        fillInStackTrace();
        detailMessage = (cause==null ? null : cause.toString());
        this.cause = cause;
        _logThrowable();
    }

---------- END SOURCE ----------
(Incident Review ID: 201376) 
======================================================================

                                    

Comments
EVALUATION

I seem to recall a similar suggestion a while back but I haven't been able to
locate the bugid.

- iag@sfbay 2003-09-04
                                     
2003-09-04
EVALUATION

ClassLoader.findBootstrapClass is an internal interface to load class with the bootstrap class loader. We can fix this internal interface to return null if class not found instead of throwing ClassNotFoundException.

The public API ClassLoader.findSystemClass and ClassLoader.loadClass will throw ClassNotFoundException as they are specified.

This involves the fix in the hotspot VM and JDK java.lang.ClassLoader.

This CR is used to track the fix for the JDK and 6864003 is for the HS fix.
                                     
2009-07-23
SUGGESTED FIX

Changeset: fdf11ce72e8e
Author:    mchung
Date:      2009-08-06 11:25 -0700
URL:       http://hg.openjdk.java.net/jdk7/tl/jdk/rev/fdf11ce72e8e

4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
Summary: Change findBootstrapClass to return null instead of throwing CNFE if class not found
Reviewed-by: alanb, dholmes, iris

! src/share/classes/java/lang/ClassLoader.java
! src/share/javavm/export/jvm.h
! src/share/native/java/lang/ClassLoader.c
                                     
2009-09-08



Hardware and Software, Engineered to Work Together