if(mainClazz != null) {
manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClazz.getName());
}
manifest.getMainAttributes().put(new Attributes.Name("X-Rascal-Saved-Class"), clazz.getName());
JarOutputStream target = new JarOutputStream(outStream, manifest);
JarEntry entry = new JarEntry("META-INF/");
target.putNextEntry(entry);
Collection<String> dirs = new ArrayList<String>();
for (JavaFileObject o : list) {
String path = o.toUri().getPath().replace(".", "/");
makeJarDirs(target, dirs, path);
entry = new JarEntry(path + ".class");
entry.setTime(o.getLastModified());
target.putNextEntry(entry);
try(InputStream stream = o.openInputStream()) {
byte[] buffer = new byte[8192];
int c = stream.read(buffer);
while (c > -1) {
target.write(buffer, 0, c);
c = stream.read(buffer);
}
}
target.closeEntry();
}
if(mainClazz != null) {
String name = mainClazz.getName();
String path = name.replace(".", "/") + ".class";
String dir = path.substring(0, path.lastIndexOf('/'));
StringBuilder dirTmp = new StringBuilder(dir.length());
for (String d : dir.split("/")) {
dirTmp.append(d);
dirTmp.append("/");
String tmp = dirTmp.toString();
if (!dirs.contains(tmp)) {
dirs.add(tmp);
entry = new JarEntry(tmp);
target.putNextEntry(entry);
}
}
entry = new JarEntry(path);
target.putNextEntry(entry);
try(InputStream stream = mainClazz.getClassLoader().getResourceAsStream(path)) {
byte[] buffer = new byte[8192];
int c = stream.read(buffer);
while (c > -1) {
target.write(buffer, 0, c);
c = stream.read(buffer);
}
}
target.closeEntry();
}
target.close();
}
}