Log4j使用

概述
  • Log4J 是 Apache 的一个开源项目。通过在项目中使用 Log4J,我们可以控制日志信息输出到控制台、文件、GUI 组件、甚至是数据库中。我们可以控制每一条日志的输出格式,通过定义日志的输出级别,可以更灵活的控制日志的输出过程。方便项目的调试。
  • 官网: ​​官网连接​

3大组件
  • Log4J主要由Logger(日志记录器)、Appender(输出端)和 Layout(日志格式化器)组成。
  • Logger控制日志的输出级别与日志是否输出;
  • Appender指定日志的输出方式(ConsoleAppender控制台、FileAppender文件、JDBCAppender等;
  • Layout控制日志信息的输出格式(simple格式、HTML格式、PatternLayout自定义格式).

日志等级说明
  • 1.Log4J 在 org.apache.log4j.Level 类中定义了:OFF < DEBUG < INFO < WARN < ERROR < FATAL < ALL
  • 2.可以简单地理解为级别越大越重要。 Log4j有一个规则:只输出级别不低于设定级别的日志信息,假设Loggers级别设定为INFO,则INFO、WARN、ERROR、FATAL和ALL级别的日志信息都会输出,而级别比INFO低的DEBUG则不会输出。
  • 3.一般只使用4个级别,优先级从高到低为 ERROR > WARN > INFO > DEBUG。

目录

说明

ERROR

发生错误事件,但仍不影响系统的继续运行

WARN

警告,即潜在的错误情形

INFO

一般在粗粒度级别上,强调应用程序的运行全程

DEBUG

一般用于细粒度级别上,对调试应用程序非常有帮助


配置log4j.properties

1.只需要在项目路径下新建log4j.properties配置文件,并配置日志的输出格式等信息,Log4J框架会自动的加载配置文件,并将配置信息设置到Logger中。

2.log4j.rootLogger = [level], appenderName, appenderName, ... (level是错误级别,appenderName是输出目的地,本例设为Console,可以定义多个)

# 控制台输出配置
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
# 如果使用PatternLayout布局就要指定的打印信息的具体格式ConversionPattern
log4j.appender.Console.layout.ConversionPattern=%d [%t] %p [%c] - %m%n
#指定日志的输出级别与输出端
log4j.rootLogger=DEBUG,Console

#%p: 输出日志信息优先级,即DEBUG,INFO,WARN,ERROR,FATAL,
#%d: 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyyy-MM-dd HH:mm:ss,SSS},输出类似:2011-10-18 22:10:28,921
#%r: 输出自应用启动到输出该log信息耗费的毫秒数
#%c: 输出日志信息所属的类目,通常就是所在类的全名
#%t: 输出产生该日志事件的线程名
#%l: 输出日志事件的发生位置,相当于%C.%M(%F:%L)的组合,包括类目名、发生的线程,以及在代码中的行数。
#%x: 输出和当前线程相关联的NDC(嵌套诊断环境),尤其用到像java servlets这样的多客户多线程的应用中。
#%%: 输出一个"%"字符
#%F: 输出日志消息产生时所在的文件名称
#%L: 输出代码中的行号
#%m: 输出代码中指定的消息,产生的日志具体信息
#%n: 输出一个回车换行符,Windows平台为"\r\n",Unix平台为"\n"输出日志信息换行
程序中使用Log4j 
  • 项目中引入log4j的jar包
  • 添加配置文件log4j.properties
  • 代码中使用
import org.apache.log4j.Logger;
public class Log4jTest {
public static void main(String[] args) {
Logger logger = Logger.getLogger(Log4jTest.class);
logger.debug("这是debug");
logger.info("这是info");
logger.warn("这是warn");
logger.error("这是error");
logger.fatal("这是fatal");
}
}
测试结果
E:\Develop\jdk8\bin\java.exe "-javaagent:E:\Develop\idea2018-3\IntelliJ IDEA 2018.3.6\lib\idea_rt.jar=61421:E:\Develop\idea2018-3\IntelliJ IDEA 2018.3.6\bin" -Dfile.encoding=UTF-8 -classpath E:\Develop\jdk8\jre\lib\charsets.jar;E:\Develop\jdk8\jre\lib\deploy.jar;E:\Develop\jdk8\jre\lib\ext\access-bridge-64.jar;E:\Develop\jdk8\jre\lib\ext\cldrdata.jar;E:\Develop\jdk8\jre\lib\ext\dnsns.jar;E:\Develop\jdk8\jre\lib\ext\jaccess.jar;E:\Develop\jdk8\jre\lib\ext\jfxrt.jar;E:\Develop\jdk8\jre\lib\ext\localedata.jar;E:\Develop\jdk8\jre\lib\ext\nashorn.jar;E:\Develop\jdk8\jre\lib\ext\sunec.jar;E:\Develop\jdk8\jre\lib\ext\sunjce_provider.jar;E:\Develop\jdk8\jre\lib\ext\sunmscapi.jar;E:\Develop\jdk8\jre\lib\ext\sunpkcs11.jar;E:\Develop\jdk8\jre\lib\ext\zipfs.jar;E:\Develop\jdk8\jre\lib\javaws.jar;E:\Develop\jdk8\jre\lib\jce.jar;E:\Develop\jdk8\jre\lib\jfr.jar;E:\Develop\jdk8\jre\lib\jfxswt.jar;E:\Develop\jdk8\jre\lib\jsse.jar;E:\Develop\jdk8\jre\lib\management-agent.jar;E:\Develop\jdk8\jre\lib\plugin.jar;E:\Develop\jdk8\jre\lib\resources.jar;E:\Develop\jdk8\jre\lib\rt.jar;E:\Develop\Hadoop-Idea-Dev\example-hdfs\target\classes;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-common\3.1.4\hadoop-common-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-annotations\3.1.4\hadoop-annotations-3.1.4.jar;E:\Develop\jdk8\lib\tools.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\guava\guava\27.0-jre\guava-27.0-jre.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\guava\failureaccess\1.0\failureaccess-1.0.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\guava\listenablefuture\9999.0-empty-to-avoid-conflict-with-guava\listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;E:\Develop\apache-maven-3.6.1\repository\org\checkerframework\checker-qual\2.5.2\checker-qual-2.5.2.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\errorprone\error_prone_annotations\2.2.0\error_prone_annotations-2.2.0.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\codehaus\mojo\animal-sniffer-annotations\1.17\animal-sniffer-annotations-1.17.jar;E:\Develop\apache-maven-3.6.1\repository\commons-cli\commons-cli\1.2\commons-cli-1.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\commons\commons-math3\3.1.1\commons-math3-3.1.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\httpcomponents\httpclient\4.5.2\httpclient-4.5.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\httpcomponents\httpcore\4.4.4\httpcore-4.4.4.jar;E:\Develop\apache-maven-3.6.1\repository\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;E:\Develop\apache-maven-3.6.1\repository\commons-io\commons-io\2.5\commons-io-2.5.jar;E:\Develop\apache-maven-3.6.1\repository\commons-net\commons-net\3.6\commons-net-3.6.jar;E:\Develop\apache-maven-3.6.1\repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;E:\Develop\apache-maven-3.6.1\repository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-server\9.4.20.v20190813\jetty-server-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-http\9.4.20.v20190813\jetty-http-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-io\9.4.20.v20190813\jetty-io-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-util\9.4.20.v20190813\jetty-util-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-servlet\9.4.20.v20190813\jetty-servlet-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-security\9.4.20.v20190813\jetty-security-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-webapp\9.4.20.v20190813\jetty-webapp-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-xml\9.4.20.v20190813\jetty-xml-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\javax\servlet\jsp\jsp-api\2.1\jsp-api-2.1.jar;E:\Develop\apache-maven-3.6.1\repository\com\sun\jersey\jersey-core\1.19\jersey-core-1.19.jar;E:\Develop\apache-maven-3.6.1\repository\javax\ws\rs\jsr311-api\1.1.1\jsr311-api-1.1.1.jar;E:\Develop\apache-maven-3.6.1\repository\com\sun\jersey\jersey-servlet\1.19\jersey-servlet-1.19.jar;E:\Develop\apache-maven-3.6.1\repository\com\sun\jersey\jersey-json\1.19\jersey-json-1.19.jar;E:\Develop\apache-maven-3.6.1\repository\org\codehaus\jettison\jettison\1.1\jettison-1.1.jar;E:\Develop\apache-maven-3.6.1\repository\com\sun\xml\bind\jaxb-impl\2.2.3-1\jaxb-impl-2.2.3-1.jar;E:\Develop\apache-maven-3.6.1\repository\org\codehaus\jackson\jackson-core-asl\1.9.2\jackson-core-asl-1.9.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\codehaus\jackson\jackson-mapper-asl\1.9.2\jackson-mapper-asl-1.9.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\codehaus\jackson\jackson-jaxrs\1.9.2\jackson-jaxrs-1.9.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\codehaus\jackson\jackson-xc\1.9.2\jackson-xc-1.9.2.jar;E:\Develop\apache-maven-3.6.1\repository\com\sun\jersey\jersey-server\1.19\jersey-server-1.19.jar;E:\Develop\apache-maven-3.6.1\repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;E:\Develop\apache-maven-3.6.1\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;E:\Develop\apache-maven-3.6.1\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;E:\Develop\apache-maven-3.6.1\repository\commons-beanutils\commons-beanutils\1.9.4\commons-beanutils-1.9.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\commons\commons-configuration2\2.1.1\commons-configuration2-2.1.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\commons\commons-lang3\3.4\commons-lang3-3.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;E:\Develop\apache-maven-3.6.1\repository\org\slf4j\slf4j-log4j12\1.7.25\slf4j-log4j12-1.7.25.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\avro\avro\1.7.7\avro-1.7.7.jar;E:\Develop\apache-maven-3.6.1\repository\com\thoughtworks\paranamer\paranamer\2.3\paranamer-2.3.jar;E:\Develop\apache-maven-3.6.1\repository\org\xerial\snappy\snappy-java\1.0.5\snappy-java-1.0.5.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\re2j\re2j\1.1\re2j-1.1.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\protobuf\protobuf-java\2.5.0\protobuf-java-2.5.0.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\code\gson\gson\2.2.4\gson-2.2.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-auth\3.1.4\hadoop-auth-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\com\nimbusds\nimbus-jose-jwt\7.9\nimbus-jose-jwt-7.9.jar;E:\Develop\apache-maven-3.6.1\repository\com\github\stephenc\jcip\jcip-annotations\1.0-1\jcip-annotations-1.0-1.jar;E:\Develop\apache-maven-3.6.1\repository\net\minidev\json-smart\2.3\json-smart-2.3.jar;E:\Develop\apache-maven-3.6.1\repository\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\curator\curator-framework\2.13.0\curator-framework-2.13.0.jar;E:\Develop\apache-maven-3.6.1\repository\com\jcraft\jsch\0.1.55\jsch-0.1.55.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\curator\curator-client\2.13.0\curator-client-2.13.0.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\curator\curator-recipes\2.13.0\curator-recipes-2.13.0.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\code\findbugs\jsr305\3.0.2\jsr305-3.0.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\htrace\htrace-core4\4.1.0-incubating\htrace-core4-4.1.0-incubating.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\zookeeper\zookeeper\3.4.13\zookeeper-3.4.13.jar;E:\Develop\apache-maven-3.6.1\repository\jline\jline\0.9.94\jline-0.9.94.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\yetus\audience-annotations\0.5.0\audience-annotations-0.5.0.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\commons\commons-compress\1.19\commons-compress-1.19.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-simplekdc\1.0.1\kerb-simplekdc-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-client\1.0.1\kerb-client-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerby-config\1.0.1\kerby-config-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-core\1.0.1\kerb-core-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerby-pkix\1.0.1\kerby-pkix-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerby-asn1\1.0.1\kerby-asn1-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerby-util\1.0.1\kerby-util-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-common\1.0.1\kerb-common-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-crypto\1.0.1\kerb-crypto-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-util\1.0.1\kerb-util-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\token-provider\1.0.1\token-provider-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-admin\1.0.1\kerb-admin-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-server\1.0.1\kerb-server-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-identity\1.0.1\kerb-identity-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerby-xdr\1.0.1\kerby-xdr-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\com\fasterxml\jackson\core\jackson-databind\2.9.10.4\jackson-databind-2.9.10.4.jar;E:\Develop\apache-maven-3.6.1\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.10\jackson-annotations-2.9.10.jar;E:\Develop\apache-maven-3.6.1\repository\com\fasterxml\jackson\core\jackson-core\2.9.10\jackson-core-2.9.10.jar;E:\Develop\apache-maven-3.6.1\repository\org\codehaus\woodstox\stax2-api\3.1.4\stax2-api-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\com\fasterxml\woodstox\woodstox-core\5.0.3\woodstox-core-5.0.3.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-hdfs\3.1.4\hadoop-hdfs-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-util-ajax\9.4.20.v20190813\jetty-util-ajax-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\commons-daemon\commons-daemon\1.0.13\commons-daemon-1.0.13.jar;E:\Develop\apache-maven-3.6.1\repository\io\netty\netty\3.10.6.Final\netty-3.10.6.Final.jar;E:\Develop\apache-maven-3.6.1\repository\io\netty\netty-all\4.1.48.Final\netty-all-4.1.48.Final.jar;E:\Develop\apache-maven-3.6.1\repository\org\fusesource\leveldbjni\leveldbjni-all\1.8\leveldbjni-all-1.8.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-client\3.1.4\hadoop-client-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-hdfs-client\3.1.4\hadoop-hdfs-client-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\com\squareup\okhttp\okhttp\2.7.5\okhttp-2.7.5.jar;E:\Develop\apache-maven-3.6.1\repository\com\squareup\okio\okio\1.6.0\okio-1.6.0.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-yarn-api\3.1.4\hadoop-yarn-api-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\javax\xml\bind\jaxb-api\2.2.11\jaxb-api-2.2.11.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-yarn-client\3.1.4\hadoop-yarn-client-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-mapreduce-client-core\3.1.4\hadoop-mapreduce-client-core-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-yarn-common\3.1.4\hadoop-yarn-common-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\com\sun\jersey\jersey-client\1.19\jersey-client-1.19.jar;E:\Develop\apache-maven-3.6.1\repository\com\fasterxml\jackson\module\jackson-module-jaxb-annotations\2.9.10\jackson-module-jaxb-annotations-2.9.10.jar;E:\Develop\apache-maven-3.6.1\repository\com\fasterxml\jackson\jaxrs\jackson-jaxrs-json-provider\2.9.10\jackson-jaxrs-json-provider-2.9.10.jar;E:\Develop\apache-maven-3.6.1\repository\com\fasterxml\jackson\jaxrs\jackson-jaxrs-base\2.9.10\jackson-jaxrs-base-2.9.10.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-mapreduce-client-jobclient\3.1.4\hadoop-mapreduce-client-jobclient-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-mapreduce-client-common\3.1.4\hadoop-mapreduce-client-common-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\junit\junit\4.13\junit-4.13.jar;E:\Develop\apache-maven-3.6.1\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar Log4jTest
2022-05-12 10:12:06,561 [main] ERROR [Log4jTest] - 这是error
2022-05-12 10:12:06,561 [main] FATAL [Log4jTest] - 这是fatal



Google-option使用

  • Google-option是Bazel Project中的命令行参数解析器。
  • option程序包已拆分为一个单独的jar,可用于通用程序。
  • GitHub:https://github.com/pcj/google-options


实现思路
  1. 使用Google Option解析命令行参数。
  2. 读取要采集的数据目录,生成上传任务,上传任务包含一个任务文件,该文件包含了要上传哪些文件到HDFS上。
  3. 执行任务,读取要上传的任务文件,挨个将任务文件中的文件上传到HDFS。上传中、上传完毕需要给任务文件添加特别的标识。

安装
  • 所谓安装就是在项目中引入Google-option的jar包
<!--Google-option的引用-->
<dependency>
<groupId>com.github.pcj</groupId>
<artifactId>google-options</artifactId>
<version>1.0.0</version>
</dependency>


Step1:首先创建参数实体类 
  • 类应提供与预期的命令行选项相对应的public字段;每个public字段都应使用@Option注释进行注释


  • Option注解参数说明

name:选项名称

defaultValue:默认值

abbrev:选项的单字符缩写

help:使用信息的帮助字符串

category:描述此选项所属的类别

allowMultiple:一个标志,指示是否应允许选项类型在单个选项列表中多次出现。 


import com.google.devtools.common.options.Option;
import com.google.devtools.common.options.OptionsBase;
import com.sun.tools.javac.util.List;

public class ServerOption extends OptionsBase {
@Option(
name = "help",
abbrev = 'h',
help = "Prints usage info",
defaultValue = "true"
)
public boolean help;

@Option(
name = "host",
abbrev = 'o',
help = "The server Host",
category = "startup",
defaultValue = ""
)
public String host;

@Option(
name = "port",
abbrev = 'p',
help = "The server Port",
category = "startup",
defaultValue = "8080"
)
public int port;

@Option(
name = "dir",
abbrev = 'd',
help = "Name of directory to serve static files",
category = "startup",
allowMultiple = true,
defaultValue = ""
)
public List<String> dirs;
}
Step2:在main方法中解析参数 
import com.google.devtools.common.options.OptionsParser;
import java.util.Collections;

public class Server {
//在main方法中解析参数
public static void main(String[] args) {
OptionsParser parser = OptionsParser.newOptionsParser(ServerOption.class);
parser.parseAndExitUponError(args);
ServerOption options = parser.getOptions(ServerOption.class);
//isEmpty() 检查字符串是否为空或空的示例 为空true
if (options.host.isEmpty() || options.port < 0 || options.dirs.isEmpty()) {
printUsage(parser);
return;
}

System.out.format("Starting server at %s:%d...\n", options.host, options.port);
for (String dirname : options.dirs) {
//format()格式化输出
System.out.format("\\--> Serving static files at <%s>\n", dirname);
}
}

private static void printUsage(OptionsParser parser) {
//输入java -jar server.jar OPTIONS; 运行jar文件
System.out.println("Usage: java -jar server.jar OPTIONS");
System.out.println(parser.describeOptions(Collections.<String, String>emptyMap(),
OptionsParser.HelpVerbosity.LONG));
}
}
测试结果
E:\Develop\jdk8\bin\java.exe "-javaagent:E:\Develop\idea2018-3\IntelliJ IDEA 2018.3.6\lib\idea_rt.jar=57407:E:\Develop\idea2018-3\IntelliJ IDEA 2018.3.6\bin" -Dfile.encoding=UTF-8 -classpath E:\Develop\jdk8\jre\lib\charsets.jar;E:\Develop\jdk8\jre\lib\deploy.jar;E:\Develop\jdk8\jre\lib\ext\access-bridge-64.jar;E:\Develop\jdk8\jre\lib\ext\cldrdata.jar;E:\Develop\jdk8\jre\lib\ext\dnsns.jar;E:\Develop\jdk8\jre\lib\ext\jaccess.jar;E:\Develop\jdk8\jre\lib\ext\jfxrt.jar;E:\Develop\jdk8\jre\lib\ext\localedata.jar;E:\Develop\jdk8\jre\lib\ext\nashorn.jar;E:\Develop\jdk8\jre\lib\ext\sunec.jar;E:\Develop\jdk8\jre\lib\ext\sunjce_provider.jar;E:\Develop\jdk8\jre\lib\ext\sunmscapi.jar;E:\Develop\jdk8\jre\lib\ext\sunpkcs11.jar;E:\Develop\jdk8\jre\lib\ext\zipfs.jar;E:\Develop\jdk8\jre\lib\javaws.jar;E:\Develop\jdk8\jre\lib\jce.jar;E:\Develop\jdk8\jre\lib\jfr.jar;E:\Develop\jdk8\jre\lib\jfxswt.jar;E:\Develop\jdk8\jre\lib\jsse.jar;E:\Develop\jdk8\jre\lib\management-agent.jar;E:\Develop\jdk8\jre\lib\plugin.jar;E:\Develop\jdk8\jre\lib\resources.jar;E:\Develop\jdk8\jre\lib\rt.jar;E:\Develop\Hadoop-Idea-Dev\example-hdfs\target\classes;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-common\3.1.4\hadoop-common-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-annotations\3.1.4\hadoop-annotations-3.1.4.jar;E:\Develop\jdk8\lib\tools.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\guava\guava\27.0-jre\guava-27.0-jre.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\guava\failureaccess\1.0\failureaccess-1.0.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\guava\listenablefuture\9999.0-empty-to-avoid-conflict-with-guava\listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;E:\Develop\apache-maven-3.6.1\repository\org\checkerframework\checker-qual\2.5.2\checker-qual-2.5.2.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\errorprone\error_prone_annotations\2.2.0\error_prone_annotations-2.2.0.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\codehaus\mojo\animal-sniffer-annotations\1.17\animal-sniffer-annotations-1.17.jar;E:\Develop\apache-maven-3.6.1\repository\commons-cli\commons-cli\1.2\commons-cli-1.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\commons\commons-math3\3.1.1\commons-math3-3.1.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\httpcomponents\httpclient\4.5.2\httpclient-4.5.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\httpcomponents\httpcore\4.4.4\httpcore-4.4.4.jar;E:\Develop\apache-maven-3.6.1\repository\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;E:\Develop\apache-maven-3.6.1\repository\commons-io\commons-io\2.5\commons-io-2.5.jar;E:\Develop\apache-maven-3.6.1\repository\commons-net\commons-net\3.6\commons-net-3.6.jar;E:\Develop\apache-maven-3.6.1\repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;E:\Develop\apache-maven-3.6.1\repository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-server\9.4.20.v20190813\jetty-server-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-http\9.4.20.v20190813\jetty-http-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-io\9.4.20.v20190813\jetty-io-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-util\9.4.20.v20190813\jetty-util-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-servlet\9.4.20.v20190813\jetty-servlet-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-security\9.4.20.v20190813\jetty-security-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-webapp\9.4.20.v20190813\jetty-webapp-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\org\eclipse\jetty\jetty-xml\9.4.20.v20190813\jetty-xml-9.4.20.v20190813.jar;E:\Develop\apache-maven-3.6.1\repository\javax\servlet\jsp\jsp-api\2.1\jsp-api-2.1.jar;E:\Develop\apache-maven-3.6.1\repository\com\sun\jersey\jersey-core\1.19\jersey-core-1.19.jar;E:\Develop\apache-maven-3.6.1\repository\javax\ws\rs\jsr311-api\1.1.1\jsr311-api-1.1.1.jar;E:\Develop\apache-maven-3.6.1\repository\com\sun\jersey\jersey-servlet\1.19\jersey-servlet-1.19.jar;E:\Develop\apache-maven-3.6.1\repository\com\sun\jersey\jersey-json\1.19\jersey-json-1.19.jar;E:\Develop\apache-maven-3.6.1\repository\org\codehaus\jettison\jettison\1.1\jettison-1.1.jar;E:\Develop\apache-maven-3.6.1\repository\com\sun\xml\bind\jaxb-impl\2.2.3-1\jaxb-impl-2.2.3-1.jar;E:\Develop\apache-maven-3.6.1\repository\org\codehaus\jackson\jackson-core-asl\1.9.2\jackson-core-asl-1.9.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\codehaus\jackson\jackson-mapper-asl\1.9.2\jackson-mapper-asl-1.9.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\codehaus\jackson\jackson-jaxrs\1.9.2\jackson-jaxrs-1.9.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\codehaus\jackson\jackson-xc\1.9.2\jackson-xc-1.9.2.jar;E:\Develop\apache-maven-3.6.1\repository\com\sun\jersey\jersey-server\1.19\jersey-server-1.19.jar;E:\Develop\apache-maven-3.6.1\repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;E:\Develop\apache-maven-3.6.1\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;E:\Develop\apache-maven-3.6.1\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;E:\Develop\apache-maven-3.6.1\repository\commons-beanutils\commons-beanutils\1.9.4\commons-beanutils-1.9.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\commons\commons-configuration2\2.1.1\commons-configuration2-2.1.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\commons\commons-lang3\3.4\commons-lang3-3.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;E:\Develop\apache-maven-3.6.1\repository\org\slf4j\slf4j-log4j12\1.7.25\slf4j-log4j12-1.7.25.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\avro\avro\1.7.7\avro-1.7.7.jar;E:\Develop\apache-maven-3.6.1\repository\com\thoughtworks\paranamer\paranamer\2.3\paranamer-2.3.jar;E:\Develop\apache-maven-3.6.1\repository\org\xerial\snappy\snappy-java\1.0.5\snappy-java-1.0.5.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\re2j\re2j\1.1\re2j-1.1.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\protobuf\protobuf-java\2.5.0\protobuf-java-2.5.0.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\code\gson\gson\2.2.4\gson-2.2.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\hadoop\hadoop-auth\3.1.4\hadoop-auth-3.1.4.jar;E:\Develop\apache-maven-3.6.1\repository\com\nimbusds\nimbus-jose-jwt\7.9\nimbus-jose-jwt-7.9.jar;E:\Develop\apache-maven-3.6.1\repository\com\github\stephenc\jcip\jcip-annotations\1.0-1\jcip-annotations-1.0-1.jar;E:\Develop\apache-maven-3.6.1\repository\net\minidev\json-smart\2.3\json-smart-2.3.jar;E:\Develop\apache-maven-3.6.1\repository\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\curator\curator-framework\2.13.0\curator-framework-2.13.0.jar;E:\Develop\apache-maven-3.6.1\repository\com\jcraft\jsch\0.1.55\jsch-0.1.55.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\curator\curator-client\2.13.0\curator-client-2.13.0.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\curator\curator-recipes\2.13.0\curator-recipes-2.13.0.jar;E:\Develop\apache-maven-3.6.1\repository\com\google\code\findbugs\jsr305\3.0.2\jsr305-3.0.2.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\htrace\htrace-core4\4.1.0-incubating\htrace-core4-4.1.0-incubating.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\zookeeper\zookeeper\3.4.13\zookeeper-3.4.13.jar;E:\Develop\apache-maven-3.6.1\repository\jline\jline\0.9.94\jline-0.9.94.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\yetus\audience-annotations\0.5.0\audience-annotations-0.5.0.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\commons\commons-compress\1.19\commons-compress-1.19.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-simplekdc\1.0.1\kerb-simplekdc-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-client\1.0.1\kerb-client-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerby-config\1.0.1\kerby-config-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-core\1.0.1\kerb-core-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerby-pkix\1.0.1\kerby-pkix-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerby-asn1\1.0.1\kerby-asn1-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerby-util\1.0.1\kerby-util-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-common\1.0.1\kerb-common-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-crypto\1.0.1\kerb-crypto-1.0.1.jar;E:\Develop\apache-maven-3.6.1\repository\org\apache\kerby\kerb-util\1.0.1\kerb-util-1.0.1.jar;