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