@@ -141,12 +141,7 @@ fn do_locate_java_home() -> Result<String> {
141
141
. unwrap ( )
142
142
. trim ( ) ;
143
143
144
- if java_exec_path. is_empty ( ) {
145
- return Err ( JavaLocatorError :: new (
146
- "Java is not installed or not in the system PATH" . into ( ) ,
147
- ) ) ;
148
- }
149
-
144
+ java_exec_path_validation ( java_exec_path) ?;
150
145
let mut home_path = follow_symlinks ( java_exec_path) ;
151
146
152
147
// Here we should have found ourselves in a directory like /usr/lib/jvm/java-8-oracle/jre/bin/java
@@ -171,12 +166,7 @@ fn do_locate_java_home() -> Result<String> {
171
166
172
167
let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?. trim ( ) ;
173
168
174
- if java_exec_path. is_empty ( ) {
175
- return Err ( JavaLocatorError :: new (
176
- "Java is not installed or not in the system PATH" . into ( ) ,
177
- ) ) ;
178
- }
179
-
169
+ java_exec_path_validation ( java_exec_path) ?;
180
170
let home_path = follow_symlinks ( java_exec_path) ;
181
171
182
172
home_path
@@ -193,12 +183,7 @@ fn do_locate_java_home() -> Result<String> {
193
183
. map_err ( |e| JavaLocatorError :: new ( format ! ( "Failed to run command `which` ({e})" ) ) ) ?;
194
184
let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?. trim ( ) ;
195
185
196
- if java_exec_path. is_empty ( ) {
197
- return Err ( JavaLocatorError :: new (
198
- "Java is not installed or not in the system PATH" . into ( ) ,
199
- ) ) ;
200
- }
201
-
186
+ java_exec_path_validation ( java_exec_path) ?;
202
187
let mut home_path = follow_symlinks ( java_exec_path) ;
203
188
204
189
// Here we should have found ourselves in a directory like /usr/lib/jvm/java-8-oracle/jre/bin/java
@@ -211,7 +196,21 @@ fn do_locate_java_home() -> Result<String> {
211
196
. map_err ( |path| JavaLocatorError :: new ( format ! ( "Java path {path:?} is invalid utf8" ) ) )
212
197
}
213
198
214
- // Its not clear to me which systems need this so for now its run on all systems.
199
+ fn java_exec_path_validation ( path : & str ) -> Result < ( ) > {
200
+ if path. is_empty ( ) {
201
+ return Err ( JavaLocatorError :: new (
202
+ "Java is not installed or not in the system PATH" . into ( ) ,
203
+ ) ) ;
204
+ }
205
+
206
+ let paths_found = path. lines ( ) . count ( ) ;
207
+ if paths_found > 1 {
208
+ eprintln ! ( "WARNING: java_locator found {paths_found} possible java locations. Using the first one. To silence this warning set JAVA_HOME env var." )
209
+ }
210
+
211
+ Ok ( ( ) )
212
+ }
213
+
215
214
fn follow_symlinks ( path : & str ) -> PathBuf {
216
215
let mut test_path = PathBuf :: from ( path) ;
217
216
while let Ok ( path) = test_path. read_link ( ) {
0 commit comments