【问题标题】:Java- Call a method from the command lineJava-从命令行调用方法
【发布时间】:2021-06-02 00:35:29
【问题描述】:

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

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

我的应用主类

    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

【问题讨论】:

  • 使用 arg 并解析它们...过于简单的例子 if(args[1].equals("generate")){ /* do generate code */ }else{ SpringApplication.run(MigrationsApplication.class, args); } 或者你可以用另一种方式来做,如果参数 gui 被传递,则转到 GUI。对于操作系统而言,GUI 应用程序和 CLI 应用程序之间没有区别,只是 GUI 应用程序调用绘制 GUI,因为它运行 CLI 没有,应用程序被调用而不是从 CLI 或 GUI 调用跨度>
  • 我真的从没用过命令行,所以完整的命令应该是 java ApplicationName args 吧?但是,假设我想在使用 dotnet 命令的地方做类似 .Net 的事情。是否可以添加我想要的前缀(前缀我的意思是dotnet)
  • 我会在问题中留下一个例子

标签: java command-line command


【解决方案1】:

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

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

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

【讨论】:

  • 使用这种方式我可以为创建方法添加一个“子命令”。比如文件名?
【解决方案2】:

您可以使用系统参数,并且由于您在项目中使用 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

【讨论】:

  • 如果我想在创建命令中添加文件名怎么办?
  • 我也可以简化命令吗?而不是做 java -jar.... 我只能做例如 applicationName run
  • create case 中使用@Value 注释并在那里传递文件名。例如``` case "create": @Value("${file.name}") final String fileName; // 代码 ```
【解决方案3】:

所以这是一个示例,展示了如何实现它,但请注意,我从不检查 args 的确切位置,无用的参数不应破坏应用程序的运行。

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

E.G 预计会起作用 java -jar MyApp.jar some crap generate 参数 generate 在那里,应该得到尊重。

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 ... case 语句,但我强烈建议您默认不支持您的 run 参数,它属于GUI模式

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多