Spring Boot的自动配置、Command-line Runner

https://www.jianshu.com/p/846bb2d26ff8

Apache Commons CLI命令行启动

https://www.cnblogs.com/xing901022/p/5612328.html

package hangout.study;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

public class CLITest {
    public static void main(String[] args) {
        String[] arg = { "-h", "-c", "config.xml" };
        testOptions(arg);
    }
    public static void testOptions(String[] args) {
        Options options = new Options();
        Option opt = new Option("h", "help", false, "Print help");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("c", "configFile", true, "Name server config properties file");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("p", "printConfigItem", false, "Print all config item");
        opt.setRequired(false);
        options.addOption(opt);

        HelpFormatter hf = new HelpFormatter();
        hf.setWidth(110);
        CommandLine commandLine = null;
        CommandLineParser parser = new PosixParser();
        try {
            commandLine = parser.parse(options, args);
            if (commandLine.hasOption('h')) {
                // 打印使用帮助
                hf.printHelp("testApp", options, true);
            }

            // 打印opts的名称和值
            System.out.println("--------------------------------------");
            Option[] opts = commandLine.getOptions();
            if (opts != null) {
                for (Option opt1 : opts) {
                    String name = opt1.getLongOpt();
                    String value = commandLine.getOptionValue(name);
                    System.out.println(name + "=>" + value);
                }
            }
        }
        catch (ParseException e) {
            hf.printHelp("testApp", options, true);
        }
    }
}

首先来参考下打jar包的文档:

https://jingyan.baidu.com/article/546ae1853f71a91149f28c85.html

关于MANIFEST.MF详解。主要是Class-Path引入外包jar包,参考文档 如下:

Class-Path: ext/mail.jar ext/activation.jar

https://blog.csdn.net/csdner999/article/details/39232115

通过 OneJar 或 Maven 打包后 jar 文件,用命令:
java -jar ****.jar
执行后总是运行指定的主方法,如果 jar 中有多个 main 方法,那么如何运行指定的 main 方法呢?
用下面的命令试试看:

java -classpath ****.jar ****.****.className [args]

“****.****”表示“包名”;
“className”表示“类名”;
“[args]”表示传入的参数;

直接运行 MANIFEST.MF 中指定的 main 方法:
java -jar mplus-service-jar-with-dependencies.jar

运行指定的 main 方法:
java -cp mplus-service-jar-with-dependencies.jar com.smbea.dubbo.bin.Console start

 //键盘录入一个字符串
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String s = sc.nextLine();
        

你可能感兴趣的:(Spring Boot的自动配置、Command-line Runner)