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( method). How can I achieve this?

My application main class

    public static void main(String[] args) {

        //if entered the command run in the command line
        //, args);

        //if entered the command create in the command line
        MigrationGenerator generator=new MigrationGenerator();
        try {
        } 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. So your code would look something like:

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

or you could use a switch statement as well.

Leave a Reply

Your email address will not be published. Required fields are marked *