简体   繁体   中英

Java Eclipse executable jar file

I am new to eclipse + Java. I am trying to create executable jar file with eclipse export option. It works very well. But in my project, I have almost 10 packages (my own) and 4 main classes. I want to create a executable jar file that can execute any of main class from 4 main classes.

For example: Double click write class name and run that class

Dont use executable jar. Instead create a normal jar which will have compiled classes.

From command line, call whichever main class you want to call as a argument to the java jar command.

java -jar test.jar com.company.unit.MainClass1

java -jar test.jar com.company.unit.MainClass2

Executable JARs don't work that way. They write a manifest file in the JAR that declares where the main class is, and it runs that one. You would have to create 4 different JARs.

Alternatively, you can just create one main class that lets you type in which of the four you want, and then have it execute that one. Basically, you'd be mimicking the functionality that you are looking for on your own.

Just a quick example of how to deal with command line options to launch different things, I would have put it into a reply to @serplat's answer but then I can't format it.

public static void main(String[] args)
{
    if(args.length == 0) {
        // Do default here--no options specified
    } else if(args.length > 2) {
        // Complain that there are too many args, or implement multi-args
    } else // known just one arg
       if(args[1].equals("option1") {
           // call the main of your first app
       } else if(args[1].equals("option2") {
           // start your second app
      ...
   }
}

There are much better ways to handle command line stuff, but this is understandable and should do what you need. Later you might look into something more flexible.

I have recently made a tutorial that shows you how to create an executable jar file that will run with a double click in Windows, Mac OSX and Linux. For my project, I packaged a slick library game I had made. Hope it helps.

http://aramk.com/blog/2010/12/05/how-to-make-a-multi-platform-executable-java-jar-file/

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