java javac显示错误而没有警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7777618/
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-10-30 21:24:24  来源:igfitidea点击:

javac show error without warnings

javajavac

提问by ren

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

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

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)?

所以我的问题是:当没有错误(从来没有)时有任何和所有警告时,我如何让 javac 只显示错误(没有警告)?

回答by COD3BOY

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

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

回答by Martijn Courteaux

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 mighthave 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.

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