@@ -80,9 +80,10 @@ The latter two commands should return something like:
80
80
81
81
## Extra Features
82
82
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.
86
87
87
88
## License
88
89
@@ -102,8 +103,10 @@ use glob::{glob, Pattern};
102
103
103
104
pub mod errors;
104
105
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" ;
107
110
108
111
/// Returns the name of the jvm dynamic library:
109
112
///
@@ -129,26 +132,16 @@ pub fn get_jvm_dyn_lib_file_name() -> &'static str {
129
132
/// If `JAVA_HOME` is not defined, the function tries to locate it using the `java` executable.
130
133
pub fn locate_java_home ( ) -> Result < String > {
131
134
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 ( ) ,
133
136
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 ( ) ,
145
138
}
146
139
}
147
140
148
141
#[ cfg( target_os = "windows" ) ]
149
- fn do_locate_java_home ( binary : & str ) -> Result < String > {
142
+ fn do_locate_java_home ( ) -> Result < String > {
150
143
let output = Command :: new ( "where" )
151
- . arg ( binary )
144
+ . arg ( LOCATE_BINARY )
152
145
. output ( )
153
146
. map_err ( |e| JavaLocatorError :: new ( format ! ( "Failed to run command `where` ({e})" ) ) ) ?;
154
147
@@ -201,9 +194,9 @@ fn do_locate_java_home() -> Result<String> {
201
194
}
202
195
203
196
#[ 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 > {
205
198
let output = Command :: new ( "which" )
206
- . arg ( binary )
199
+ . arg ( LOCATE_BINARY )
207
200
. output ( )
208
201
. map_err ( |e| JavaLocatorError :: new ( format ! ( "Failed to run command `which` ({e})" ) ) ) ?;
209
202
let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?. trim ( ) ;
@@ -293,19 +286,12 @@ mod unit_tests {
293
286
294
287
#[ test]
295
288
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( ) ) ;
304
290
}
305
291
306
292
#[ test]
307
293
fn jni_headers_test ( ) {
308
- let java_home = locate_java_home_with_fallback ( ) . unwrap ( ) ;
294
+ let java_home = do_locate_java_home ( ) . unwrap ( ) ;
309
295
assert ! ( PathBuf :: from( java_home)
310
296
. join( "include" )
311
297
. join( "jni.h" )
0 commit comments