简体   繁体   English

如何在java中禁用运行时警告?

[英]How to disable runtime warnings in java?

I am using a jar file in a java program and it generates warnings during runtime. 我在java程序中使用jar文件,它在运行时生成警告。 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'.

From the appearance of the message, you are using HtmlUnit which uses Commons Logging for logging messages. 从消息的外观来看,您正在使用HtmlUnit,它使用Commons Logging来记录消息。 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. 除非您将Commons Logging配置为写入文件,否则日志消息将由Commons Logging的简单记录器记录,该记录器将写入控制台。

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). 配置Commons Logging以写入磁盘上的文件(使用log4j)。
  2. Redirect the console output to /dev/null or its equivalent, as sal pinpointed. 将控制台输出重定向到/ dev / null或其等效项,如sal精确定位。

A much easier way, without having to add Log4j as a dependency, is to simply redirect STDERR. 更简单的方法是,无需将Log4j作为依赖项添加,只需重定向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: 注意:确保在完成HtmlUnit后重置流:

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

// ...

System.setErr(err);

Assuming these are log messages, you could configure the logger to write everything to null or /dev/null 假设这些是日志消息,您可以配置记录器将所有内容写入null或/ 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>

Just copy first lines from the link Vineet posted: 只需从Vineet发布的链接中复制第一行:

If you don't explicitly configure commons logging to use LOG4J or another logging framework then it will use the simple logger. 如果您没有显式配置commons日志记录以使用LOG4J或其他日志记录框架,那么它将使用简单的记录器。 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. 在程序开头的某处添加此代码。

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. 由于OP一直在询问如何重定向到/ dev / null:您可以通过调用System.setOutSystem.setErr来实现类似的效果,传入一个PrintStream ,它不会对给定的输出做任何事情。

This is a terrible hack, and the previous answers are far far cleaner. 这是一个可怕的黑客,以前的答案远远更清晰。

I found that HtmlUnit does not include the log4j implementation, only the commons logging "interface" (and abstract classes). 我发现HtmlUnit不包含log4j实现,只有commons记录“interface”(和抽象类)。 Once you toss log4j.jar on the path, HtmlUnit behaves itself much better, and honors its configs. 一旦你在路径上扔了log4j.jar,HtmlUnit就会表现得更好,并且尊重它的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM