eclipse eclipse成功时Maven编译失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6563578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 20:17:00  来源:igfitidea点击:

Maven compilation fails while eclipse is successful

eclipsemavenjarsunpom.xml

提问by ronk

I am using maven3.03 and Eclipse 3.5 on Windows XP. I converted old web project to be in maven configuration style.
When i compile the project in eclipse everything compile.
Note: The classpath contains: Maven Dependencies and JDK(1.6_018).
When i compile from command line using mvn, I get few errors:
1.package com.sun.xml.internal.stream.buffer.stax does not exist.
2.package com.sun.xml.internal.stream.writers does not exist
3.cannot find symbol - symbol : class XMLDOMWriterImpl
4.package com.sun.xml.internal.messaging.saaj.util does not exist
5.package com.sun.xml.internal.bind.v2.runtime.unmarshaller does not exist
6.cannot find symbol - symbol : class NamespaceContexHelper
7.cannot find symbol symbol : class ByteOutputStream

我在 Windows XP 上使用 maven3.03 和 Eclipse 3.5。我将旧的 web 项目转换为 maven 配置样式。
当我在 Eclipse 中编译项目时,一切都会编译。
注意:类路径包含:Maven 依赖项和 JDK(1.6_018)。
当我使用 mvn 从命令行编译时,我得到的错误很少:
1.package com.sun.xml.internal.stream.buffer.stax 不存在。
2.package com.sun.xml.internal.stream.writers 不存在
3.找不到符号 - 符号:类 XMLDOMWriterImpl
4.package com.sun.xml.internal.messaging.saaj.util 不存在
5.package com .sun.xml.internal.bind.v2.runtime.unmarshaller 不存在
6.找不到符号 - 符号:类 NamespaceContexHelper
7.找不到符号符号:类ByteOutputStream

I can see that this something with sun jar. But i just can not understand why eclipse is ok and command line is not.

我可以看到这个带有太阳罐的东西。但我就是不明白为什么 eclipse 可以,而命令行不行。

EDIT: One of the errors i didn't mention in the above list is:

编辑:我在上面的列表中没有提到的错误之一是:

[ERROR]<path>\EventsViewer.java:[54,69] inconvertible types found: <br>java.util.SortedMap<java.util.Date,java.util.List<com.myClass>>
required: java.util.Map<? extends java.util.Date,? extends java.util.List<com.myOtherClass>>


When i see the same line in eclipse i get warning:


当我在 Eclipse 中看到同一行时,我收到警告:

Type safety: Unchecked cast from SortedMap<Date,List<myClass>> to Map<? extends Date,? extends 
 List<myOtherClass>>

In Eclipse i get warning and in maven i get error. I did check the org.eclipse.jdt.core.prefs and see the setting is org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning.

Update: I got read of some of the above errors. The "problem" is that in eclipse it appears as unused import. Stangly maven report this as error. After removing this unused import the error was gone. But still problem 3 and 7 occures

Conclusion: I guess that the warnings become errors in the javac. since I don't use any supressWarnings. I am just surprised that the error is different. What is it?

在 Eclipse 中我收到警告,在 maven 中我收到错误。我确实检查了 org.eclipse.jdt.core.prefs 并看到设置是 org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning。

更新:我读到了上述一些错误。“问题”是在 eclipse 中它显示为未使用的导入。Stangly maven 将此报告为错误。删除这个未使用的导入后,错误消失了。但仍然出现问题 3 和 7

结论:我猜这些警告变成了 javac 中的错误。因为我不使用任何 supressWarnings。我只是惊讶于错误是不同的。它是什么?

回答by deelew

This is the real reason maybe from maven forum's answer

这可能是 maven 论坛回答的真正原因

" Jerome is right that it's not recommended practice to use com.sun classes from rt.jar. And especially ones marked "internal"; you're just asking for trouble.

“ Jerome 是对的,不建议使用 rt.jar 中的 com.sun 类。尤其是那些标记为“内部”的类;你只是在自找麻烦。

However I was curious about this, so tried it out. The above class can indeed be accessed by Eclipse. However compiling the code on the commandline using javac fails with the "does not exist" error. So my assumption is that Sun's java compilerdetects when a special "internal" class is being accessed, and refuses to import the class. Eclipse uses a different compiler which presumably does not have this check.

不过我对这个很好奇,所以试了一下。上面的类确实可以被Eclipse访问。但是,使用 javac 在命令行上编译代码失败,并显示“不存在”错误。所以我的假设是 Sun 的java 编译器会检测到何时访问了一个特殊的“内部”类,并拒绝导入该类。Eclipse 使用不同的编译器,它大概没有这个检查。

Maven just uses the javac compiler available in the system execution path. Therefore the problem is nothing to do with Maven at all. It's the compiler that maven is invoking which is refusing to compile the source. I can't see any public flags in the javac commandline to disable this "blocking" of internal access, so unless you want to avoid using Sun's javac compiler you'll just have to avoid using this internal class.

Maven 仅使用系统执行路径中可用的 javac 编译器。因此,问题根本与 Maven 无关。Maven 正在调用的编译器拒绝编译源代码。我在 javac 命令行中看不到任何公共标志来禁用这种“阻止”内部访问,因此除非您想避免使用 Sun 的 javac 编译器,否则您只需要避免使用这个内部类。

Just for fun, I tried out Jerome's suggestion of putting rt.jar on the classpath: javac -cp /usr/java/jdk1.6.0_03/jre/lib/rt.jar Foo.java but that still failed to compile.

只是为了好玩,我尝试了 Jerome 的将 rt.jar 放在类路径上的建议: javac -cp /usr/java/jdk1.6.0_03/jre/lib/rt.jar Foo.java 但仍然无法编译。

The ByteOutputStream class canbe loaded at runtime via Class.forName("..").

ByteOutputStream 类可以在运行时通过 Class.forName("..") 加载。

Interestingly, a project I'm working on does happen to import class com.sun.org.apache.xml.internal.utils.SAXSourceLocator; and this works ok. Warnings are issued, but the code compiles (and yes, it's on my TODO list to fix this :-). So it's not allinternal classes that are being blocked, just selected ones. "

有趣的是,我正在做的一个项目碰巧导入了类 com.sun.org.apache.xml.internal.utils.SAXSourceLocator; 这工作正常。发出警告,但代码编译(是的,它在我的待办事项列表中以解决此问题:-)。所以并不是所有的内部类都被阻止,只是被选中的类。”

回答by Jeanne Boyarsky

Double check you are using the same JDK for both the Eclipse and Maven builds. Even if you think you are, double check again anyway.

仔细检查您对 Eclipse 和 Maven 构建使用相同的 JDK。即使你认为你是,无论如何都要再次检查。

These packages are some of the ones that are included in some versions of Java and not others.

这些包是某些 Java 版本中包含的一些包,而其他版本中不包含这些包。

回答by Omnaest

If the java version differs, adding the maven-compiler-plugin

如果java版本不同,添加 maven-compiler-plugin

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <target>1.6</target>
                    <source>1.6</source>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

with configuration of source and target pointing towards 1.6 and updating the project configuration from the Maven project context menu will solve the out of sync problem in many cases (which leads to different compilation results):

将source和target的配置指向1.6,从Maven项目上下文菜单更新项目配置,可以解决很多情况下不同步的问题(导致编译结果不同):

enter image description here

在此处输入图片说明

回答by vkrams

This is my case: I've got the same error in IntelliJ after adding //TODO, Intellij Automatically added this line in import section import com.sun.xml.internal.bind.v2.TODO;

这是我的情况:添加后我在 IntelliJ 中遇到了同样的错误,Intellij//TODO自动在导入部分添加了这一行import com.sun.xml.internal.bind.v2.TODO;

After removing the above import line, the problem resolved. Now I can able to compile successfully using maven

删除上面的导入行后,问题解决了。现在我可以使用 maven 成功编译