This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I know that some ZIP utilities can place a password on a ZIP file. However, I don't know if all ZIP decompressers respect this password. (A cursory glance through that java.util.zip and java.util.jar APIs turned up no reference to a password feature, so I assume that you could use jar.exe to extract a ZIP file that was password protected.) What cames to mind would be to write a program that took a .jar file as an argument, read the bytes of the file and alter them using some algorithm that you wrote. You would then have to write a ClassLoader that could take one of these resulting files, reverse-engineer it to retreive the original .jar file, and then load the classes from that. (Please note that this is just a theoretical solution--I take no blame for its impracticality ) [ May 24, 2003: Message edited by: Joel McNary ]
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
It's not clear what your goal is here. A "jar file" is a format that, by design, anyone can extract from. If you change the file so it's no longer extractable, it's not really a jar file any more. What's in the jar file? If you're using it for storing something other than .class files, then there are various ways you can encrypt the contents of a file. However if you're talking about the most common situation in which the jar file contains .class files which must be accessible and understandable by a JVM, then fundamentally it's not going to be possible to prevent people from accessing the original .class file. Evn if you bury it under one or more layers of encrytion and decryption - the JVM still needs to be able to receive an unecrypted version, or the class won't load. And it's always possible for someone to hack a JVM (source code for some versions is freely available) to copy the unencrypted version to a file. See this article for details.