Linux 使用 Eclipse 创建一个 java 可执行文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5293321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:14:56  来源:igfitidea点击:

Create a java executable with Eclipse

javalinuxeclipsecompiler-constructionubuntu

提问by Micah

This is a totally newbie question. I'm running Eclipse on Ubuntu. I created a test project that I want to compile to an executable (whataver the linux equivalent is of a Windows .exe file). Here's the contents of my program:

这是一个完全新手的问题。我在 Ubuntu 上运行 Eclipse。我创建了一个测试项目,我想将其编译为可执行文件(Linux 等效项是 Windows .exe 文件)。这是我的程序的内容:

public class MyTest {
    public static void main(String[] args) {

        System.out.println("You passed in: " + args[0]);

    }
}

I want to know how to compile it and then how to execute it from the command line.

我想知道如何编译它,然后如何从命令行执行它。

Thanks!

谢谢!

采纳答案by adarshr

You need to create an executable JAR file. Steps will follow shortly.

您需要创建一个可执行的 JAR 文件。很快就会有步骤。

Right click on the project and select JAR file under Export.

右键单击项目并在导出下选择 JAR 文件。

enter image description here

在此处输入图片说明

Enter the path where you want it to be saved. The example here is of Windows. Change according to your platform.

输入要保存的路径。这里的例子是 Windows。根据您的平台进行更改。

enter image description here

在此处输入图片说明

Click Next.

点击下一步。

enter image description here

在此处输入图片说明

Edit the Main Class field by browsing and selecting the location of your class that contains the mainmethod.

通过浏览并选择包含该main方法的类的位置来编辑 Main Class 字段。

enter image description here

在此处输入图片说明

Run it

运行

To run the JAR file, open up a shell or command prompt and execute the following command:

要运行 JAR 文件,请打开 shell 或命令提示符并执行以下命令:

java -jar path/to/test.jar

java -jar path/to/test.jar

回答by whirlwin

In Eclipse, choose file then export, and you will have to choose runnable jar. Also, you will be prompted to select the main class, MyTest in your case.

在 Eclipse 中,选择文件然后导出,您将不得不选择可运行的 jar。此外,系统将提示您选择主类 MyTest 在您的情况下。

回答by Bit

The Eclipes tutorials are very helpful, if you do the "create a Hello World application" tutorial it will walk you through the process of setup the project, building the app and running the jar file.

Eclipes 教程非常有用,如果您执行“创建 Hello World 应用程序”教程,它将引导您完成设置项目、构建应用程序和运行 jar 文件的过程。

回答by Stephen C

I want to know how to compile it ...

我想知道怎么编译...

See other answers on how to get Eclipse to create a JAR file.

请参阅有关如何让 Eclipse 创建 JAR 文件的其他答案。

... and then how to execute it from the command line.

...然后如何从命令行执行它。

In the simple case, you execute it by running java -jar yourApp.jar <args>.

在简单的情况下,您通过运行来执行它java -jar yourApp.jar <args>

If your application depends on external libraries, then it is a bit more complicated ...

如果您的应用程序依赖于外部库,那么它有点复杂...

how come I would choose jar file over executable jar file?

为什么我会选择 jar 文件而不是可执行的 jar 文件?

  • Because a JAR file is portable, and a binary executable is not.
  • Because JIT compiled code runs faster than code that is compiled ahead of time.
  • Because the standard Java tool chain does not support creation of binary executables. Ditto for Eclipse, AFAIK.
  • 因为 JAR 文件是可移植的,而二进制可执行文件则不是。
  • 因为 JIT 编译的代码比提前编译的代码运行得更快。
  • 因为标准 Java 工具链不支持创建二进制可执行文件。Eclipse 同上,AFAIK。

回答by charlie schindler

Marked solution does not work. Use "runnable" jar instead and then java -jar <jarfile>

标记的解决方案不起作用。改用“可运行”jar,然后java -jar <jarfile>