简体   繁体   中英

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?

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

There is this excellent tutorial that can walk you through.

To compile code, you need a compiler. You can either use the Sun Oracle 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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