简体   繁体   English

如何从Java应用程序编译和创建JAR?

[英]How do I compile and create JAR from a java application?

I'm creating an application to generate Java applications from templates. 我正在创建一个从模板生成Java应用程序的应用程序。 I'd like to call the java compiler and later the jar to create the jar package. 我想调用java编译器,然后调用jar来创建jar包。 What's the best way to do that? 最好的方法是什么?

Quick example: My application will generate the file myApp.java based on the myTemplate.java file. 快速示例:我的应用程序将基于myTemplate.java文件生成文件myApp.java。 My application will replace a lot of place holders in the template with generated java code. 我的应用程序将使用生成的java代码替换模板中的许多占位符。 I'd like to run, from my application the following commands: 我想从我的应用程序运行以下命令:

javac *.java
jar -cvf myApp.jar *.class (I know the command is not right).

I'd like to be able to find out if the compilation failed and/or the jar creation failed. 我希望能够找出编译失败和/或jar创建失败。

This is something like what eclipse does, but much simpler, of couse. 这就像日食所做的那样,但更为简单。

Thanks very much for your help. 非常感谢您的帮助。

Java provides Compiler API that can be used to compile java classes programmatically. Java提供了编译器API ,可用于以编程方式编译java类。

Here is the link to Good Tutorial using Compiler API 以下是使用Compiler API的Good Tutorial的链接

You can use JarOutputStream to create jars programmatically. 您可以使用JarOutputStream以编程方式创建jar。 Here is a nice SO Post for Creating Jars using JarOutputStream 这是一个很好的使用JarOutputStream创建JAR的SO帖子

Hope this helps. 希望这可以帮助。

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

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