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?)
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.