1 /* Execute a Java program.
2 Copyright (C) 2001-2003, 2006-2020 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
29 #include "classpath.h"
32 #include "concat-filename.h"
38 #define _(str) gettext (str)
41 /* Survey of Java virtual machines.
43 A = does it work without CLASSPATH being set
44 B = does it work with CLASSPATH being set to empty
45 C = option to set CLASSPATH, other than setting it in the environment
50 $JAVA unknown N Y n/a true
51 gij GCC 3.0 Y Y n/a gij --version >/dev/null
52 java JDK 1.1.8 Y Y -classpath P java -version 2>/dev/null
53 jre JDK 1.1.8 N Y -classpath P jre 2>/dev/null; test $? = 1
54 java JDK 1.3.0 Y Y -classpath P java -version 2>/dev/null
55 jview MS IE Y Y -cp P jview -? >nul; %errorlevel% = 1
57 The CLASSPATH is a colon separated list of pathnames. (On Windows: a
58 semicolon separated list of pathnames.)
60 We try the Java virtual machines in the following order:
61 1. getenv ("JAVA"), because the user must be able to override our
63 2. "gij", because it is a completely free JVM,
64 3. "java", because it is a standard JVM,
65 4. "jre", comes last because it requires a CLASSPATH environment variable,
66 5. "jview", on Windows only, because it is frequently installed.
68 We unset the JAVA_HOME environment variable, because a wrong setting of
69 this variable can confuse the JDK's javac.
73 execute_java_class (const char *class_name
,
74 const char * const *classpaths
,
75 unsigned int classpaths_count
,
76 bool use_minimal_classpath
,
78 const char * const *args
,
79 bool verbose
, bool quiet
,
80 execute_fn
*executer
, void *private_data
)
88 const char * const *arg
;
90 for (nargs
= 0, arg
= args
; *arg
!= NULL
; nargs
++, arg
++)
94 /* First, try a class compiled to a native code executable. */
97 char *exe_pathname
= xconcatenated_filename (exe_dir
, class_name
, EXEEXT
);
99 char **argv
= (char **) xmalloca ((1 + nargs
+ 1) * sizeof (char *));
104 set_classpath (classpaths
, classpaths_count
, use_minimal_classpath
,
107 argv
[0] = exe_pathname
;
108 for (i
= 0; i
<= nargs
; i
++)
109 argv
[1 + i
] = (char *) args
[i
];
113 char *command
= shell_quote_argv (argv
);
114 printf ("%s\n", command
);
118 err
= executer (class_name
, exe_pathname
, argv
, private_data
);
120 /* Reset CLASSPATH. */
121 reset_classpath (old_classpath
);
129 const char *java
= getenv ("JAVA");
130 if (java
!= NULL
&& java
[0] != '\0')
132 /* Because $JAVA may consist of a command and options, we use the
133 shell. Because $JAVA has been set by the user, we leave all
134 all environment variables in place, including JAVA_HOME, and
135 we don't erase the user's CLASSPATH. */
137 unsigned int command_length
;
140 const char * const *arg
;
145 set_classpath (classpaths
, classpaths_count
, false,
148 command_length
= strlen (java
);
149 command_length
+= 1 + shell_quote_length (class_name
);
150 for (arg
= args
; *arg
!= NULL
; arg
++)
151 command_length
+= 1 + shell_quote_length (*arg
);
154 command
= (char *) xmalloca (command_length
);
156 /* Don't shell_quote $JAVA, because it may consist of a command
158 memcpy (p
, java
, strlen (java
));
161 p
= shell_quote_copy (p
, class_name
);
162 for (arg
= args
; *arg
!= NULL
; arg
++)
165 p
= shell_quote_copy (p
, *arg
);
168 /* Ensure command_length was correctly calculated. */
169 if (p
- command
> command_length
)
173 printf ("%s\n", command
);
175 argv
[0] = BOURNE_SHELL
;
179 err
= executer (java
, BOURNE_SHELL
, argv
, private_data
);
183 /* Reset CLASSPATH. */
184 reset_classpath (old_classpath
);
190 /* Unset the JAVA_HOME environment variable. */
191 old_JAVA_HOME
= getenv ("JAVA_HOME");
192 if (old_JAVA_HOME
!= NULL
)
194 old_JAVA_HOME
= xstrdup (old_JAVA_HOME
);
195 unsetenv ("JAVA_HOME");
199 static bool gij_tested
;
200 static bool gij_present
;
204 /* Test for presence of gij: "gij --version > /dev/null" */
209 argv
[1] = "--version";
211 exitstatus
= execute ("gij", "gij", argv
, false, false, true, true,
213 gij_present
= (exitstatus
== 0);
220 char **argv
= (char **) xmalloca ((2 + nargs
+ 1) * sizeof (char *));
225 set_classpath (classpaths
, classpaths_count
, use_minimal_classpath
,
229 argv
[1] = (char *) class_name
;
230 for (i
= 0; i
<= nargs
; i
++)
231 argv
[2 + i
] = (char *) args
[i
];
235 char *command
= shell_quote_argv (argv
);
236 printf ("%s\n", command
);
240 err
= executer ("gij", "gij", argv
, private_data
);
242 /* Reset CLASSPATH. */
243 reset_classpath (old_classpath
);
252 static bool java_tested
;
253 static bool java_present
;
257 /* Test for presence of java: "java -version 2> /dev/null" */
262 argv
[1] = "-version";
264 exitstatus
= execute ("java", "java", argv
, false, false, true, true,
266 java_present
= (exitstatus
== 0);
273 char **argv
= (char **) xmalloca ((2 + nargs
+ 1) * sizeof (char *));
276 /* Set CLASSPATH. We don't use the "-classpath ..." option because
277 in JDK 1.1.x its argument should also contain the JDK's classes.zip,
278 but we don't know its location. (In JDK 1.3.0 it would work.) */
280 set_classpath (classpaths
, classpaths_count
, use_minimal_classpath
,
284 argv
[1] = (char *) class_name
;
285 for (i
= 0; i
<= nargs
; i
++)
286 argv
[2 + i
] = (char *) args
[i
];
290 char *command
= shell_quote_argv (argv
);
291 printf ("%s\n", command
);
295 err
= executer ("java", "java", argv
, private_data
);
297 /* Reset CLASSPATH. */
298 reset_classpath (old_classpath
);
307 static bool jre_tested
;
308 static bool jre_present
;
312 /* Test for presence of jre: "jre 2> /dev/null ; test $? = 1" */
318 exitstatus
= execute ("jre", "jre", argv
, false, false, true, true,
320 jre_present
= (exitstatus
== 0 || exitstatus
== 1);
327 char **argv
= (char **) xmalloca ((2 + nargs
+ 1) * sizeof (char *));
330 /* Set CLASSPATH. We don't use the "-classpath ..." option because
331 in JDK 1.1.x its argument should also contain the JDK's classes.zip,
332 but we don't know its location. */
334 set_classpath (classpaths
, classpaths_count
, use_minimal_classpath
,
338 argv
[1] = (char *) class_name
;
339 for (i
= 0; i
<= nargs
; i
++)
340 argv
[2 + i
] = (char *) args
[i
];
344 char *command
= shell_quote_argv (argv
);
345 printf ("%s\n", command
);
349 err
= executer ("jre", "jre", argv
, private_data
);
351 /* Reset CLASSPATH. */
352 reset_classpath (old_classpath
);
360 #if defined _WIN32 || defined __CYGWIN__
361 /* Native Windows, Cygwin */
363 static bool jview_tested
;
364 static bool jview_present
;
368 /* Test for presence of jview: "jview -? >nul ; test $? = 1" */
375 exitstatus
= execute ("jview", "jview", argv
, false, false, true, true,
377 jview_present
= (exitstatus
== 0 || exitstatus
== 1);
384 char **argv
= (char **) xmalloca ((2 + nargs
+ 1) * sizeof (char *));
389 set_classpath (classpaths
, classpaths_count
, use_minimal_classpath
,
393 argv
[1] = (char *) class_name
;
394 for (i
= 0; i
<= nargs
; i
++)
395 argv
[2 + i
] = (char *) args
[i
];
399 char *command
= shell_quote_argv (argv
);
400 printf ("%s\n", command
);
404 err
= executer ("jview", "jview", argv
, private_data
);
406 /* Reset CLASSPATH. */
407 reset_classpath (old_classpath
);
417 error (0, 0, _("Java virtual machine not found, try installing gij or set $JAVA"));
421 if (old_JAVA_HOME
!= NULL
)
423 xsetenv ("JAVA_HOME", old_JAVA_HOME
, 1);
424 free (old_JAVA_HOME
);