简体   繁体   中英

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.

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

Eg, to add a manifest file from the current cmake source directory use:

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

I was having exactly the same problem.

I was under the impression that CMake was going to produce a manifest based upon the other input to add_jar(). That is not the case at all.

You are responsible for creating a manifest file, and then pointing CMake to it with the MANIFEST option to add_jar()

So, in the case of this trivial HelloWorld example, it might look like this:

Manifest-Version: 1.0
Created-By: YOUR NAME HERE
Main-Class: HelloWorld

Why Java requires a manifest file (when all the jars are known at javac compile time) is left as a mystery for the reader to ponder.

ALSO IMPORTANT It seems that the manifest file is not automatically a dependency for the jar target. You can modify the manifest, and run "make" again, and your jar will not be updated with the new manifest. At least this is the confirmed behaviour on my system:

cmake version 3.19.7 GNU Make 4.3 java javac 11.0.10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM