【Maven问题】 错误: 程序包xxx 不存在

今天编译maven的时候出现问题了

mvn clean compile
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project iot_push_server: Compilation failure: Co
mpilation failure:
[ERROR] /D:/iot_push-master/iot_push_server/src/main/java/com/lxr/iot/bootstrap/BaseApi.java:[3,31] 程序包com.sun.istack.internal不存在
[ERROR] /D:/iot_push-master/iot_push_server/src/main/java/com/lxr/iot/bootstrap/BaseApi.java:[74,35] 找不到符号
[ERROR]   符号:   类 NotNull
[ERROR]   位置: 接口 com.lxr.iot.bootstrap.BaseApi
[ERROR] /D:/iot_push-master/iot_push_server/src/main/java/com/lxr/iot/bootstrap/BaseApi.java:[74,49] 找不到符号
[ERROR]   符号:   类 NotNull
[ERROR]   位置: 接口 com.lxr.iot.bootstrap.BaseApi
[ERROR] /D:/iot_push-master/iot_push_server/src/main/java/com/lxr/iot/bootstrap/BaseApi.java:[74,81] 找不到符号
[ERROR]   符号:   类 NotNull
[ERROR]   位置: 接口 com.lxr.iot.bootstrap.BaseApi
[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
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn  -rf :iot_push_server

这里最关键的提示是:

程序包 com.sun.istack.internal 不存在

我看了下ide,没有报错,可以点到com.sun.istack.internal这个包里面!
这就有点麻烦了。

首先,大概猜下为啥这个包不存在
我们是在编译java文件的时候需要依赖这个包中的class,也就是mvn compile调用的编译程序的classpath中没有包含 com.sun.istack.internal这个包,而ide中却包含了这个包。而通过ide,我们观察到 com.sun.istack.internal这个包在 ${MAVEN_HOME}/jre/lib/rt.jar这个jar中,所以我们可以得出一个大致的结论:

mvn compile调用的编译程序的classpath中缺少${MAVEN_HOME}/jre/lib/rt.jar 这个jar包

那么下一步,我们应该通过日志,去观察 mvn compile的编译程序所依赖的classpath
通过maven 命令,执行以下命令debug。

mvn compile -X -e

控制台日志

[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ iot_push_server ---
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-compiler-plugin:3.3:compile from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-compiler
-plugin:3.3, parent: sun.misc.Launcher$AppClassLoader@33909752]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-compiler-plugin:3.3:compile' with basic configurator -->
[DEBUG]   (f) basedir = D:\iot_push-master\iot_push_server
[DEBUG]   (f) buildDirectory = D:\iot_push-master\iot_push_server\target
[DEBUG]   (f) classpathElements = [XXXXXXXX some jar XXXXXXXX]
[DEBUG]   (f) compileSourceRoots = [D:\iot_push-master\iot_push_server\src\main\java]
[DEBUG]   (f) compilerId = javac
[DEBUG]   (f) debug = true
[DEBUG]   (f) encoding = UTF-8
[DEBUG]   (f) failOnError = true
[DEBUG]   (f) forceJavacCompilerUse = false
[DEBUG]   (f) fork = false
[DEBUG]   (f) generatedSourcesDirectory = D:\iot_push-master\iot_push_server\target\generated-sources\annotations
[DEBUG]   (f) mojoExecution = org.apache.maven.plugins:maven-compiler-plugin:3.3:compile {execution: default-compile}
[DEBUG]   (f) optimize = false
[DEBUG]   (f) outputDirectory = D:\iot_push-master\iot_push_server\target\classes
[DEBUG]   (f) project = MavenProject: com.lxr.iot:iot_push_server:1.0-SNAPSHOT @ D:\iot_push-master\iot_push_server\pom.xml
[DEBUG]   (f) projectArtifact = com.lxr.iot:iot_push_server:jar:1.0-SNAPSHOT
[DEBUG]   (f) session = org.apache.maven.execution.MavenSession@610db97e
[DEBUG]   (f) showDeprecation = false
[DEBUG]   (f) showWarnings = false
[DEBUG]   (f) skipMultiThreadWarning = false
[DEBUG]   (f) source = 1.8
[DEBUG]   (f) staleMillis = 0
[DEBUG]   (f) target = 1.8
[DEBUG]   (f) useIncrementalCompilation = true
[DEBUG]   (f) verbose = false
[DEBUG] -- end configuration --
[DEBUG] Using compiler 'javac'.  (=======重要========)
[DEBUG] Source directories: [D:\iot_push-master\iot_push_server\src\main\java]
[DEBUG] Classpath: [D:\iot_push-master\iot_push_server\target\classes
 D:\iot_push-master\iot_push_common\target\classes , XXXXXXXX some jar XXXXXXXX]
[DEBUG] Output directory: D:\iot_push-master\iot_push_server\target\classes
[DEBUG] Adding D:\iot_push-master\iot_push_server\target\generated-sources\annotations to compile source roots:
  D:\iot_push-master\iot_push_server\src\main\java
[DEBUG] New compile source roots:
  D:\iot_push-master\iot_push_server\src\main\java
  D:\iot_push-master\iot_push_server\target\generated-sources\annotations

  ...
  
[DEBUG] Source roots:
[DEBUG]  D:\iot_push-master\iot_push_server\src\main\java
[DEBUG]  D:\iot_push-master\iot_push_server\target\generated-sources\annotations
[DEBUG] Command line options:(=======重要========)
[DEBUG] -d D:\iot_push-master\iot_push_server\target\classes -classpath XXXXXXXX some jar XXXXXXXX -sourcepath D:\iot_push-master\iot_push_server\src\main\java;D:\iot_push-master\iot_push_server\target\generated
-sources\annotations; -s D:\iot_push-master\iot_push_server\target\generated-sources\annotations -g -nowarn -target 1.8 -source 1.8 -encoding UTF-8

[DEBUG] incrementalBuildHelper#beforeRebuildExecution
[INFO] Compiling 31 source files to D:\iot_push-master\iot_push_server\target\classes
[DEBUG] incrementalBuildHelper#afterRebuildExecution

我们通过日志,发现两个很关键的内容

  1. 使用javac编译程序
[DEBUG] Using compiler 'javac'.
  1. javac的classpath参数不包含 jdk的lib文件中所包含的jar包
[DEBUG] Command line options:
[DEBUG] -d D:\iot_push-master\iot_push_server\target\classes 
-classpath ...  -sourcepath 

通过classpath,我们知道 javac会找两个的classpath,一个是系统环境变量 CLASSPATH,一个是参数 -classpath ,这里参数不包含classpath,那么必须在环境变量CLASSPATH 中寻找。

验证:

我们在window中查找classpath

C:\Users\ezlhq> echo %classpath%

.;D:\java\jdk1.8.0_152\lib\dt.jar;D:\java\jdk1.8.0_152\lib\tools.jar;

对比 ide中的classpath

同时我们手动用javac 去编译类

C:\Users\ezlhq>javac -verbose D:\iot_push-master\iot_push_server\src\main\java\com\lxr\iot\bootstrap\BaseApi.java

[解析开始时间 RegularFileObject[D:\iot_push-master\iot_push_server\src\main\java\com\lxr\iot\bootstrap\BaseApi.java]]
[解析已完成, 用时 24 毫秒]
[源文件的搜索路径: .,D:\java\jdk1.8.0_152\lib\dt.jar,D:\java\jdk1.8.0_152\lib\tools.jar]
[类文件的搜索路径: xxxx

果然验证了我们之前的答案。mvn调用javac去编译我们应用程序的时候,并没有带上 rt.jar这个jar包

那么如何解决这个问题了?

  1. window修改全局变量后还得重启才能生效,考虑到还有其他开发人员,不可能人人都重启电脑,故弃之
  2. javac -classpath的时候加上 rt.jar
    通过maven-compiler-plugin 插件使用

修改pom:bootclasspath 参考


            
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    3.5.1
                    
                        1.8
                        1.8
                        UTF-8
                      
                        
                            
                            
                            
                            ${java.home}\lib\rt.jar;${java.home}\lib\jce.jar
                        
                    
                
            
        

你可能感兴趣的:(【Maven问题】 错误: 程序包xxx 不存在)