0

I've got a very simple cmake java project

(1) CMakeLists.txt:

cmake_minimum_required (VERSION 2.8)
find_package(Java)
include(UseJava)
enable_testing()

project (HelloWorld)
set(CMAKE_JAVA_COMPILE_FLAGS "-source" "1.8" "-target" "1.8")
set(CMAKE_JAVA_MANIFEST MANIFEST.MF)
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}")

(2) HelloWorld.java

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

(3) MANIFEST.MF

Manifest-Version: 1.0
Extension-Name: My
Implementation-Version: 1.1
Main-Class: HelloWorld

(4) cmake . && make

-- Jar file /root/mynet/mytest/build/useCmake/forJava/HelloWorld.jar
-- Class compiled to /root/mynet/mytest/build/useCmake/forJava/CMakeFiles/HelloWorld.dir
-- Configuring done
-- Generating done
-- Build files have been written to: /root/mynet/mytest/build/useCmake/forJava
/usr/bin/cmake -H/root/mynet/mytest/build/useCmake/forJava -B/root/mynet/mytest/build/useCmake/forJava --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /root/mynet/mytest/build/useCmake/forJava/CMakeFiles /root/mynet/mytest/build/useCmake/forJava/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/root/mynet/mytest/build/useCmake/forJava'
make -f CMakeFiles/HelloWorld.dir/build.make CMakeFiles/HelloWorld.dir/depend
make[2]: Entering directory '/root/mynet/mytest/build/useCmake/forJava'
cd /root/mynet/mytest/build/useCmake/forJava && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /root/mynet/mytest/build/useCmake/forJava /root/mynet/mytest/build/useCmake/forJava /root/mynet/mytest/build/useCmake/forJava /root/mynet/mytest/build/useCmake/forJava /root/mynet/mytest/build/useCmake/forJava/CMakeFiles/HelloWorld.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/root/mynet/mytest/build/useCmake/forJava'
make -f CMakeFiles/HelloWorld.dir/build.make CMakeFiles/HelloWorld.dir/build
make[2]: Entering directory '/root/mynet/mytest/build/useCmake/forJava'
make[2]: Nothing to be done for 'CMakeFiles/HelloWorld.dir/build'.
make[2]: Leaving directory '/root/mynet/mytest/build/useCmake/forJava'
[100%] Built target HelloWorld
make[1]: Leaving directory '/root/mynet/mytest/build/useCmake/forJava'
/usr/bin/cmake -E cmake_progress_start /root/mynet/mytest/build/useCmake/forJava/CMakeFiles 0

OK, things looks fine, but

java -jar HelloWorld.jar
no main manifest attribute, in HelloWorld.jar

I checked the META-INF/MANIFEST.MF in my jar, it's not the MANIFEST of my own:

jar tvf HelloWorld.jar
     0 Tue Feb 19 03:56:08 GMT 2019 META-INF/
    69 Tue Feb 19 03:56:08 GMT 2019 META-INF/MANIFEST.MF
   427 Tue Feb 19 03:49:20 GMT 2019 HelloWorld.class

It contains only 2 lines:

Manifest-Version: 1.0
Created-By: 1.8.0_191 (Oracle Corporation)

So seems my manifest file didn't take effect. How to fix it? Thanks a lot

Hind Forsum
  • 9,717
  • 13
  • 63
  • 119

1 Answers1

1

According to the cmake documentation:

To define a custom manifest for the jar, you can set it with the manifest named argument:

add_jar(example MANIFEST /path/to/manifest)

But note, I've never heard of people using cmake to build Java code.

Have a look at maven, ivy or gradle instead - they are actively supported and developed.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • I cannot have 2 "add_jar" for the same target "HelloWorld", cmake will report error – Hind Forsum Feb 19 '19 at 06:03
  • 1
    @HindForsum: No needs in additional `add_jar`, just add `MANIFEST` option to **existed** `add_jar` call, which creates `HelloWorld` target, like in [that question](https://stackoverflow.com/questions/26317677/how-to-add-a-manifest-file-usig-cmake-and-module-usejava). (I agree that documentation isn't clear about that). – Tsyvarev Feb 19 '19 at 10:12