如何使用源代码(.java 文件)创建一个 java jar 文件?

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

how to create a java jar file with source code (.java files)?

javajarcompilation

提问by TonyGW

I'm trying to submit my java homework,and would like to create a jar file that contains the compiled .class files as well as my original source code .java files. I'm using IntelliJ, but didn't find an option of exporting .java to jar. Is there a way to do it by commandlines?

我正在尝试提交我的 java 作业,并想创建一个 jar 文件,其中包含已编译的 .class 文件以及我的原始源代码 .java 文件。我正在使用 IntelliJ,但没有找到将 .java 导出到 jar 的选项。有没有办法通过命令行来做到这一点?

采纳答案by Prateek

Creating a jar file using IntelliJ

使用 IntelliJ 创建 jar 文件

For Intellij IDEA version 11.0.2

对于 Intellij IDEA 版本 11.0.2

File | Project Structure | Artifacts then you should press alt+insert or click the plus icon and create new artifact choose --> jar --> From modules with dependencies.

档案 | 项目结构 | Artifacts 然后您应该按 alt+insert 或单击加号图标并创建新的工件选择 --> jar --> From modules with dependencies。

Next goto Build | Build artifacts --> choose your artifact.

下一步转到构建 | 构建工件 --> 选择您的工件。

Refer this link create-jar-with-IntelliJ

请参阅此链接create-jar-with-IntelliJ

Creating a jar File in Command Prompt

在命令提示符中创建 jar 文件

 - Start Command Prompt.

 - Navigate to the folder that holds your class files:

    C:\>cd \mywork

 - Set path to include JDK's bin.  For example:

    C:\mywork> path c:\Program Files\Java\jdk1.7.0_25\bin;%path%

 - Compile your class(es):

    C:\mywork> javac *.java

 - Create a manifest file

   manifest.txt with, for example, this content:

        Manifest-Version: 1.0
        Created-By: 1.8.0_171 (Oracle Corporation) #your jdk version
        Main-Class: mainPackage.MainClass

 - Create the jar file:

    C:\mywork> jar cvfm Craps.jar manifest.txt *.class

回答by Hariharan

You can achieve through command line, but i suggest you to go for build script, which is professional way of doing so.

您可以通过命令行实现,但我建议您使用构建脚本,这是这样做的专业方法。

Try Ant Scriptto achieve your requirement.

努力Ant Script达到你的要求。

Tutorial link for Ant - http://www.tutorialspoint.com/ant/ant_creating_jar_files.htm

Ant 的教程链接 - http://www.tutorialspoint.com/ant/ant_creating_jar_files.htm

回答by upog

回答by Manish Doshi

In Eclipse Help contents, expand "Java development user guide" ==> "Tasks" ==> "Creating JAR files." Follow the instructions for "Creating a new JAR file" or "Creating a new runnable JAR file."

在 Eclipse 帮助内容中,展开 "Java development user guide" ==> "Tasks" ==> "Creating JAR files." Follow the instructions for "Creating a new JAR file" or "Creating a new runnable JAR file."

The JAR File and Runnable JAR File commands are for some reason located under the File menu: click on Export... and expand the Java node.

JAR File 和 Runnable JAR File 命令出于某种原因位于 File 菜单下:单击 Export... 并展开 Java 节点。

Refer this: http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

请参阅:http: //docs.oracle.com/javase/tutorial/deployment/jar/build.html