0

So I have the following project Structure: I have a package called "com.github.liketechnik". My main class is Build in this package ("com.github.liketechnik.Build").

I programmatically put the compiled classes from there into a jar file with a correct manifest, e. g. containing a version attribute and a Main-Class entry "com.github.liketechnik.Build". The classes inside the jar are contained under "com/github/liketechnik/Build.class".

When running java -jar output.jar it fails directly: "Error: Mainclass com.github.liketechnik.Build could not be found or loaded" (translated from german). When running the class directly via java -cp build/main com.github.liketechnik.Build (e. g. the class files directly, outside the jar), it works without proplems.

I tried this hours and also searched every single file for typos, so I appreciate any help with this problem as all other threads concerning such problems did not help with mine. If you need any more Information or the sourcecode directly I've no problems with giving this to you.

EDIT: Added structure of jar file.

The jar file has the following structure:

  • one dir "META-INF" containing "MANIFEST.MF" with the mentioned entries.

  • one dir structure "com/github/liketechnik/Build.class" (for example, contains other classes (in subpackages) too.

Manifest:

Manifest-Version: 1.0
Main-Class: com.github.liketechnik.Build

Build.java:

package com.github.liketechnik;

public class Build {

    public static void main(String[] args) {
        System.out.println("Hi");
    }
}

Output when manually running: Hi (as expected) Output when running from jar: Fehler: Hauptklasse com.github.liketechnik.Build konnte nicht gefunden oder geladen werden (Why?)

liketechnik
  • 104
  • 5
  • What does the structure of the JAR file look like? You can open it with any ZIP tool. – Code-Apprentice Jul 06 '17 at 19:52
  • edited my answer showing the strucutre. I checked this file too, it seems correct too me. – liketechnik Jul 06 '17 at 19:56
  • Instead of describing the contents of files (such as the manifest), just **show** them by copy and pasting directly. You don't necessarily have to paste the entire thing, but showing relevant parts is much more helpful than an English description alone. – Code-Apprentice Jul 06 '17 at 19:59
  • Also read [mcve]. You should provide the exact code that recreates the problem you are having so that anyone else can just copy and paste it to see for themselves. – Code-Apprentice Jul 06 '17 at 19:59
  • Is it better like this? Or what should I append to this to make it easier to understand? – liketechnik Jul 06 '17 at 20:28
  • The code example is perfect. You could probably edit your question so that the entire thing reads more easily. You do not need to mark the edited parts. – Code-Apprentice Jul 07 '17 at 06:22

1 Answers1

0

Ok, seems like creating .jar archives programmatically is not as easy as it seems. It seems like beneath the .zip specifications one needs to handle (see this question) is another caveat, so the entries need to be added following the directory hierachy.

Example: Assume you have directories a and b, b containing files test.class toll.class, you have to first add a, then b and then the files inside b . Otherwise it won't work.

liketechnik
  • 104
  • 5