javac 显示错误且没有警告

发布于 2024-12-10 05:15:23 字数 642 浏览 0 评论 0原文

我正在使用 Sun 的 javac 1.6.0_26。我这样调用它:

javac -Xlint -encoding UTF-8 

通常如果有错误,则只显示错误。然而,这段代码

class Test{
    public static void main(String args[]) {
        java.util.Date d = new java.util.Date();
        system.out.println(d.getDate());
    }

会产生警告和错误:

java:5: warning: [deprecation] getDate() in java.util.Date has been deprecated
        system.out.println(d.getDate());
                            ^
java:5: package system does not exist
        system.out.println(d.getDate());

所以我的问题是:当没有错误(绝不是两者)时有任何和所有警告时,如何使 javac 仅显示错误(没有警告)?

I'm using Sun's javac 1.6.0_26. I invoke it like this:

javac -Xlint -encoding UTF-8 

and usually if there are errors only them are displayed. However this code

class Test{
    public static void main(String args[]) {
        java.util.Date d = new java.util.Date();
        system.out.println(d.getDate());
    }

produces both warning and error:

java:5: warning: [deprecation] getDate() in java.util.Date has been deprecated
        system.out.println(d.getDate());
                            ^
java:5: package system does not exist
        system.out.println(d.getDate());

So my question is: how do I make javac show only errors (without warnins) when there are any and all warnings when there are no errors (never both)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

禾厶谷欠 2024-12-17 05:15:23

javac -nowarn 中有一个标准选项,可以禁用警告消息。您可以从 javac-options 获取更多信息

There is a standard option in javac -nowarnwhich disable warning messages. You can get more informations from javac-options

云裳 2024-12-17 05:15:23

忽略编译器警告不是一个好主意。大多数时候它们确实很重要。仅当您非常确定要忽略警告时,才可以添加注释:

@SuppressWarnings("deprecation")
System.out.println(d.getDate());

就像您在想:让我先修复错误,然后修复警告。但我不认为这是一种很好的工作方式。为了解决警告,您可能必须更改大量代码,并且所有其他错误解决工作都是不需要的,因为您需要其他代码。从全球角度看待问题总是好的。

It's a bad idea to ignore the compiler warnings. Most of the time they are really important. Only if you are very sure that you want to ignore a warning you can add an annotation:

@SuppressWarnings("deprecation")
System.out.println(d.getDate());

It's like you are thinking: Let me first fix errors and afterwards the warnings. But I don't think that is a nice way of working. To solve warnings, you might have to change a lot of code, and all your other error-solving work was unneeded because you needed other code. It is always good the look at a problem globally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文