javac show error without warnings

余生长醉 提交于 2019-12-08 19:32:12

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/7777618/javac-show-error-without-warnings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!