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

﹥>﹥吖頭↗ 提交于 2019-12-09 03:05:59

问题


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?

回答1:


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

There is this excellent tutorial that can walk you through.




回答2:


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.



来源:https://stackoverflow.com/questions/10237903/how-to-create-a-runnable-jar-file-from-source-code-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!