Script to remove comments in Java

一个人想着一个人 提交于 2019-12-02 10:25:12

问题


Does anyone know how (or process) to remove comments from Java class file at build time? i would like to do this for specific packages in the project when the project is build using Ant. Any ANT scripts or some custom code most welcome.


回答1:


There are no comments in the compiled .class file.




回答2:


I believe the Java compiler does this for you already.




回答3:


Class files don't have comments, they are removed by the compiler. If you want to strip comments from the source files, you could use the Ant ReplaceRegExp task. But I'm curious as to why you would want to do this.




回答4:


To shrink a Java jar file you can unset the -g flag to get rid of the debugging symbols, and you can run a code obfuscator. Code obfuscation is common in the J2ME world to minimize over-the-air download time to the phone. The downside to these steps is that it makes debugging much, much harder.




回答5:


the .class files don't normally include comments (you could actually store additional info about comments in there.. but AFAIK no one does that).

What is often present is the debug information containing the original variable names, source file information and line numbers information with the byte code. This can be controlled via the -g switch for javac or the debug attribute of the ant javac task.



来源:https://stackoverflow.com/questions/834584/script-to-remove-comments-in-java

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