@@ -141,12 +141,7 @@ fn do_locate_java_home() -> Result<String> {
141141 . unwrap ( )
142142 . trim ( ) ;
143143
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) ?;
150145 let mut home_path = follow_symlinks ( java_exec_path) ;
151146
152147 // 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> {
171166
172167 let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?. trim ( ) ;
173168
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) ?;
180170 let home_path = follow_symlinks ( java_exec_path) ;
181171
182172 home_path
@@ -193,12 +183,7 @@ fn do_locate_java_home() -> Result<String> {
193183 . map_err ( |e| JavaLocatorError :: new ( format ! ( "Failed to run command `which` ({e})" ) ) ) ?;
194184 let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?. trim ( ) ;
195185
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) ?;
202187 let mut home_path = follow_symlinks ( java_exec_path) ;
203188
204189 // 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> {
211196 . map_err ( |path| JavaLocatorError :: new ( format ! ( "Java path {path:?} is invalid utf8" ) ) )
212197}
213198
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+
215214fn follow_symlinks ( path : & str ) -> PathBuf {
216215 let mut test_path = PathBuf :: from ( path) ;
217216 while let Ok ( path) = test_path. read_link ( ) {
0 commit comments