简体   繁体   English

如何编译将.class文件直接放入JAR的Java文件

[英]How to compile Java files that put .class files directly into JAR

First some reference: 首先参考一下:
1st Link 第一链接
2nd link 第二个链接

The first article 1st Link mentions about compiling the Java files directly into JAR files and avoiding one step in the build process. 第一篇文章1st Link提到了将Java文件直接编译为JAR文件,并避免了构建过程中的一个步骤。 Does anyone know this? 有人知道吗?

-Vadiraj -Vadiraj

As you linked to my blog post I thought it was only fair to give you an update. 当您链接到我的博客文章时,我认为给您更新是公平的。

Compiling directly to a Jar is actually fairly simple to do. 实际上,直接编译为Jar相当简单。 Basically you extend 基本上你扩展

javax.tools.ForwardingJavaFileObject

Then override openOutputStream method and direct it to your Jar. 然后重写openOutputStream方法并将其定向到您的Jar中。 As the Java Compiler is highly concurrent but writing to a jar file is highly sequential I'd recommend that you buffer to byte arrays and then have a background thread that writes the byte arrays as they arrive. 由于Java编译器是高度并发的,但是写入jar文件的序列是高度顺序的,所以我建议您缓冲到字节数组,然后有一个后台线程在字节数组到达时写入它们。

I do exactly this is my experimental build tool JCompilo https://code.google.com/p/jcompilo/ This tool is completely undocumented but crazy fast. 我的确是我的实验性构建工具JCompilo https://code.google.com/p/jcompilo/。该工具完全没有文档记载,但速度很快。 Currently it's about 20-80% faster than any other Java build tool and about 10x faster than the Scala compiler for the same code. 目前,对于相同的代码,它比任何其他Java构建工具快约20-80%,比Scala编译器快约10倍。

As the author is talking about extending the compiler itself, it is possible that he has knowledge of the built-in capabilities of the compiler (that is what the compiler is capable of, maybe with a little encouragement by tweaking the code). 在作者谈论扩展编译器本身时,他可能了解编译器的内置功能(这就是编译器的功能,可能通过调整代码有些鼓励)。

Right now I'm investigating extending the Java 6 compiler to remove the unneeded file exists checks and possible jaring the class files directly in the compiler. 现在,我正在研究扩展Java 6编译器以删除不需要的文件,并进行检查,并可能直接在编译器中封装类文件。 [emphasis mine] [强调我的]

That capability, however, is certainly not supported officially (no documentation exist about it on the javac webpage ). 但是,肯定不正式支持该功能( javac网页上没有关于此功能的文档)。

At best, the feature is compiler dependent; 此功能充其量取决于编译器。 possibly requiring modification of the compiler's source code. 可能需要修改编译器的源代码。

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

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