Maven clean install: cannot find symbol - java

My Maven clean install is failing. Below is the error message.
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile
(default-compile) on project MyProject: Compilation failure:
Compilation failure:
[ERROR] C:\..\MyClass.java:[13,2] cannot find symbol
[ERROR] symbol : class MyAnnotation
[ERROR] location: class mypackage.MyClass
MyClass.java
public class MyClass{
#MyAnnotation
public static class MyAnnotation{
//some static nested class code here...
}
MyAnnotation.java
#Retention (RetentionPolicy.RUNTIME)
public #interface MyAnnotation{
}
I have no clue why this would present problems, can anyone please give me some ideas?

Found the problem...
I apologize as I didn't include enough code for anyone to determine the root cause of the issue, normally I don't include import statements in my posts but this time I should have. The below class is a more full example. As we can see, the below class declares a static import to an object that resides in the static nested class (within the same .java file). While this is not illegal from a compilation standpoint, it was causing issues in my Maven clean install. I'm still not %100 sure why maven does not like this, but this fashion of static importing doesn't really make sense to begin with. To fix this, I removed the static import and substituded a normal static call (MyAnnoation.someObject) wherever the static import was being used.
package com.classes;
import static com.classes.MyClass.MyAnnotation.someObject;
public class MyClass{
#MyAnnotation
public static class MyAnnotation{
public static final Object someObject = new Object();
}
Again, my apologies for not providing the static import details in my original post, hopefully someone finds this helpful.

Did your maven-compiler-plugin is using 1.5. By default it used 1.4 and annotations are introduced in 1.5

Try the following:
Import just one of the classes.
Always use the FQN (fully-qualified name) for the other case, let's assume that's the annotation (#com.foo.MyAnnotation).
Also, as Vinay suggested, set the source and target versions in your maven-compiler-plugin's <configuration/>.

Related

In java, why can I only import abstract classes?

In my Java I have a class import:
import cc.hyperium.mods.HyperiumModIntegration;
however, it fails to import with error Cannot resolve symbol 'HyperiumModIntegration'.
The class I'm importing looks like this:
package cc.hyperium.mods;
public class HyperiumModIntegration {
public HyperiumModIntegration() {
}
}
Weirdly, if I make the class abstract, it imports just fine.
IntelliJ will show the class in code completion, however.
Invalidating caches and restarting in IntelliJ fixed it.
(Note to self: stop using EAP.)

Kotlin/KAPT Generated Kotlin class is not recognized as class member, but it does inside of methods

I have written an annotation processor that generates a builder class for my classes annotated with #DataBuilder
#Target(AnnotationTarget.CLASS)
#Retention(AnnotationRetention.SOURCE)
annotation class DataBuilder
My classes annotated with this annotation are located in the com.my.package.model package and the generated builder class is also located in the same package com.my.package.model but in the generated directory of course build/generated/source/kapt/debug/com/my/package/model/MyModelBuilder.kt, I can use these generated classes fine inside of my model classes(written in Kotlin)
BUT I can NOT use the generated MyModelBuilder Kotlin class inside of a java class as a class member
package com.my.package.home;
import com.my.package.model.MyModelBuilder;
public class Home {
MyModelBuilder builder; // <=== AS recognizes the class, but I'm having an compilation issue
}
Android Studio recognizes the class, but I’m having this compilation issue
com/my/package/home/Home.java:4: error: cannot find symbol
MyModelBuilder builder;
^
symbol: class MyModelBuilder
location: class Home
it’s weird because I can use this generated builder class only inside of methods, this code compiles fine:
package com.my.package.home;
import com.my.package.model.MyModelBuilder;
public class Home {
public void hello() {
MyModelBuilder builder;
}
}
could somebody here help me to understand this behavior and how to fix this? In advance, thanks!
UPDATE
I just created this repo with the necessary code to replicate the issue
https://github.com/epool/HelloKapt
The project works fine after cloning and running, to replicate the issue please un-comment this line https://github.com/epool/HelloKapt/blob/master/app/src/main/java/com/nearsoft/hellokapt/app/MainActivity.java#L13
Note: If I convert my MainActivity.java class to Kotlin(MainActivity.kt) the issues is NOT reproducible and works fine, but I don’t want to do so due to some project limitations so far
Kotlin Issue: https://youtrack.jetbrains.net/issue/KT-24591
Looking at your Github project, I notice that you don't declare a dependency on kotlin-stdlib-jdk7 in the app module. When I build the module, compiler emits the following warnings:
warning: unknown enum constant AnnotationTarget.CLASS
reason: class file for kotlin.annotation.AnnotationTarget not found
warning: unknown enum constant AnnotationRetention.SOURCE
reason: class file for kotlin.annotation.AnnotationRetention not found
warning: unknown enum constant AnnotationTarget.CLASS
reason: class file for kotlin.annotation.AnnotationTarget not found
Since kotlin-stdlib-jdk7 is declared as implementation in the annotations module, the app module doesn't see it as a transitive dependency, that might be the reason why compilation fails. To fix it, you should probably declare the correct dependency in the app module, or at least use apiElements scope for kotlin-stdlib-jdk7 in annotations.
The fact that the IDE doesn't notify you that compilation failed might be a tools bug, but there's definitely no underlying Kotlin compiler issue.

Maven can't find class even though package is included

I have the following code:
package osu.cs362.URLValidator;
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.*;
import static org.mockito.Mockito.*;
public class DomainValidatorTest {
RegexValidator rev = mock(RegexValidator.class);
}
This includes the package osu.cs362.URLValidator which contains RegexValidator.
However, when running mvn test I get:
cannot find symbol class RegexValidator
Why can't it find this class? Is this a pom.xml issue?
It is not a Maven problem.
If the RegexValidator class had the declaration like that :
package osu.cs362.URLValidator;
public class RegexValidator {
...
}
you would have not the problem. So I suppose it is not the case.
Besides, filesytem folders are not Java packages.
For example, nobody prevents you from declaring your class in the folder :
osu/cs362 of your classpath folder and declaring the package of the class like that: fictive.folder.
The class will compile.
It is the case for DomainValidatorTest. The package is not symmetric with the folder layout but the class is found by the compiler and it doesn't cause a compilation error.
But of course, it is a bad practice and it is misleading. That's why packages should always be symmetric to the folders layout.
You shoud move your DomainValidatorTest.java to directory:
src/test/java/osu/cs362/URLValidator
Directory structure should be the same as java package.

Can not find test class 'junit.test.DepartmentTest' in project 'jyxxw'

when I run configurations, but my Eclipse shows that
Can not find test class 'junit.test.DepartmentTest' in project 'jyxxw'
This is a message that Eclipse gives when it doesn't recognize the class as being a test class. Do you have any #Test annotations in your class? (I realize that sounds like a silly question, but they could be in the superclass. In which case Eclipse doesn't see them.)
Can you try replacing your test code temporarily with a really simple test and see if the error goes away?
package junit.test;
import org.junit.*;
public class DepartmentTest {
#Test public void myTest() {}
}
If this works, you can see what's missing in your class. If it doesn't work, it is likely a classpath issue.

recompile single class in package

I am running into this problem when trying to recompile a single class inside a package.
Now this class uses global types and some of these global types reference it. So taking it out of the package really isn't an option.
So when I try to compile it with javac alone, I get invalid symbol errors and netbeans shows it is trying to compile things like classespackage.globaltype. Basically it is searching for the global classes inside of the package. Is there anyway to stop it from doing that?
Here is the code:
Global
public class Global {
example.Main main;
public Global(example.Main m) {
main = m;
}
}
example.Main
package example;
public class Main {
public static void main(String[] args) {
Global g = new Global(new Main()); // COMPILE ERROR
}
}
I get invalid symbol errors
You probably meant "Cannot find symbol" errors? This can be caused by anything. Imported class which is not in the compiletime classpath, methods which does not exist, variables which are out of the scope. You really need to post the compilation errors to get more detailed answers.
At least, this much sounds like that you didn't specify the dependencies (the imported classes) in the compiletime classpath using the -cp or -classpath argument.
Is there anyway to stop it from doing that?
By listening to those errors and taking actions accordingly.
Update as per the posted code example: the cause of the problem is that classes in the default package (i.e. classes without a package declaration) are invisible to classes inside a concrete package (i.e. classes with a package declaration). You need to put Global in a package. Then it's visible (importable) to classes inside a package.
If you reference the global types in a class package, you'll have to include them in your classpath. When you do javac, make sure you include the global types class in your classpath (with the -cp option.)

Categories