@@ -80,9 +80,10 @@ The latter two commands should return something like:
8080
8181## Extra Features
8282
83- * `locate-jdk-only`: Attempts to locate the JDK first by searching for the Java compiler. \
84- If this fails, it falls back to locating the JRE by searching for the Java runtime.
85- This solves issues in JDK 8 where the JRE resides in a subdirectory and not in the JDK root.
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.
8687
8788## License
8889
@@ -102,8 +103,10 @@ use glob::{glob, Pattern};
102103
103104pub mod errors;
104105
105- const JAVA_BINARY : & str = "java" ;
106- const JAVAC_BINARY : & str = "javac" ;
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" ;
107110
108111/// Returns the name of the jvm dynamic library:
109112///
@@ -129,26 +132,16 @@ pub fn get_jvm_dyn_lib_file_name() -> &'static str {
129132/// If `JAVA_HOME` is not defined, the function tries to locate it using the `java` executable.
130133pub fn locate_java_home ( ) -> Result < String > {
131134 match & env:: var ( "JAVA_HOME" ) {
132- Ok ( s) if s. is_empty ( ) => locate_java_home_with_fallback ( ) ,
135+ Ok ( s) if s. is_empty ( ) => do_locate_java_home ( ) ,
133136 Ok ( java_home_env_var) => Ok ( java_home_env_var. clone ( ) ) ,
134- Err ( _) => locate_java_home_with_fallback ( ) ,
135- }
136- }
137-
138- fn locate_java_home_with_fallback ( ) -> Result < String > {
139- if cfg ! ( feature = "locate-jdk-only" ) {
140- // Try locating the JDK first by searching for the Java compiler
141- // If this fails, fallback to locating the JRE, by locating the Java runtime
142- do_locate_java_home ( JAVAC_BINARY ) . or_else ( |_| do_locate_java_home ( JAVA_BINARY ) )
143- } else {
144- do_locate_java_home ( JAVA_BINARY )
137+ Err ( _) => do_locate_java_home ( ) ,
145138 }
146139}
147140
148141#[ cfg( target_os = "windows" ) ]
149- fn do_locate_java_home ( binary : & str ) -> Result < String > {
142+ fn do_locate_java_home ( ) -> Result < String > {
150143 let output = Command :: new ( "where" )
151- . arg ( binary )
144+ . arg ( LOCATE_BINARY )
152145 . output ( )
153146 . map_err ( |e| JavaLocatorError :: new ( format ! ( "Failed to run command `where` ({e})" ) ) ) ?;
154147
@@ -201,9 +194,9 @@ fn do_locate_java_home() -> Result<String> {
201194}
202195
203196#[ cfg( not( any( target_os = "windows" , target_os = "macos" ) ) ) ] // Unix
204- fn do_locate_java_home ( binary : & str ) -> Result < String > {
197+ fn do_locate_java_home ( ) -> Result < String > {
205198 let output = Command :: new ( "which" )
206- . arg ( binary )
199+ . arg ( LOCATE_BINARY )
207200 . output ( )
208201 . map_err ( |e| JavaLocatorError :: new ( format ! ( "Failed to run command `which` ({e})" ) ) ) ?;
209202 let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?. trim ( ) ;
@@ -293,19 +286,12 @@ mod unit_tests {
293286
294287 #[ test]
295288 fn locate_java_from_exec_test ( ) {
296- println ! (
297- "do_locate_java_home: {}" ,
298- do_locate_java_home( JAVA_BINARY ) . unwrap( )
299- ) ;
300- println ! (
301- "do_locate_java_home: {}" ,
302- do_locate_java_home( JAVAC_BINARY ) . unwrap( )
303- ) ;
289+ println ! ( "do_locate_java_home: {}" , do_locate_java_home( ) . unwrap( ) ) ;
304290 }
305291
306292 #[ test]
307293 fn jni_headers_test ( ) {
308- let java_home = locate_java_home_with_fallback ( ) . unwrap ( ) ;
294+ let java_home = do_locate_java_home ( ) . unwrap ( ) ;
309295 assert ! ( PathBuf :: from( java_home)
310296 . join( "include" )
311297 . join( "jni.h" )
0 commit comments