@@ -133,23 +133,24 @@ fn do_locate_java_home() -> Result<String> {
133133 . output ( )
134134 . map_err ( |e| JavaLocatorError :: new ( format ! ( "Failed to run command `where` ({e})" ) ) ) ?;
135135
136- let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?
137- // Windows will return multiple lines if there are multiple `java` in the PATH.
136+ let java_exec_path_raw = std:: str:: from_utf8 ( & output. stdout ) ?;
137+ java_exec_path_validation ( java_exec_path_raw) ?;
138+
139+ // Windows will return multiple lines if there are multiple `java` in the PATH.
140+ let paths_found = java_exec_path_raw. lines ( ) . count ( ) ;
141+ if paths_found > 1 {
142+ eprintln ! ( "WARNING: java_locator found {paths_found} possible java locations. Using the first one. To silence this warning set JAVA_HOME env var." )
143+ }
144+
145+ let java_exec_path = java_exec_path_raw
138146 . lines ( )
139147 // The first line is the one that would be run, so take just that line.
140148 . next ( )
141- . unwrap ( )
149+ . expect ( "gauranteed to have at least one line by java_exec_path_validation" )
142150 . trim ( ) ;
143151
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-
150152 let mut home_path = follow_symlinks ( java_exec_path) ;
151153
152- // Here we should have found ourselves in a directory like /usr/lib/jvm/java-8-oracle/jre/bin/java
153154 home_path. pop ( ) ;
154155 home_path. pop ( ) ;
155156
@@ -171,12 +172,7 @@ fn do_locate_java_home() -> Result<String> {
171172
172173 let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?. trim ( ) ;
173174
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-
175+ java_exec_path_validation ( java_exec_path) ?;
180176 let home_path = follow_symlinks ( java_exec_path) ;
181177
182178 home_path
@@ -193,12 +189,7 @@ fn do_locate_java_home() -> Result<String> {
193189 . map_err ( |e| JavaLocatorError :: new ( format ! ( "Failed to run command `which` ({e})" ) ) ) ?;
194190 let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?. trim ( ) ;
195191
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-
192+ java_exec_path_validation ( java_exec_path) ?;
202193 let mut home_path = follow_symlinks ( java_exec_path) ;
203194
204195 // Here we should have found ourselves in a directory like /usr/lib/jvm/java-8-oracle/jre/bin/java
@@ -211,7 +202,16 @@ fn do_locate_java_home() -> Result<String> {
211202 . map_err ( |path| JavaLocatorError :: new ( format ! ( "Java path {path:?} is invalid utf8" ) ) )
212203}
213204
214- // Its not clear to me which systems need this so for now its run on all systems.
205+ fn java_exec_path_validation ( path : & str ) -> Result < ( ) > {
206+ if path. is_empty ( ) {
207+ return Err ( JavaLocatorError :: new (
208+ "Java is not installed or not in the system PATH" . into ( ) ,
209+ ) ) ;
210+ }
211+
212+ Ok ( ( ) )
213+ }
214+
215215fn follow_symlinks ( path : & str ) -> PathBuf {
216216 let mut test_path = PathBuf :: from ( path) ;
217217 while let Ok ( path) = test_path. read_link ( ) {
0 commit comments