【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 <goals> -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 参考

<pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                      <!-- 过期的方法的警告
                        <compilerArgs>
                            <arg>-Xlint:deprecation</arg>
                        </compilerArgs> -->
                        <compilerArguments>
                            <!-- 是否输出所有的编译信息(包括类的加载等)-->
                            <!--<verbose />-->
                            <!-- 解决maven命令编译报错,因为rt.jar 和jce.jar在jre的lib下面,不在jdk的lib下面,
                            导致maven找不到(java7以后会出现这个问题),将这2个jar包拷贝到jdk的lib下面估计也好使-->
                            <bootclasspath>${java.home}\lib\rt.jar;${java.home}\lib\jce.jar</bootclasspath>
                        </compilerArguments>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容