I'm curious about the flexibility of JAR files. I know they're mainly used for sharing Java code in a compressed format, but could I throw in a PDF or a text file in there without any Java code, and it would still be a valid JAR? Are there any downsides to that instead of just using a ZIP file?
3 Answers
Yes, you can include all sorts of files in a JAR. It's quite common to pack images or sounds that a Java program might need. The only real downside is that not all programs will know how to handle those non-Java files, but using an unzip tool should work just fine.
Just to clarify, a JAR is a zip file but with specific requirements for Java to run it. If there’s no valid Java program inside, then the JAR won’t execute any Java code. You can have a 'working' JAR that’s packed with non-Java files, but it’d be useless from a Java standpoint.
Absolutely! A JAR file is essentially just a zip file with a specific structure. You can definitely add files like PDFs or text documents. Just keep in mind that if you're not using it for Java purposes, it might confuse users since they wouldn't expect to find random files in a JAR.

Yeah, exactly! You'd just end up with a valid JAR that doesn't do anything useful in terms of Java execution. It’d be like having a toolbox with no tools to use.