# how to find\_jar for external .jar file

**URL:** https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137
**Category:** Usage
**Created:** [May 1, 2020, 5:17pm UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137 "2020-05-01T17:17:45Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)
#### Post date: [May 1, 2020, 5:17pm UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137/1 "2020-05-01T17:17:45Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 1, 2020, 5:53pm UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137/2 "2020-05-01T17:53:05Z")

</div>

`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.

---

<div class="post-metadata">

### Author: ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)
#### Post date: [May 1, 2020, 8:24pm UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137/3 "2020-05-01T20:24:03Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 1, 2020, 10:51pm UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137/4 "2020-05-01T22:51:43Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)
#### Post date: [May 4, 2020, 5:19pm UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137/5 "2020-05-04T17:19:33Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 4, 2020, 5:53pm UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137/6 "2020-05-04T17:53:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)
#### Post date: [May 4, 2020, 6:20pm UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137/7 "2020-05-04T18:20:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 5, 2020, 4:00am UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137/8 "2020-05-05T04:00:06Z")

</div>

Why would you remove `NAMES` completely? Does this work?

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

```

---

<div class="post-metadata">

### Author: ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)
#### Post date: [May 5, 2020, 4:44am UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137/9 "2020-05-05T04:44:04Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 5, 2020, 1:17pm UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137/10 "2020-05-05T13:17:06Z")

</div>

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

---

<div class="post-metadata">

### Author: ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)
#### Post date: [May 5, 2020, 5:31pm UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137/11 "2020-05-05T17:31:41Z")

</div>

I set [CMAKE\_FIND\_DEBUG\_MODE](https://cmake.org/cmake/help/latest/variable/CMAKE_FIND_DEBUG_MODE.html) 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

2. 

/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](https://cmake.org/cmake/help/latest/variable/CMAKE_FIND_DEBUG_MODE.html) . 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

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 7, 2020, 3:27pm UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137/12 "2020-05-07T15:27:01Z")

</div>

`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.

---

<div class="post-metadata">

### Author: ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)
#### Post date: [May 7, 2020, 3:58pm UTC](https://discourse.cmake.org/t/how-to-find-jar-for-external-jar-file/1137/13 "2020-05-07T15:58:36Z")

</div>

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](https://discourse.cmake.org/t/how-to-install-a-custom-target/670)
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
