JarOutputStream problems on Windows
by ricardoz on Sep.08, 2008, under Tips
If you ever tried to create a JAR file from Java code using JarOutputStream on a Windows platform you might have become as frustrated as me. I ran into two problems and since they took a nice part of my sleep time I’ve decided to share them and the solutions I found.
First of all, always include a Manifest in your JAR. You can do this quite easily by adding something similar to this to your code:
1 2 3 4 5 6 | Manifest manifest = new Manifest(); Attributes attributes = manifest.getMainAttributes(); attributes.putValue("Created-By", "Myself"); attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); // Maybe some more attributes here JarOutputStream out = new JarOutputStream(new FileOutputStream(pathToJarFile), manifest); |
Without a manifest your JAR will just not work.
Finally (this really annoyed the hell out of me) for some reason if you create your JAR using JarOutputStream and JarEntry on Windows, the paths within the ZIP meta-data are created using the Windows file separator (\), and if you have classes within packages other than the root package your application will just spill out a dreaded ClassNotFoundException.
To solve this apply the following to your file paths before creating the JarEntry:
1 2 | name = name.replaceAll("\\"+System.getProperty("file.separator"), "/"); JarEntry jarEntry = new JarEntry(name); |