简体   繁体   English

Java-从命令行调用方法

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

I want to be able to use my application through the command line.我希望能够通过命令行使用我的应用程序。 More specifically the behavior that I want to implement is this one:更具体地说,我想要实现的行为是这样的:

If in the command line I enter the command create I want the method generateMigrationFile to be called, else if the command I entered was run I want to run my migrations( SpringApplication.run method).如果在命令行中输入命令 create 我希望调用方法generateMigrationFile ,否则如果我输入的命令已运行我想运行我的迁移( SpringApplication.run方法)。 How can I achieve this?我怎样才能做到这一点?

My application main class我的应用程序主要 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");
        }

I want to call it like:我想这样称呼它:

migrationsApp run

migrationsApp create

The arguments you supply when executing the command line are passed on to the args parameter.您在执行命令行时提供的 arguments 将传递给args参数。 So your code would look something like:所以你的代码看起来像:

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

or you could use a switch statement as well.或者您也可以使用 switch 语句。

You can use system argument and since you are using spring in your project you can use @Value annotation for example:您可以使用系统参数,并且由于您在项目中使用 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");
        }
}

Pass the argument as --command in command line.在命令行中将参数作为--command传递。 If you are running jar use command java -jar --command=run如果您正在运行 jar 使用命令 java -jar --command=run

So this is an example that shows an idea of how it can be implemented but note I'm never checking args at an exact position, useless arguments should not break the application running.因此,这是一个示例,它显示了如何实现它的想法,但请注意,我从来没有在确切的 position 上检查 args,无用的 arguments 不应破坏应用程序的运行。

You will also notice in my example if no argument is passed it assume GUI mode, good practice so if people double click on your.JAR it loads in GUI mode.在我的示例中,您还会注意到,如果没有传递任何参数,它会采用 GUI 模式,这是一种很好的做法,因此如果人们双击 your.JAR,它会以 GUI 模式加载。 if this was CLI only app then yeah forcing an argument or arguments is fine but if you have a GUI mode use that mode as the default unless overridden by the user.如果这是仅限 CLI 的应用程序,那么是的,强制参数或 arguments 很好,但如果您有 GUI 模式,则使用该模式作为默认模式,除非被用户覆盖。

EG expected to work java -jar MyApp.jar some crap generate the argument generate is there and it should be honored. 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");
                }
        }
}

I'm using an If because it's a simple demo with only 1 custom argument if you want to support more switch it to a switch... case statement but I would highly recommend that your run argument is not supported and by default, it falls to GUI mode我正在使用 If 因为它是一个简单的演示,只有 1 个自定义参数,如果你想支持更多 switch 它到switch... case语句,但我强烈建议你的run参数不受支持,默认情况下,它属于到 GUI 模式

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

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