繁体   English   中英

Java-从命令行调用方法

[英]Java- Call a method from the command line

我希望能够通过命令行使用我的应用程序。 更具体地说,我想要实现的行为是这样的:

如果在命令行中输入命令 create 我希望调用方法generateMigrationFile ,否则如果我输入的命令已运行我想运行我的迁移( SpringApplication.run方法)。 我怎样才能做到这一点?

我的应用程序主要 class

    public static void main(String[] args) {

        //if entered the command run in the command line
        //SpringApplication.run(MigrationsApplication.class, args);

        //if entered the command create in the command line
        MigrationGenerator generator=new MigrationGenerator();
        try {
            generator.generateMigrationFile("TEST");
        } catch (IOException e) {
            System.out.println("There was an error generating the file");
        }

我想这样称呼它:

migrationsApp run

migrationsApp create

您在执行命令行时提供的 arguments 将传递给args参数。 所以你的代码看起来像:

If (args.length == 0){
  System.exit(-1);
}
if (args[0].equals("run")){
// do run
} else if (args[0].equals("create"){
// do create
}

或者您也可以使用 switch 语句。

您可以使用系统参数,并且由于您在项目中使用 spring 您可以使用@Value注释,例如:

public static void main(String[] args) {

        @Value("${command}")
        final String command;
        switch(command) {
          case "run":
            SpringApplication.run(MigrationsApplication.class, args);
            break;
          case "create":
             MigrationGenerator generator=new MigrationGenerator();
             try {
                generator.generateMigrationFile("TEST");
             } catch (IOException e) {
               System.out.println("There was an error generating the file");
             }
             break;
          default:
            throw new IllegalArgumentException("Command is Invalid");
        }
}

在命令行中将参数作为--command传递。 如果您正在运行 jar 使用命令 java -jar --command=run

因此,这是一个示例,它显示了如何实现它的想法,但请注意,我从来没有在确切的 position 上检查 args,无用的 arguments 不应破坏应用程序的运行。

在我的示例中,您还会注意到,如果没有传递任何参数,它会采用 GUI 模式,这是一种很好的做法,因此如果人们双击 your.JAR,它会以 GUI 模式加载。 如果这是仅限 CLI 的应用程序,那么是的,强制参数或 arguments 很好,但如果您有 GUI 模式,则使用该模式作为默认模式,除非被用户覆盖。

EG 预计可以工作java -jar MyApp.jar some crap generate参数 generate is there 并且应该兑现。

public static void main(String[] args) {
        boolean showGui = true;
        boolean generate = false;

        for (String arg : args){
                if(arg.equals("generate")){
                        showGui = false;
                        generate = true;
                }
        }

        //if entered the command run in the command line
        if(showGui){
                SpringApplication.run(MigrationsApplication.class, args);
        }

        //if entered the command create in the command line
        if(generate){
                MigrationGenerator generator=new MigrationGenerator();
                try {
                    generator.generateMigrationFile("TEST");
                } catch (IOException e) {
                    System.out.println("There was an error generating the file");
                }
        }
}

我正在使用 If 因为它是一个简单的演示,只有 1 个自定义参数,如果你想支持更多 switch 它到switch... case语句,但我强烈建议你的run参数不受支持,默认情况下,它属于到 GUI 模式

暂无
暂无

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

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