How do you make a Zip/Jar in Java that will not contain the absolute pathname?
I'm generating a .jar file in Java, but the .jar contains an absolute pathname of where it is in the system (/tmp/tempXXX/foo instead of /foo). The tree is like this:
Instead of this:
Is it possible to fix this? Here is the function that makes it:
public static void add(File source, JarOutputStream target, String removeme)
BufferedInputStream in = null;
File source2 = source;
String name = source2.getPath().replace("\\", "/");
name += "/";
JarEntry entry = new JarEntry(name);
for (File nestedFile : source.listFiles())
add(nestedFile, target, removeme);
JarEntry entry = new JarEntry(source2.getPath().replace("\\", "/"));
in = new BufferedInputStream(new FileInputStream(source));
byte buffer = new byte;
int count = in.read(buffer);
if (count == -1)
target.write(buffer, 0, count);
if (in != null)
The source2 variable was made for modifying the path, but when modifying, it gave an "Invalid .jar file" error.
The modification was this:
File source2 = new File(source.getPath().replaceAll("^" + removeme, ""));
Re: How do you make a Zip/Jar in Java that will not contain the absolute pathname?
I've never seen a Jar being created this way, so I'm only guessing, but have you tried removing removeme (which I assume is the pathname leading up to the directory you're working with) from the String name instead of from the File? And in that case, wouldn't remove me just be your source path?