简体   繁体   English

使用jdk1.6编译器API进行编译时,如何将内存中的jar文件放入类路径中?

[英]How to put a jar file in memory into classpath when compiling with jdk1.6 compiler API?

I'm doing some dynamic compilation using jdk1.6 compiler API. 我正在使用jdk1.6编译器API进行一些动态编译。
For now, I just dumped all jar files needed to disk , in order to include them in classpath when compiling. 现在,我只是将所有需要的jar文件转储到disk,以便在编译时将它们包含在classpath中。
Is there some kind of trick to put jar files in memory into classpath directly? 是否有某种技巧可以将内存中的jar文件直接放入类路径中?
I failed to figure out how to do this by extending javax.tools.ForwardingJavaFileManager. 我无法通过扩展javax.tools.ForwardingJavaFileManager来确定如何做到这一点。
Could someone give me some hints? 有人可以给我一些提示吗?
Thanks. 谢谢。

Read here on how to add jar files to your class path using manifest. 在这里阅读有关如何使用清单将jar文件添加到类路径的信息。

There should be a META-INF folder inside of your jar, the Manifest.mf file should be in that folder of the jar. jar内应该有一个META-INF文件夹,Manifest.mf文件应该在jar的该文件夹中。

There is also an option to put the jar files in JAVA_HOME/lib/ext but this is a bad practice. 还有一个选项可以将jar文件放在JAVA_HOME/lib/ext但这是一个坏习惯。 The JRE can be used by many other applications than yours, and they don't need to have your classes in their classpath. 除了您的应用程序外,JRE可以由其他许多应用程序使用,并且它们不需要在类路径中包含您的类。

I wrote my own FileManager which is entirely in memory. 我编写了自己的FileManager,它完全位于内存中。 Using this approach to can load and even write results into an in-memory "file system" 使用这种方法可以加载甚至将结果写入内存中的“文件系统”

https://github.com/OpenHFT/Java-Runtime-Compiler/blob/master/compiler/src/main/java/net/openhft/compiler/MyJavaFileManager.java https://github.com/OpenHFT/Java-Runtime-Compiler/blob/master/compiler/src/main/java/net/openhft/compiler/MyJavaFileManager.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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