@@ -133,23 +133,24 @@ fn do_locate_java_home() -> Result<String> {
133
133
. output ( )
134
134
. map_err ( |e| JavaLocatorError :: new ( format ! ( "Failed to run command `where` ({e})" ) ) ) ?;
135
135
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
138
146
. lines ( )
139
147
// The first line is the one that would be run, so take just that line.
140
148
. next ( )
141
- . unwrap ( )
149
+ . expect ( "gauranteed to have at least one line by java_exec_path_validation" )
142
150
. trim ( ) ;
143
151
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
-
150
152
let mut home_path = follow_symlinks ( java_exec_path) ;
151
153
152
- // Here we should have found ourselves in a directory like /usr/lib/jvm/java-8-oracle/jre/bin/java
153
154
home_path. pop ( ) ;
154
155
home_path. pop ( ) ;
155
156
@@ -171,12 +172,7 @@ fn do_locate_java_home() -> Result<String> {
171
172
172
173
let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?. trim ( ) ;
173
174
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) ?;
180
176
let home_path = follow_symlinks ( java_exec_path) ;
181
177
182
178
home_path
@@ -193,12 +189,7 @@ fn do_locate_java_home() -> Result<String> {
193
189
. map_err ( |e| JavaLocatorError :: new ( format ! ( "Failed to run command `which` ({e})" ) ) ) ?;
194
190
let java_exec_path = std:: str:: from_utf8 ( & output. stdout ) ?. trim ( ) ;
195
191
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) ?;
202
193
let mut home_path = follow_symlinks ( java_exec_path) ;
203
194
204
195
// 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> {
211
202
. map_err ( |path| JavaLocatorError :: new ( format ! ( "Java path {path:?} is invalid utf8" ) ) )
212
203
}
213
204
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
+
215
215
fn follow_symlinks ( path : & str ) -> PathBuf {
216
216
let mut test_path = PathBuf :: from ( path) ;
217
217
while let Ok ( path) = test_path. read_link ( ) {
0 commit comments