Question

I'm creating an application to generate Java applications from templates. I'd like to call the java compiler and later the jar to create the jar package. What's the best way to do that?

Quick example: My application will generate the file myApp.java based on the myTemplate.java file. My application will replace a lot of place holders in the template with generated java code. 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.

This is something like what eclipse does, but much simpler, of couse.

Thanks very much for your help.

Was it helpful?

Solution

Java provides Compiler API that can be used to compile java classes programmatically.

Here is the link to Good Tutorial using Compiler API

You can use JarOutputStream to create jars programmatically. Here is a nice SO Post for Creating Jars using JarOutputStream

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top