如何在 Java 中禁用运行时警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1478383/
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
How to disable runtime warnings in java?
提问by Yatendra Goel
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.
我在 java 程序中使用 jar 文件,它在运行时生成警告。但我不希望我的客户看到这些警告。如何禁用这些警告。
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'.
回答by Vineet Reynolds
From the appearance of the message, you are using HtmlUnit which uses Commons Loggingfor logging messages. Unless you configure Commons Logging to write to a file, the log messages will get logged by the simple loggerof Commons Logging which writes out onto the console.
从消息的外观来看,您正在使用HtmlUnit,它使用 Commons Logging来记录消息。除非您将 Commons Logging 配置为写入文件,否则日志消息将由Commons Logging的简单记录器记录,该记录器会写出到控制台。
If you want to make the error messages go away you could adopt either of the options:
如果您想让错误消息消失,您可以采用以下任一选项:
- Configure Commons Logging to write to a file on disk (using log4j).
- Redirect the console output to /dev/null or its equivalent, as sal pinpointed.
- 配置 Commons Logging 以写入磁盘上的文件(使用 log4j)。
- 正如萨尔指出的那样,将控制台输出重定向到 /dev/null 或其等效项。
回答by LanguagesNamedAfterCofee
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);
回答by sal
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>
回答by Denis Tulskiy
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. When using the simple logger, you can change the default logging level by setting the following system property:
如果您没有明确配置公共日志记录以使用 LOG4J 或其他日志记录框架,那么它将使用简单的记录器。使用简单记录器时,您可以通过设置以下系统属性来更改默认日志记录级别:
System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog","fatal");
Add this code somewhere in the beginning of the program.
将此代码添加到程序开头的某处。
回答by daveb
Since the OP keeps asking how to redirect to /dev/null:
You can achieve a similar effect by calling System.setOutand System.setErr, passing in a PrintStreamthat does nothing with the output it's given.
由于 OP 不断询问如何重定向到 /dev/null:您可以通过调用System.setOutand来实现类似的效果System.setErr,传入一个PrintStream对其给出的输出没有任何作用的。
This is a terrible hack, and the previous answers are far far cleaner.
这是一个可怕的黑客,以前的答案要清晰得多。
回答by Roboprog
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.
我发现 HtmlUnit 不包括 log4j 实现,只有公共日志记录“接口”(和抽象类)。一旦你把 log4j.jar 放到路径上,HtmlUnit 就会表现得更好,并尊重它的配置。

