JDK-6331821 : When annotation class is not available, javac fails compiling a class that uses the annotated class
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0u1
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: generic
  • CPU: generic
  • Submitted: 2005-10-03
  • Updated: 2024-10-07
  • Resolved: 2023-03-25
Related Reports
Relates :  
Relates :  
Relates :  
Description
I have following directory structure:

.
./Main.java
./sahoo/Sless.class
./sahoo/Sless.java
./sahoo/SlessBean.class
./sahoo/SlessBean.java
./lib/javax/ejb/Local.class
./lib/javax/ejb/Local.java

./Main.java
-----------
class Main{
 public static void main(String... args){
   sahoo.SlessBean sless;
 }
}

./sahoo/SlessBean.java
----------------------
package sahoo;
@javax.ejb.Local(sahoo.Sless.class)
public class SlessBean implements Sless {
 public void bar() {}
}

./sahoo/Sless.java
------------------
package sahoo;
public interface Sless {
 void bar();
}

When I try compiling:
javac Main.java

I get the following error:

Main.java:3: cannot find symbol
symbol  : class SlessBean
location: package sahoo
   sahoo.SlessBean sless;
        ^
1 error

But when I compile with lib in classpath, i.e. 
javac -classpath .:lib Main.java
it compiles fine.

So I don't understand why I need javax.ejb.Local to be in my compilation class path. More over, even if I needed that class, I would expect a better error message from compiler.
javax.ejb.Local.class is defined as follows:
lib/javax/ejb/Local.java
------------------------
package javax.ejb;
 
public @interface Local{
 Class[] value() default {};
}

Comments
JDK 8, 11, 19 all report the error correctly: $ javac Main.java ./sahoo/SlessBean.java:2: error: package javax.ejb does not exist @javax.ejb.Local(sahoo.Sless.class) ^ 1 error If you add "-classpath .:lib" then the compilation succeeds as expected.
25-03-2023

SUGGESTED FIX Peter, From the above suggested fix, I am getting the impression that compilation will still fail, but with a better error message. Should compilation fail? See my notes in comments section, where I show how behavior is different for annotations and interfaces. Sahoo
04-10-2005

SUGGESTED FIX The code below reports the following error: error: cannot access javax.ejb.Local class file for javax.ejb.Local not found 1 error However, this is still not informative enough and we should consider if it should be a warning. Index: src/share/classes/com/sun/tools/javac/jvm/ClassReader.java @@ -1205,6 +1205,8 @@ sym.attributes_field = ((sym.attributes_field == null) ? newList : newList.prependList(sym.attributes_field)); + } catch (CompletionFailure ex) { + log.error("cant.access", ex.sym, ex.errmsg); } finally { currentClassFileName = previousClassFileName; }
04-10-2005

EVALUATION Hi Peter, I have updated description with the content of lib/javax/ejb/Local.java. Thanks, Sahoo
04-10-2005

EVALUATION The test case is not complete. The file lib/javax/ejb/Local.java is missing.
04-10-2005

EVALUATION Giving a compiler error seems appropriate, however the error message should be improved.
03-10-2005