0

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.

Marcus
  • 1,675
  • 3
  • 18
  • 29
  • Maven Archetypes already does provide a mechanism to create Java applications from templates. Why reinvent the wheel? – Puce Feb 17 '14 at 15:30
  • [how-do-i-programmatically-compile-and-instantiate-a-java-class](http://stackoverflow.com/questions/2946338/how-do-i-programmatically-compile-and-instantiate-a-java-class) - needs a JDK. – Joop Eggen Feb 17 '14 at 15:34
  • @Puce: not using Maven would be a good reason not to be using Maven archetypes. – Gimby Feb 17 '14 at 15:46
  • @Gimby AFAIK, you only need Maven to use the Maven Archetypes, but the generated content doesn't have to depend on Maven. And I guess there are similar constructs like Maven Archetyes for the other popular build tools? – Puce Feb 17 '14 at 15:52
  • @Puce I would highly doubt that you apply Maven archetypes but then not apply Maven for your actual build and project management though. In any case I was just answering your question. I'm sure there are hundreds of code generation tools out there. Heck, you could use the more complex templating engines probably. – Gimby Feb 17 '14 at 15:57
  • @Gimby And my question was more about if the OP is aware that such tools exist. :-) – Puce Feb 17 '14 at 16:01
  • Maven doesn't provide me any help on this. I'm creating applications from multiple templates. I just used one in the example to make things simpler. Each template is a base for a part of the application. My only concern here is how to evoke the compiler. Should I use something like "system()" in C? Should I use something close to fork in C? Basically, I need to be able to compile a whole application that is in a directory. Doesn't matter if I used templates or not. – Marcus Feb 17 '14 at 16:46

1 Answers1

2

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.

Community
  • 1
  • 1
Sanjeev
  • 9,876
  • 2
  • 22
  • 33
  • Thanks for your help. I can't open this link here (it is blocked). As soon as I get home, I'll take a look at that. Thanks again. – Marcus Feb 17 '14 at 17:54
  • That's exactly what I needed. It is working perfectly. Thanks very much. – Marcus Feb 17 '14 at 20:13