0

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

2
  • How would you determine which class to run? Commented Aug 25, 2010 at 22:57
  • If I can pass argument at runtime to tell the class name. Is it possible? I am sorry I am not java wizard :( Commented Aug 25, 2010 at 23:21

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

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

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.

Comments

1

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/

1 Comment

This link seems to fail? If there an alternative link you can provide Aram?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.