Search code examples
javajarcmakemanifestprogram-entry-point

cmake jar package cannot execute: no main manifest attribute


I've got a very simple cmake-java project from the internet as below:

cat HelloWorld.java

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Then CMakeLists.txt file:

cmake_minimum_required (VERSION 2.8)
find_package(Java REQUIRED)
include(UseJava)
enable_testing()
project (HelloWorld)
set(CMAKE_JAVA_COMPILE_FLAGS "-source" "1.8" "-target" "1.8")
add_jar(HelloWorld HelloWorld.java)
get_target_property(_jarFile HelloWorld JAR_FILE)
get_target_property(_classDir HelloWorld CLASSDIR)
message(STATUS "Jar file ${_jarFile}")
message(STATUS "Class compiled to ${_classDir}")
add_test(NAME TestHelloWorld COMMAND ${Java_JAVA_EXECUTABLE} -cp ${_jarFile} HelloWorld)

I'm on centos7 with jdk 1.8 and cmake 3.8, Then

cmake . && make -j8

There's HelloWorld.jar file and it gives error when run:

java -jar HelloWorld.jar HelloWorld

no main manifest attribute, in HelloWorld.jar

Yes, there's no main class definition inside it:

$ cat META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Created-By: 1.8.0_191 (Oracle Corporation)

I just wish to know how to add content into MANIFEST.MF using cmake, would you help to explain? Thanks a lot.


Solution

  • Use the MANIFEST option of the add_jar command to add a custom manifest file.

    E.g., to add a manifest file from the current cmake source directory use:

    add_jar(HelloWorld HelloWorld.java MANIFEST "${CMAKE_CURRENT_SOURCE_DIR}/MANIFEST.MF")