for the error reproduction, I created a demo project: https://github.com/boyang9602/demo . I have read Maven compile "Cannot find symbol", What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?, and maven "cannot find symbol" message unhelpful . All of them cannot work.
Here are the error messages:
~/eclipse-workspace/demo (master|✔ ) ✔ $ mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.github:demo >---------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ demo ---
[INFO] Deleting /home/bo/eclipse-workspace/demo/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/bo/eclipse-workspace/demo/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/bo/eclipse-workspace/demo/src/main/java/demo/Demo.java:[19,60] cannot find symbol
symbol: variable line
location: class java.lang.Object
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.621 s
[INFO] Finished at: 2020-11-02T23:19:08-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project demo: Compilation failure
[ERROR] /home/bo/eclipse-workspace/demo/src/main/java/demo/Demo.java:[19,60] cannot find symbol
[ERROR] symbol: variable line
[ERROR] location: class java.lang.Object
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
It says at [19,60] cannot find symbol line
. However, it is a valid symbol from dependency javaparser
. And it could be compiled in eclipse.
[ERROR] /home/bo/eclipse-workspace/demo/src/main/java/demo/Demo.java:[19,60] cannot find symbol
[ERROR] symbol: variable line
[ERROR] location: class java.lang.Object
Below is the code,
package demo;
import java.util.List;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.BodyDeclaration;
import gr.uom.java.xmi.UMLOperation;
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
UMLOperation targetMethod = null;
String fileContent = "public class Demo{}";
CompilationUnit cu = StaticJavaParser.parse(fileContent);
List<BodyDeclaration> bodyDecls = cu.findAll(BodyDeclaration.class,
decl -> decl.getEnd().get().line < targetMethod.getLocationInfo().getEndLine());
}
}
If I change the predicate to some statements which do not use line
(the symbol that cannot be found), maven could work. For example, decl.getEnd().get() == null
.
My system is Ubuntu 20.04, and
~/eclipse-workspace/demo (master|✔ ) ✔ $ java -version
openjdk version "1.8.0_272"
OpenJDK Runtime Environment (build 1.8.0_272-8u272-b10-0ubuntu1~20.04-b10)
OpenJDK 64-Bit Server VM (build 25.272-b10, mixed mode)
~/eclipse-workspace/demo (master|✔ ) ✘ $ mvn --version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 1.8.0_272, vendor: Private Build, runtime: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_CA, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-52-generic", arch: "amd64", family: "unix"
Javaparser in the build path is the same as the one used by maven.
Also tried to delete the javaparser lib at local and re-download by maven.
Update: I solved it by explicitly cast the returned value to a Position object, which it should be. Then it could compile. However, it is still unexplainable for the problem. New code below:
package demo;
import java.util.List;
import com.github.javaparser.Position;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.BodyDeclaration;
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileContent = "public class Demo{}";
CompilationUnit cu = StaticJavaParser.parse(fileContent);
List<BodyDeclaration> bodyDecls = cu.findAll(BodyDeclaration.class,
decl -> ((Position)(decl.getEnd().get())).line < 0);
}
}