[英]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:您可以改为:
<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。
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。
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.