how to find_jar for external .jar file

Could you please advise why the following is not working.
find_package(Java COMPONENTS Development)
include(UseJava)
set(MY_JAR_PACKAGE_INCLUDE_DIR “${CMAKE_SOURCE_DIR}/mvngened”)
find_jar(myJavaJar, ext.jar
NAMES “${CMAKE_SOURCE_DIR}/mvngened/ext.jar”
PATHS “${CMAKE_SOURCE_DIR}/mvngened”
)
then I am getting -NOTFOUND even though file is there and log shows cmake is looking in right ${CMAKE_SOURCE_DIR}/mvngened dir.
This code is a part of CMakeLists.txt file located in the CMAKE_SOURCE_DIR.
Is CMAKE only able to find_jar if it was built as a part of the cmake project?
Should I be using include_directiries(${CMAKE_SOURCE_DIR}/mvngened ) or somehow specified jar as IMPORTED?

NAMES ext.jar is probably what you meant. The search algorithm will search for each NAMES argument in each PATHS argument, so manually putting them together is unnecessary.

Yes, the “,” is a typo. Thanks. I don’t have it my actual code. However the issue is that file is not being found even if unnecessary full file name is specified in NAME.

Well, the , is another problem I didn’t notice. What did you end up trying that didn’t work? The values passed to NAMES should not be full paths.

  1. I read that that per NAMES definition it is full path ( see 3.17 Finding JARs ,

“ The names of the full path to a file that is searched for is specified by the names after NAMES argument”. Is my interpretation correct?

  1. I tried omit NAMES and PATHS but still get NOT found

That documentation is poorly worded. It is probably better as:

The names the file that is searched for is specified by the names after NAMES argument

Yep, that is much more logical description than official doc.
Still the issue is that even with option without names

find_jar(myJavaJar ext.jar
PATHS “${CMAKE_SOURCE_DIR}/mvngened”
)

find_jar(myJavaJar ”${CMAKE_SOURCE_DIR}/mvngened/ext.jar”
PATHS “${CMAKE_SOURCE_DIR}/mvngened”
)
and with name specified after NAMES art without name before NAMES and still jar is not found even thought it is there under directory specified by PATHS

Why would you remove NAMES completely? Does this work?

find_jar(myJavaJar
  NAMES ext.jar
  PATHS “${CMAKE_SOURCE_DIR}/mvngened”)

Because definition indicates name | NAMES name1… so it seems dropping NAMES list is valid . No, the case with no name but NAMES name1 did not work. Is there a way to debug find_jar ? I looked at CMakeCache but nothing gives path it tried

You can use 3.17’s --debug-find CMake flag to help track down the problem.

I set CMAKE_FIND_DEBUG_MODE to TRUE (before find_jar and find_path commands) which supposedly is equivalent to --debug-find and found that find_ considered only kind of system root path (e.g …_COMPILER_PATH, dir that is a parent tor CMAKE_HOME , toolchains that are in the PATH, but ignored PATHS, HINTS,
for find_jar it created non -existing path by concatenating path to a gcc platform specific compiler and the following paths postfix-es:

  1. /usr/share/java/est.jar

/usr/local/java/ext.jar

  1. full path (starting from /home…) which is set in PATHS/ext.jar

It considered 2 of the platform-specific cross-compilers on a systems to form head portion of the path.

So the problem is that find_jar does not implicitly uses PATH but postpend it to some known toolchain path locations. So

Is there way to change this behaviour? Maybe ENV variable settings?

Also find_jar is not listed as supported by CMAKE_FIND_DEBUG_MODE . Is this a documentation error?

or set some of the variables NO_DEFAULT_PATH, NO_CMAKE_ENVIRONMENT_PATH , NO_CMAKE_PATH, NO_SYSTEM_ENVIRONMENT_PATH, NO_CMAKE_SYSTEM_PATH, CMAKE_FIND_ROOT_PATH_BOTH, ONLY_CMAKE_FIND_ROOT_PATH, orNO_CMAKE_FIND_ROOT_PATH ???
How do I set combination of these?
UPDATE:
I tried find_path with NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_FIND_ROOT_PATH
resulting in cmake find root path is no longer prepended to search, and file was found.
For the find_jar with these parameters I hoped to fix the issue by removing .jar extention from the NAMES name 1 like ext instead of ext.jar. I wish cmake documentation would indicate that names should not contain extension

Best Regards

find_jar is a wrapper around find_path. Something might be wrong in UseJava.cmake. I recommend debugging it to see why its constucted find_path is not behaving as you expect.

I tested 2 options with name and no NAMES and with NAMES name 1 and no name and both work fine with name /name1 not having not only extension .jar but even with just a firts several characters in the name. Then find_jar finds full path to ext.jar ok. So I suggest documentation is updated.
Still the following questions remain:

  1. How do I find all instances of .jar files under the PATHS when multiple .jar files are there and form/fill a list variable
  2. In case .jar file is not built with cmake (e.g. built with maven), but need to be installed and therefore install(PROGRAMS is being used for installation, how to ensure the downstream consumers of my library and the jar file be able to find it ? How to ensure the myprojConfigure.cmake file I generate during cmake build has appropriate path for jar file not my own host path to .jar
    Attempt to
    find_jar(extJar …
    add_custom_target(extJartarget ALL
    DEPENDS ${extJar} # File level dep-y so must b file not target
    )
    set_property(TARGET extJartarget PROPERTY
    INSTALL_FILES
    ${CMAKE_SOURCE_DIR}/mvngened/ext.jar
    )
    install(TARGETS extJartarget
    EXPORT extJartargetExpname
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    COMPONENT RunTimeJar
    )
    gives error
    install TARGETS given target “extJartarget” which is not an executable,
    library, or module. <-yes itis .jar, so?>
    Alternative installation works with install(PROGRAMS ${extJar} … installs file ok, but it does not create target statement in the myprojConfigure.cmake as my other libraries installed with install(TARGETS , install(EXPORT do.
    adding set_target_properties(extJartarget
    PROPERTIES EXPORT_NAME myOtherExpedTargets) does not help
    install_jar does install .jar despite there call to add_jar (contradictory to documentation?) However I wish it would be possible to add external .jar to target list as with any executable.
    Here is relevant post: How to install custom target
  3. What is controlling enablement of the set(CMAKE_FIND_DEBUG_MODE TRUE) . I observe it is working on a fresh install-build but then it stop generating debug output for find_jar /fin_path