I'm seeing some class loading behavior that appears to be inconsistent with the JVM spec and am wondering if this is a bug. Or if not, hoping someone can explain why.

The example code found below simply prints hello from its main method. It has an unused method that contains a method call to a method that declares it takes a 'C' (which is an interface) as an argument.

When the main is executed **(without A, B, and C in the class path)** a ClassNotFound Error is thrown for Interface C. (Note C is never actually needed at runtime since it is only referenced in a method that never executes).

This appears to be a violation of the JVM specification

Section 2.17.1 of the Java VM Spec, 2nd edition says:

> 
> The only requirement regarding when resolution is performed is that **any errors detected during resolution must be thrown at a point in the program where some action is taken by the program that might, directly or indirectly, require linkage to the class or interface involved in the error**
> 
> 
> 

Section 2.17.3 of the Java VM Spec, 2nd edition says:

> 
> The Java programming language allows an implementation flexibility as to when linking activities (and, because of recursion, loading) take place, provided that the semantics of the language are respected, that a class or interface is completely verified and prepared before it is initialized, and that **errors detected during linkage are thrown at a point in the program where some action is taken by the program that might require linkage to the class or interface involved in the error**.
> 
> 
> 

Note: If I change the type of the parameter on the definition to a class instead of an interface then the code loads and executes correctly.

```java
/**
 * This version fails, the method call in neverCalled() is to a method whose 
 * parameter definition is for an Interface
 */
public class Main {

    public  void neverCalled(){
          A a = new A();
          B b = new B(); // B implements C

          //method takeInter is declared to take paramters of type Interface C
          //This code is causes a ClassNotFound error do be thrown when Main
          //is loaded if A, B, and C is not in the class path
          a.takeInter(b); 
    }

    public static void main(String[] args) {
        System.out.println("Hello...");
    }
}

/**
 * This version runs, the method call in neverCalled() is to a method whose 
 * parameter definition is for a Class
 */
public class Main {

    public  void neverCalled(){
          A a = new A();
          B b = new B(); // B implements C

          //method takeInter is declared to take paramters of type Interface C
          //This code is causes a ClassNotFound error do be thrown when Main
          //is loaded if A, B, and C is not in the class path
          a.takeClass(b); 
    }

    public static void main(String[] args) {
        System.out.println("Hello...");
    }
}

public class A {
    public void takeClass(B in){};
    public void takeInter(C in){}
}

public class B implements C {}

public interface C {}

```

---

Ed,

I wasn't intentionally trying to take the quote out of context I pulled out what I thought was the relevant part. Thank you for helping me try to understand this.

Anyhow, the spec seems pretty clear to me. It says the errors must be thrown **at** a point not **by a point**. Granted I read the VM spec after reading the following in Chapter 8 of Inside The Java Virtual Machine, so maybe that colored my interpretation.

From, <http://www.artima.com/insidejvm/ed2/linkmod.html>

> 
> As described in Chapter 7, "The Lifetime of a Class," different implementations of the Java virtual machine are permitted to perform resolution at different times during the execution of a program. An implementation may choose to link everything up front by following all symbolic references from the initial class, then all symbolic references from subsequent classes, until every symbolic reference has been resolved. In this case, the application would be completely linked before its main() method was ever invoked. This approach is called early resolution. Alternatively, an implementation may choose to wait until the very last minute to resolve each symbolic reference. In this case, the Java virtual machine would resolve a symbolic reference only when it is first used by the running program. This approach is called late resolution. Implementations may also use a resolution strategy in-between these two extremes.
> 
> 
> Although a Java virtual machine implementation has some freedom in choosing when to resolve symbolic references, **every Java virtual machine must give the outward impression that it uses late resolution**. No matter when a particular Java virtual machine performs its resolution, **it will always throw any error that results from attempting to resolve a symbolic reference at the point in the execution of the program where the symbolic reference was actually used for the first time**. In this way, it will always appear to the user as if the resolution were late. If a Java virtual machine does early resolution, and during early resolution discovers that a class file is missing, it won't report the class file missing by throwing the appropriate error until later in the program when something in that class file is actually used. If the class is never used by the program, the error will never be thrown.
> 
> 
>