@@ -141,15 +141,9 @@ 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
152- // Here we should have found ourselves in a directory like /usr/lib/jvm/java-8-oracle/jre/bin/java
153147 home_path. pop ( ) ;
154148 home_path. pop ( ) ;
155149
@@ -171,12 +165,7 @@ fn do_locate_java_home() -> Result<String> {
171165
172166 let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?. trim ( ) ;
173167
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) ?;
180169 let home_path = follow_symlinks ( java_exec_path) ;
181170
182171 home_path
@@ -193,12 +182,7 @@ fn do_locate_java_home() -> Result<String> {
193182 . map_err ( |e| JavaLocatorError :: new ( format ! ( "Failed to run command `which` ({e})" ) ) ) ?;
194183 let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?. trim ( ) ;
195184
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) ?;
202186 let mut home_path = follow_symlinks ( java_exec_path) ;
203187
204188 // 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> {
211195 . map_err ( |path| JavaLocatorError :: new ( format ! ( "Java path {path:?} is invalid utf8" ) ) )
212196}
213197
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+
215213fn follow_symlinks ( path : & str ) -> PathBuf {
216214 let mut test_path = PathBuf :: from ( path) ;
217215 while let Ok ( path) = test_path. read_link ( ) {
0 commit comments