CodePoolUtils { public static byte[] createCodePoolDataFromClass(Class klass) throws IOException { String res = '/' + klass.getName().replace('.', '/') + ".class"; URL location = klass.getResource(res); System.out.println("createCodePoolDataFromClass " + klass + " -> " + location); String slocation = location.toString(); ByteArrayOutputStream oo = new ByteArrayOutputStream(); try (ZipOutputStream zoo = new ZipOutputStream(oo)) { if (slocation.startsWith("jar:file:")) { URL locationbase = klass.getResource("/"); int end = slocation.lastIndexOf('!'); int start = slocation.lastIndexOf("/", end); String jarName = slocation.substring(start + 1, end); ZipEntry entry = new JarEntry(jarName); zoo.putNextEntry(entry); try (InputStream in = locationbase.openStream()) { copyStream(in, zoo); } zoo.closeEntry(); } else if (slocation.startsWith("file:/")) { URL locationbase = klass.getResource("/"); String slocationbase = locationbase.toString(); Path directory = Paths.get(slocationbase.substring("file:".length())); int skip = slocationbase.length() - 6; ZipEntry dummy_entry = new ZipEntry("generated_" + System.currentTimeMillis() + ".jar"); zoo.putNextEntry(dummy_entry); ZipOutputStream dummy_jar = new ZipOutputStream(zoo); addFileToZip(skip, directory.toFile(), dummy_jar); dummy_jar.finish(); zoo.closeEntry(); } } byte[] resb = oo.toByteArray(); return resb; }  static String encodeCodePoolData(InputStream input); static String serializeExecutor(Object executor); static byte[] createZipWithOneEntry(String entryfilename, byte[] filedata); static byte[] createCodePoolDataFromClass(Class klass); static List<URL> unzipCodePoolData(Path directory, byte[] data);  }
@Test public void packFromJarTest() throws Exception { byte[] zip = CodePoolUtils.createCodePoolDataFromClass(SimpleExecutor.class); Set<String> entries = new HashSet<>(); try (ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(zip));) { ZipEntry nextEntry; while ((nextEntry = in.getNextEntry()) != null) { if (nextEntry.isDirectory()) { System.out.println("dir " + nextEntry.getName()); } else { System.out.println("file " + nextEntry.getName()); entries.add(nextEntry.getName()); } } } boolean ok1 = entries.stream().filter(s -> s.startsWith("majordodo-test-clients-") && s.endsWith(".jar")).findFirst().isPresent(); boolean ok2 = entries.stream().filter(s -> s.startsWith("generated_") && s.endsWith(".jar")).findFirst().isPresent(); assertTrue(ok1 || ok2); }