@@ -78,12 +78,19 @@ The latter two commands should return something like:
78
78
79
79
> /usr/lib/jvm/java-11-openjdk-amd64/lib
80
80
81
+ ## Extra Features
82
+
83
+ * `locate-jdk-only`: Attempts to locate the JDK by searching for the Java compiler,
84
+ as opposed to searching for the runtime.
85
+ This solves issues in JDK 8 where the JRE resides in a subdirectory and not in the JDK root,
86
+ so development files are not found in JAVA_HOME as would be expected.
87
+
81
88
## License
82
89
83
90
At your option, under:
84
91
85
- * Apache License, Version 2.0, (http://www.apache.org/licenses/LICENSE-2.0)
86
- * MIT license (http://opensource.org/licenses/MIT)
92
+ * Apache License, Version 2.0, (< http://www.apache.org/licenses/LICENSE-2.0> )
93
+ * MIT license (< http://opensource.org/licenses/MIT> )
87
94
88
95
*/
89
96
@@ -96,6 +103,11 @@ use glob::{glob, Pattern};
96
103
97
104
pub mod errors;
98
105
106
+ #[ cfg( not( feature = "locate-jdk-only" ) ) ]
107
+ const LOCATE_BINARY : & str = "java" ;
108
+ #[ cfg( feature = "locate-jdk-only" ) ]
109
+ const LOCATE_BINARY : & str = "javac" ;
110
+
99
111
/// Returns the name of the jvm dynamic library:
100
112
///
101
113
/// * libjvm.so for Linux
@@ -129,7 +141,7 @@ pub fn locate_java_home() -> Result<String> {
129
141
#[ cfg( target_os = "windows" ) ]
130
142
fn do_locate_java_home ( ) -> Result < String > {
131
143
let output = Command :: new ( "where" )
132
- . arg ( "java" )
144
+ . arg ( LOCATE_BINARY )
133
145
. output ( )
134
146
. map_err ( |e| JavaLocatorError :: new ( format ! ( "Failed to run command `where` ({e})" ) ) ) ?;
135
147
@@ -184,7 +196,7 @@ fn do_locate_java_home() -> Result<String> {
184
196
#[ cfg( not( any( target_os = "windows" , target_os = "macos" ) ) ) ] // Unix
185
197
fn do_locate_java_home ( ) -> Result < String > {
186
198
let output = Command :: new ( "which" )
187
- . arg ( "java" )
199
+ . arg ( LOCATE_BINARY )
188
200
. output ( )
189
201
. map_err ( |e| JavaLocatorError :: new ( format ! ( "Failed to run command `which` ({e})" ) ) ) ?;
190
202
let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?. trim ( ) ;
@@ -276,4 +288,13 @@ mod unit_tests {
276
288
fn locate_java_from_exec_test ( ) {
277
289
println ! ( "do_locate_java_home: {}" , do_locate_java_home( ) . unwrap( ) ) ;
278
290
}
291
+
292
+ #[ test]
293
+ fn jni_headers_test ( ) {
294
+ let java_home = do_locate_java_home ( ) . unwrap ( ) ;
295
+ assert ! ( PathBuf :: from( java_home)
296
+ . join( "include" )
297
+ . join( "jni.h" )
298
+ . exists( ) ) ;
299
+ }
279
300
}
0 commit comments