How to disable runtime warnings in java?

孤者浪人 提交于 2019-12-21 17:44:17

问题


I am using a jar file in a java program and it generates warnings during runtime. But I don't want that my customer sees these warnings. How can I disable these warnings.

The warning is as below:

Sep 25, 2009 10:10:33 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://www.craigslist.org/js/jquery.js', but got 'application/x-javascript'.

回答1:


From the appearance of the message, you are using HtmlUnit which uses Commons Logging for logging messages. Unless you configure Commons Logging to write to a file, the log messages will get logged by the simple logger of Commons Logging which writes out onto the console.

If you want to make the error messages go away you could adopt either of the options:

  1. Configure Commons Logging to write to a file on disk (using log4j).
  2. Redirect the console output to /dev/null or its equivalent, as sal pinpointed.



回答2:


A much easier way, without having to add Log4j as a dependency, is to simply redirect STDERR.

System.setErr(new PrintStream("/dev/null"));

That's all you really need.

Note: Make sure you reset the stream after you are done with HtmlUnit:

final PrintStream err = new PrintStream(System.err);
System.setErr(new PrintStream("/dev/null"));

// ...

System.setErr(err);



回答3:


Just copy first lines from the link Vineet posted:

If you don't explicitly configure commons logging to use LOG4J or another logging framework then it will use the simple logger. When using the simple logger, you can change the default logging level by setting the following system property:

System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog","fatal");

Add this code somewhere in the beginning of the program.




回答4:


Assuming these are log messages, you could configure the logger to write everything to null or /dev/null

Putting a file like this in the path might help

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="NULL" class="org.apache.log4j.FileAppender">
        <param name="File" value="/dev/null"/>
    </appender>

   <logger name="com.gargoylesoftware.htmlunit">
       <level value="FATAL"/>
       <appender-ref ref="NULL"/>
   </logger>

</log4j:configuration>



回答5:


Since the OP keeps asking how to redirect to /dev/null: You can achieve a similar effect by calling System.setOut and System.setErr, passing in a PrintStream that does nothing with the output it's given.

This is a terrible hack, and the previous answers are far far cleaner.




回答6:


I found that HtmlUnit does not include the log4j implementation, only the commons logging "interface" (and abstract classes). Once you toss log4j.jar on the path, HtmlUnit behaves itself much better, and honors its configs.



来源:https://stackoverflow.com/questions/1478383/how-to-disable-runtime-warnings-in-java

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