如何将Java程序转换为jar?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2887473/
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-13 14:03:39  来源:igfitidea点击:

How to convert Java program into jar?

javajar

提问by firestruq

A little help from you all... I was trying to convert a simple java program into jar but nothing seems to have happened. I have 2 files: Tester.java , Tester.Class. Then I used this command line:

大家的一点帮助......我试图将一个简单的java程序转换为jar,但似乎什么也没发生。我有 2 个文件: Tester.java 、 Tester.Class 。然后我使用了这个命令行:

jar -cvf Tester.jar Tester.class

jar -cvf Tester.jar Tester.class

The .jar file was created but nothing seems to work. What did I miss?

.jar 文件已创建,但似乎没有任何效果。我错过了什么?

采纳答案by aioobe

To run the program in the jar file you created you would need to execute

要在您创建的 jar 文件中运行程序,您需要执行

java -cp Tester.jar your.package.Main

A more convenient way to execute the jar is to be able to do

执行 jar 的一种更方便的方法是能够做到

java -jar Tester.jar

That, however, requires that you specify the main-class in a manifest file, that should be included in the jar-file:

但是,这要求您在清单文件中指定主类,该文件应包含在 jar 文件中:

  • Put

    Manifest-Version: 1.2
    Main-Class: your.package.Main
    

    in manifest.txt

  • And to create the jar:

    jar cvfm Tester.jar manifest.txt Tester.class
    
  • Manifest-Version: 1.2
    Main-Class: your.package.Main
    

    在 manifest.txt

  • 并创建罐子:

    jar cvfm Tester.jar manifest.txt Tester.class
    

回答by PeterMmm

If you wanted to run late this:

如果你想迟到:

java -jar Tester.jar

you should read this tutorial lesson.

你应该阅读本教程课程

回答by Matthew Flaschen

Your command will create a jar file. You may need to set the Main-Class manifest header.

您的命令将创建一个 jar 文件。您可能需要设置Main-Class manifest header

回答by Jonik

As Matthew Flaschen commentedanswered, you'll need to have a "manifest file" in your jar, and that should contain Main-Classheader pointing out which is the main class in the jar to execute. The answer by aioobeperfectly illustrates the simplest way to do this.

正如 Matthew Flaschen评论回答的那样,您的 jar 中需要有一个“清单文件”,该文件应包含Main-Class标头,指出哪个是 jar 中要执行的主类。aioobe 的回答完美地说明了执行此操作的最简单方法。

But instead of doing that always "manually", I'd recommend that you take a look at a build tool, such as Apache Ant(or Maven, but that's probably a bit harder to get started with), which are very commonly used to automate these kind of build sequences.

但是我建议您不要总是“手动”这样做,而是建议您查看构建工具,例如Apache Ant(或Maven,但上手可能有点困难),它们非常常用自动化这些类型的构建序列。

With Ant, you'd create a "buildfile" (most commonly named build.xml) like this:

使用 Ant,您将创建一个“构建文件”(最常命名为build.xml),如下所示:

<project name="Tester">
    <target name="build" 
            description="Compiles the code and packages it in a jar.">

        <javac srcdir="src" destdir="classes" source="1.6" target="1.6"/>

        <jar destfile="Tester.jar">
            <fileset dir="classes"/>
            <manifest>
                <attribute name="Main-Class" value="com.example.Tester"/>
            </manifest>
        </jar>
    </target>
</project>

Now, calling ant buildwould compile your code and package it in "Tester.jar", which will also contain the correct type of manifest header, so that you can run it with java -jar Tester.jar. (Note that this example assumes that your sources are in "src" directory, relative to where you run the command. You'll also need to have Ant installed of course.)

现在,调用ant build将编译您的代码并将其打包在“Tester.jar”中,其中还将包含正确类型的清单标头,以便您可以使用java -jar Tester.jar. (请注意,此示例假定您的源代码位于“src”目录中,相对于您运行命令的位置。当然,您还需要安装 Ant。)

If you do decide to try out Ant, its official manualwill be immensely useful (especially the list of Ant "tasks" which e.g. shows what options you can give to specific tasks like javacor jar).

如果您决定尝试使用 Ant,它的官方手册将非常有用(尤其是 Ant“任务”列表,例如显示您可以为特定任务(如javacjar)提供哪些选项)。

回答by Sajad Bahmani

I suggest use some IDE like Netbeans , Eclipse , IntelliJ IDEA and focus on your programming (build yor program by on click) .

我建议使用一些 IDE,如 Netbeans、Eclipse、IntelliJ IDEA 并专注于您的编程(通过单击构建您的程序)。