简体   繁体   English

如何从 .java 文件制作 JAR?

[英]How do I make a JAR from a .java file?

I was writing a simple program using a Java application (not application that has projects, but application within a project; .java ) that has a single frame.我正在使用具有单个框架的 Java 应用程序(不是具有项目的应用程序,而是项目中的应用程序; .java )编写一个简单的程序。 Both of the files are .java so I can't write a manifest needed by the JAR.这两个文件都是.java,所以我无法编写 JAR 所需的清单

The MyApp.java starts like a class with package, imports then public class MyApp and has a main function, but it's still .java file! MyApp.java就像一个带包的类,然后导入公共类MyApp并有一个 main 函数,但它仍然是 .java 文件! I'm writing it in JDeveloper 11g if it helps.如果有帮助,我将在 JDeveloper 11g 中编写它。

Any ideas how to make a JAR from these files?任何想法如何从这些文件制作 JAR?

Open a command prompt.打开命令提示符。

Go to the directory where you have your .java files转到您拥有 .java 文件的目录

Create a directory build创建目录构建

Run java compilation from the command line从命令行运行 java 编译

javac -d ./build *.java

if there are no errors, in the build directory you should have your class tree如果没有错误,在构建目录中你应该有你的类树

move to the build directory and do a移动到构建目录并执行

jar cvf YourJar.jar *

For adding manifest check jar command line switches用于添加清单检查 jar 命令行开关

Simply with command line:只需使用命令行:

javac MyApp.java
jar -cf myJar.jar MyApp.class

Sure IDEs avoid using command line terminal确保 IDE 避免使用命令行终端

Ok this is the solution I would have liked to find, instead here I write it:好的,这是我希望找到的解决方案,而不是在这里我写:

First create the directory structure corresponding to the package defined for the .java file, if it is my.super.application create the directory "my" and inside it "super" and inside it the .java file "App.java"首先创建与为 .java 文件定义的包相对应的目录结构,如果是 my.super.application 创建目录“my”,在它里面创建“super”,在里面创建 .java 文件“App.java”

then from command line:然后从命令行:

   javac -cp /path/to/lib1.jar:/path/to/lib2.jar path/to/my/super/App.java

Notice the above will include multiple libraries, if under windows use "," to separate multiple files otherwise under GNU/Linux use ":" To create a jar file注意上面会包含多个库,windows下用","分隔多个文件,GNU/Linux下用":"创建jar文件

   jar -cvfe App.jar App my/app/

the above will create the application with its corresponding Manifest indicating the App as the main class.以上将创建应用程序,其相应的清单指示应用程序作为主类。

Including the required libraries inside the jar file is not possible using java or jar command line parameters.使用 java 或 jar 命令行参数无法在 jar 文件中包含所需的库。

You can instead:您可以改为:

  1. manually extract libraries to the root folder of the jar file手动将库解压到 jar 文件的根文件夹
  2. use an IDE such as Netbeans and insert a rule inside post-jar section of nbproject/build-impl.xml to extract the libraries inside the jar.使用诸如 Netbeans 之类的 IDE,并在 nbproject/build-impl.xml 的 post-jar 部分插入规则以提取 jar 中的库。 See below.见下文。
 <target name="-post-jar"> <!-- Empty placeholder for easier customization. --> <!-- You can override this target in the ../build.xml file. --> <jar jarfile="${dist.jar}" update="true"> <zipfileset src="${dist.jar}" includes="**/*.class" /> <zipfileset src="${file.reference.iText-1.0.8.jar}" includes="**/*"/> <zipfileset src="${file.reference.itextpdf-3.2.1.jar}" includes="**/*"/> </jar> </target>

the file.reference names are found inside project.properties file after you added the libraries to the Netbeans IDE.将库添加到 Netbeans IDE 后,在 project.properties 文件中可以找到 file.reference 名称。

Often you will want to specify a manifest, like so:通常你会想要指定一个清单,像这样:

jar -cvfm myJar.jar myManifest.txt myApp.class

Which reads: "create verbose jarFilename manifestFilename", followed by the files you want to include.其内容为:“创建详细的 jarFilename manifestFilename”,后跟要包含的文件。 Verbose means print messages about what it's doing. Verbose 意味着打印关于它正在做什么的消息。

Note that the name of the manifest file you supply can be anything, as jar will automatically rename it and put it into the right directory within the jar file.请注意,您提供的清单文件的名称可以是任何名称,因为jar会自动重命名并将其放入 jar 文件中的正确目录中。

This can be done without terminal, directly from IDE.这可以在没有终端的情况下直接从 IDE 完成。 Netbeans, for example.例如,Netbeans。

  1. Create a separate project with packages (Create Project - Java - Java Class Library).使用包创建一个单独的项目(创建项目 - Java - Java 类库)。
  2. Put your .java classes there.把你的 .java 类放在那里。
  3. Build this project.构建这个项目。
  4. Go to your project folder and find build and dist folders there.转到您的项目文件夹并在那里找到 build 和 dist 文件夹。
  5. Find .jar file in your dist folder.在 dist 文件夹中找到 .jar 文件。
  6. Get your other project and add this .jar file to project libraries.获取您的其他项目并将此 .jar 文件添加到项目库中。
  7. You can now reference classes from this library and its methods directly from code, if import is automatically done for you.如果导入是自动为您完成的,您现在可以直接从代码中引用该库中的类及其方法。

Here is another fancy way of doing this:这是执行此操作的另一种奇特方式:

$ ls | grep .java | xargs -I {} javac {} ; jar -cf myJar.jar *.class

Which will grab all the .java files ( ls | grep .java ) from your current directory and compile them into .class ( xargs -I {} javac {} ) and then create the jar file from the previously compiled classes ( jar -cf myJar.jar *.class ).这将从当前目录中获取所有 .java 文件( ls | grep .java )并将它们编译成 .class ( xargs -I {} javac {} ),然后从以前编译的类( jar -cf myJar.jar *.class )创建 jar 文件jar -cf myJar.jar *.class )。

Perhaps the most beginner-friendly way to compile a JAR from your Java code is to use an IDE (integrated development environment; essentially just user-friendly software for development) like Netbeans or Eclipse.从 Java 代码编译 JAR 的最适合初学者的方法可能是使用 IDE(集成开发环境;本质上只是用户友好的开发软件),如 Netbeans 或 Eclipse。

  • Install and set-up an IDE.安装并设置 IDE。 Here is the latest version of Eclipse .这是 Eclipse 的最新版本
  • Create a project in your IDE and put your Java files inside of the project folder.在您的 IDE 中创建一个项目并将您的 Java 文件放在项目文件夹中。
  • Select the project in the IDE and export the project as a JAR.在 IDE 中选择项目并将项目导出为 JAR。 Double check that the appropriate java files are selected when exporting.在导出时仔细检查是否选择了适当的 java 文件。

You can always do this all very easily with the command line.您始终可以使用命令行轻松完成所有这些操作。 Make sure that you are in the same directory as the files targeted before executing a command such as this:在执行如下命令之前,请确保您与目标文件位于同一目录中:

javac YourApp.java
jar -cf YourJar.jar YourApp.class

...changing "YourApp" and "YourJar" to the proper names of your files, respectively. ...分别将“YourApp”和“YourJar”更改为文件的正确名称。

I was writing a simple program using a Java application (not application that has projects, but application within a project; .java ) that has a single frame.我正在使用具有单个框架的Java应用程序(不是具有项目的应用程序,而是项目中的应用程序; .java )编写一个简单的程序。 Both of the files are .java so I can't write a manifest needed by the JAR.这两个文件都是.java,因此我无法编写JAR所需的清单

The MyApp.java starts like a class with package, imports then public class MyApp and has a main function, but it's still .java file! MyApp.java像带有包的类一样开始,然后导入公共类MyApp并具有主要功能,但它仍然是.java文件! I'm writing it in JDeveloper 11g if it helps.如果有帮助,我正在JDeveloper 11g中编写它。

Any ideas how to make a JAR from these files?有什么想法如何从这些文件制作JAR吗?

If you have one simple main class(let's say Count.class), and want a jar with a MANIFEST file that points to this class as the entry point, you can do this:如果您有一个简单的主类(比方说 Count.class),并且想要一个带有指向此类作为入口点的 MANIFEST 文件的 jar,您可以这样做:

jar cfe app.jar Count Count.class 

Source: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html来源: https : //docs.oracle.com/javase/tutorial/deployment/jar/appman.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM