Search code examples
javajarentry-point

How can I create a jar file with an entry point inside a file tree?


I am trying to create a jar file using this command:

jar -cef ./bin/a/MainClass formatter.jar ./bin/*

Where bin contains two folders with class files (lets say "./bin/a/" and "./bin/b/").

I can create the jar fine, but I can't seem to get it to execute properly. It runs without printing or doing anything. I assume it must not be using the entry point I supplied.

MainClass has a simple main method:

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

What am I doing wrong? Is it necessary for the jar to be created in the same folder as the class containing main?

Edit: jar -tf as requested:

C:\Users\Jonathan\Documents\Workspace\JavaAutoFormatter>jar -tf formatter.jar
META-INF/
META-INF/MANIFEST.MF
bin/a/
bin/a/MainClass.class
bin/a/ParsedFile.class
bin/b/
bin/b/Token.class
bin/b/Tokenifier.class

Solution

  • See this tutorial. The argument for the jar command's e option should be a class name, not a path name. Furthermore, when specifying the list of classes to be included in the jar, care should be taken to reference the files such that the directory structure matches the package name. Otherwise the classpath lookups will fail. Try:

    jar -cef a.MainClass formatter.jar -C bin .