简体   繁体   中英

JAVA, JVM, Multiple Main methods

I have two classes where each class has one main method, the main class where it will become the entry point is the RunPattern class. The question is, which method in DataCreator class will be called first when DataCreator.serialize(fileName) is called in the RunPattern class ? is it the main() method or serialize()? Is it different than static scope?

    import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import java.io.ObjectOutputStream;
import java.io.FileOutputStream;


public class RunPattern {

    public static void main(String args[]){``// The first main() method  

        if(!new File("data.ser").exists()){

            DataCreator.serialize("data.ser");

        }

    }

}

    class DataCreator {

    private static final String DEFAULT_FILE = "data.ser";

    public static void main(String args[]){ //  the second  main 

        String fileName;
        if(args.length == 1){
            fileName = args[0];
        }else{
            fileName = DEFAULT_FILE;
        }
        serialize(fileName);

    }

    public static void serialize(String fileName){

        try{

            serializeToFile(createData(), fileName);


        }catch(IOException exc){
            exc.printStackTrace();
        }


    }

    private static Serializable createData(){

        ToDoListCollection data = new ToDoListCollectionImpl();
        ToDoList listOne = new ToDoListImpl();
        ToDoList listTwo = new ToDoListImpl();
        ToDoList listThree = new ToDoListImpl();
        listOne.setListName("Daily Routine");
        listTwo.setListName("Programmer Hair Washing Product");
        listThree.setListName("Reading List");
        listOne.add("get up");
        listOne.add("Brew cuppa java");
        listOne.add("Read JVM Times");
        listTwo.add("Latter");
        listTwo.add("Rinse");
        listTwo.add("Repeat");
        listTwo.add("eventually throw too much conditioner");
        listThree.add("the complete of duke");
        listThree.add("how green was java");
        listThree.add("URL, sweet URL" );
        data.add(listOne);
        data.add(listTwo);
        data.add(listThree);
        return data;


    }

    public static void serializeToFile(Serializable data, String fileName) throws IOException {

        ObjectOutputStream serOut = new ObjectOutputStream(new FileOutputStream(fileName));
        serOut.writeObject(data);
        serOut.close();
    }

}

Well. If you run your program with

java -cp ... RunPattern

then the main method of your RunPattern class is invoked. This in turn calls

DataCreator.serialize("data.ser");

which simply invokes the method serialize of your DataCreator class. The main method in this class is not involved.

The main method of DataCreator will never be called this way.

Some info about setting the main method in the Manifest-File
Since you are usually developing within an IDE, this is done for you automaticially.
But you can choose which mainmethod is your entrypoint in the Runconfiguration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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