Java Eclipse 可执行 jar 文件

发布于 2024-09-16 04:41:10 字数 163 浏览 5 评论 0原文

我是 eclipse + Java 的新手。我正在尝试使用 eclipse 创建可执行 jar 文件 导出选项。它运作得很好。但在我的项目中,我有近 10 个包(我自己的)和 4 个主要类。我想创建一个可执行的 jar 文件,它可以执行 4 个主类中的任何一个主类。

例如:双击写入类名并运行该类

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

-柠檬树下少年和吉他 2024-09-23 04:41:10

不要使用可执行 jar。相反,创建一个普通的 jar,其中包含已编译的类。

从命令行,调用您想要作为 java jar 命令的参数调用的主类。

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

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

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
只等公子 2024-09-23 04:41:10

可执行 JAR 不是这样工作的。他们在 JAR 中编写一个清单文件,声明主类所在的位置,并运行该文件。您必须创建 4 个不同的 JAR。

或者,您可以只创建一个主类,让您键入所需的四个主类中的哪一个,然后让它执行该主类。基本上,您会自己模仿您正在寻找的功能。

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.

江挽川 2024-09-23 04:41:10

只是一个如何处理命令行选项来启动不同东西的简单示例,我会将其放入对 @serplat 答案的回复中,但随后我无法格式化它。

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
      ...
   }
}

有很多更好的方法来处理命令行内容,但这是可以理解的并且应该满足您的需要。稍后您可能会考虑一些更灵活的东西。

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.

岁月蹉跎了容颜 2024-09-23 04:41:10

我最近制作了一个教程,向您展示如何创建一个可执行的 jar 文件,该文件将在 Windows、Mac OSX 和 Linux 中双击运行。对于我的项目,我打包了一个我制作的漂亮的库游戏。希望有帮助。

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文