Package does not exist, java file generated from annotation processor


Darling

I'm trying to generate a class with (my custom) annotation processor. (I am using spring-boot, maven) for example

@Matcher
public class A{...}

should produce

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
public class AMatcher extends TypeSafeMatcher<A>{...}

However, when I generate the file, it fails with package org.hamcrest does not existan error .

Since the annotation processor runs first, my first guess is that the classPath is not set, so it can't find the package.

However, I'm not very experienced with annotation processors, so I'm not sure if this is the case.

What could be causing the error, and what are the legal ways to fix this? (if it is possible)

file structure

src
 main
  java
   com
    testProject
     util
      A.java

The structure of the generated file

target
 generated-sources
  annotations
   com
    testProject
     util
      AMatcher.java

Thanks for the help!

rzwitserloot

My first guess is that the classPath is not set, so it can't find the package.

Not how.

The annotation processor just kicks out a text file. At "creation time", i.e. when the annotation processor creates this file, it's just a text file; it won't be parsed as a source file at all, so it's impossible for the classpath to apply anything to the file at this stage.

Because that processor made the file, the compiler realizes that the new source file is now there, so it starts compiling over again (but is a bit clever, there's no need to reprocess all the files) - that's the "rounds" system, RoundEnvwhat's this in the annotation processor. The files in the "first" compilation run have no meaning or are completely fine until some AP generates the files, in which case those first files will be compiled in the second round. In other words, javaceven errors of a "not found" nature, such as the one you encountered, are deferred until the end of processing.

The classpath applied to any round is no different and is the full compile classpath.

So the most likely explanation is that hamcrest (or junit) is not on the classpath at all. Probably you only have the junit jar on the path and no junit's transient deps ( org.hamcresthere).

A good next step is to forget about the comments for now and write your AP made files as actual non-generated source files (for now, to test and fix this), you'll find that you get the same error, Thus showing that it has nothing to do with the AP. Even if you move the entire AP out of the path/disable AP processing at this point.

Related


Package does not exist - Java

The internet This is a very trivial question, but the stack doesn't seem to have many good answers. (see below for tree structure) In start.javawhat I'm doing: import web.*, but can't find the packaged site. All classes in the network are " package web.excepti

Package does not exist - Java

The internet This is a very trivial question, but the stack doesn't seem to have many good answers. (see below for tree structure) In start.javawhat I'm doing: import web.*, but can't find the packaged site. All classes in the network are " package web.excepti

Package javax.servlet.annotation does not exist

hard: I have defined some of the following imports as shown in the image below. I would like to know how to get rid of the "package javax.servlet.annotation does not exist"error. I'm currently working on a "Java Web" type project in NetBeans 7.4. . Second, I b

Package javax.annotation.security does not exist

Alvaro Gonzalez I'm trying to start a Jersey/1.7 based project from scratch (instead of duplicating an existing project and adding new code on top of it, which my clients usually do) to see how this stuff works. I'm at a very early stage trying to handle a sim

Package javax.annotation.security does not exist

Alvaro Gonzalez I'm trying to start a Jersey/1.7 based project from scratch (instead of duplicating an existing project and adding new code on top of it, which my clients usually do) to see how this stuff works. I'm at a very early stage trying to handle a sim

Package javax.annotation.security does not exist

Alvaro Gonzalez I'm trying to start a Jersey/1.7 based project from scratch (instead of duplicating an existing project and adding new code on top of it, which my clients usually do) to see how this stuff works. I'm at a very early stage trying to handle a sim

Package javax.servlet.annotation does not exist

rocky I have defined some of the following imports as shown in the image below. I would like to know how to get rid of the "package javax.servlet.annotation does not exist"error. I'm currently working on a "Java Web" type project in NetBeans 7.4. . Second, I b

Package javax.servlet.annotation does not exist

rocky I have defined some of the following imports as shown in the image below. I would like to know how to get rid of the "package javax.servlet.annotation does not exist"error. I'm currently working on a "Java Web" type project in NetBeans 7.4. . Second, I b

Package javax.servlet.annotation does not exist

hard: I have defined some of the following imports as shown in the image below. I would like to know how to get rid of the "package javax.servlet.annotation does not exist"error. I'm currently working on a "Java Web" type project in NetBeans 7.4. . Second, I b

Package javax.annotation.security does not exist

Alvaro Gonzalez I'm trying to start a Jersey/1.7 based project from scratch (instead of duplicating an existing project and adding new code on top of it, which my clients usually do) to see how this stuff works. I'm at a very early stage trying to handle a sim

Package javax.annotation.security does not exist

Alvaro Gonzalez I'm trying to start a Jersey/1.7 based project from scratch (instead of duplicating an existing project and adding new code on top of it, which my clients usually do) to see how this stuff works. I'm at a very early stage trying to handle a sim

Package javax.annotation.security does not exist

Alvaro Gonzalez I'm trying to start a Jersey/1.7 based project from scratch (instead of duplicating an existing project and adding new code on top of it, which my clients usually do) to see how this stuff works. I'm at a very early stage trying to handle a sim

Package javax.annotation.security does not exist

Alvaro Gonzalez I'm trying to start a Jersey/1.7 based project from scratch (instead of duplicating an existing project and adding new code on top of it, which my clients usually do) to see how this stuff works. I'm at a very early stage trying to handle a sim