How to disable runtime warnings in java?

2019-04-10 07:46发布

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

6条回答
Anthone
2楼-- · 2019-04-10 08:10

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楼-- · 2019-04-10 08:11

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.
查看更多
地球回转人心会变
4楼-- · 2019-04-10 08:21

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.

查看更多
Luminary・发光体
5楼-- · 2019-04-10 08:24

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.

查看更多
Anthone
6楼-- · 2019-04-10 08:25

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.

查看更多
手持菜刀,她持情操
7楼-- · 2019-04-10 08:34

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>
查看更多
登录 后发表回答