1

I am trying to create a fat-jar programmatically. So far, I managed to create a Jar file with my code inside. The problem is that, if I run this, I receive an exception saying Cannot load user class: org.company.bla.bla.

Here is my code

    private static void createJar() throws IOException {

        Manifest manifest = new Manifest();
        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
        JarOutputStream target = new JarOutputStream(new FileOutputStream("events.jar"), manifest);

        Path root = Paths.get(".").normalize().toAbsolutePath();
        System.out.println(root.toString());

        Files.walk(root).forEach(f -> add(f.toFile(), target));

        target.close();
    }

    private static void add(File source, JarOutputStream target)
    {
        BufferedInputStream in = null;
        try {
            if (source.isDirectory()) {
                String name = source.getPath().replace("\\", "/");
                if (!name.isEmpty()) {
                    if (!name.endsWith("/"))
                        name += "/";
                    JarEntry entry = new JarEntry(name);
                    entry.setTime(source.lastModified());
                    target.putNextEntry(entry);
                    target.closeEntry();
                }
                for (File nestedFile: source.listFiles())
                    add(nestedFile, target);
                return;
            }

            JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
            entry.setTime(source.lastModified());
            target.putNextEntry(entry);
            in = new BufferedInputStream(new FileInputStream(source));

            byte[] buffer = new byte[1024];
            while (true) {
                int count = in.read(buffer);
                if (count == -1)
                    break;
                target.write(buffer, 0, count);
            }
            target.closeEntry();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

How can I also add the dependencies to this Jar? Or where is the path where the dependencies are so I can include them?

Cœur
  • 37,241
  • 25
  • 195
  • 267
spaghettifunk
  • 1,936
  • 4
  • 24
  • 46
  • Possible duplicate of [How to use JarOutputStream to create a JAR file?](https://stackoverflow.com/questions/1281229/how-to-use-jaroutputstream-to-create-a-jar-file) – Raman Shrivastava Sep 16 '19 at 20:22
  • Actually it's not a duplicate. My question is more specific. I am able to create a Jar, but I am missing the dependencies, hence a fat-jar. The code above is based on that question. – spaghettifunk Sep 17 '19 at 06:47
  • The only real question here is whether `/org/company/bla/bla.class` is in the JAR file, and if not why not. – user207421 Sep 17 '19 at 06:52
  • I have a maven project and in the `pom.xml` I add a dependency (let's say kafka consumer). Now, when I create the JAR with the code above, the `kafka consumer` dependency is not in there. If I create the JAR with `mvn package` then the dependency is in there. But as I said in the question, I'd like to achieve the same result programmatically and not with the CLI. – spaghettifunk Sep 17 '19 at 07:00
  • @spaghettifunk - Have you got the answer of it ?? Even I am trying to create one similar to that. – Avinash Sep 14 '22 at 12:54

0 Answers0