简体   繁体   中英

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

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

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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