views:

133

answers:

3

Hi All! 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

Thanks in advance!

+2  A: 

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.

Serplat
Hi, Thanks for your reply. I tried your solution. However, I am not expert to make such supper main class. can you please provide pseudo code?
+1  A: 

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.

Bill K
A: 

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
Harsha Hulageri