Java Eclipse executable jar file

守給你的承諾、 提交于 2019-12-12 04:35:33

问题


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


回答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.




回答2:


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



回答3:


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.




回答4:


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/



来源:https://stackoverflow.com/questions/3570845/java-eclipse-executable-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!