Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a runnable jar file from source code programmatically?

I need to create runnable .jar file programmatically from a string. My decision is to create a .class file from string and add it to the .jar using JarOutputStream.

  1. What API must I use to create the .class file?
  2. Are there any other solutions to create a .jar from the source code?
like image 770
Anton Putov Avatar asked Apr 19 '12 22:04

Anton Putov


People also ask

How do I create a runnable JAR file?

To export your project, right-click it and select Export. Select Java > Runnable JAR file as the export destination and click Next. On the next page, specify the name and path of the JAR file to create and select the Launch configuration that includes the project name and the name of the test class.


2 Answers

In order to do that, you can use the Java Compiler API.

There is this excellent tutorial that can walk you through.

like image 91
Samuel Rossille Avatar answered Sep 18 '22 14:09

Samuel Rossille


To compile code, you need a compiler. You can either use the SunOracle compiler or the Eclipse compiler. Calling the compiler API (both have documented APIs) will produce a .class file in a temporary location. You can then make a jar.

For an example of this sort of thing, start with, for example, the Maven Compiler Plugin, which is a Java module which uses the compiler API. You'll have to find your way into the Plexus compiler module.

like image 27
bmargulies Avatar answered Sep 17 '22 14:09

bmargulies