Java .class files vs. .java file in a jar

拟墨画扇 提交于 2019-12-10 10:37:38

问题


Should you use .class files or .java files in a .jar library?

I want to take a class that I've written in a .java file and add it to a jar so I can use it as a library in my tomcat's WEB-INF/lib directory.

I understand that jars can contain .class files, .java files, or both (or just about anything else). I'm wondering what is specifically needed at runtime for my jar to be readable by the JVM. The answer to this question told me that .class files will work, but will .java files work as well?

I've found many descriptions of how to create a jar, and lots of information on the differences between .java files and .class files. What I'm lacking, I guess, is an understanding of how the jar libraries are interpreted by the JVM. Are they compiled at runtime?

Thank you


回答1:


Jar files are collections of compiled java classes. You need the .class file in a jar. The .java file is you source file.




回答2:


No. You'll need the byte-code (or class files) for the JVM to load the classes (although you can include the .java files, they won't be executed by the Java Runtime). A jar file is a zip file (with a tiny bit of metadata).

Also, you can add classes to your WEB-INF/classes folder (in addition, well instead of, to the WEB-INF/lib for jars, as you mentioned).



来源:https://stackoverflow.com/questions/32877978/java-class-files-vs-java-file-in-a-jar

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