1 /* Execute a Java program.
2 Copyright (C) 2001-2003 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 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 #include "classpath.h"
41 #define _(str) gettext (str)
44 /* Survey of Java virtual machines.
46 A = does it work without CLASSPATH being set
47 B = does it work with CLASSPATH being set to empty
48 C = option to set CLASSPATH, other than setting it in the environment
53 $JAVA unknown N Y n/a true
54 gij GCC 3.0 Y Y n/a gij --version >/dev/null
55 java JDK 1.1.8 Y Y -classpath P java -version 2>/dev/null
56 jre JDK 1.1.8 N Y -classpath P jre 2>/dev/null; test $? = 1
57 java JDK 1.3.0 Y Y -classpath P java -version 2>/dev/null
58 jview MS IE Y Y -cp P jview -? >nul; %errorlevel% = 1
60 The CLASSPATH is a colon separated list of pathnames. (On Windows: a
61 semicolon separated list of pathnames.)
63 We try the Java virtual machines in the following order:
64 1. getenv ("JAVA"), because the user must be able to override our
66 2. "gij", because it is a completely free JVM,
67 3. "java", because it is a standard JVM,
68 4. "jre", comes last because it requires a CLASSPATH environment variable,
69 5. "jview", on Windows only, because it is frequently installed.
71 We unset the JAVA_HOME environment variable, because a wrong setting of
72 this variable can confuse the JDK's javac.
76 execute_java_class (const char *class_name,
77 const char * const *classpaths,
78 unsigned int classpaths_count,
79 bool use_minimal_classpath,
81 const char * const *args,
82 bool verbose, bool quiet,
83 execute_fn *executer, void *private_data)
91 const char * const *arg;
93 for (nargs = 0, arg = args; *arg != NULL; nargs++, arg++)
97 /* First, try a class compiled to a native code executable. */
100 char *exe_pathname = concatenated_pathname (exe_dir, class_name, EXEEXT);
102 char **argv = (char **) xallocsa ((1 + nargs + 1) * sizeof (char *));
107 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
110 argv[0] = exe_pathname;
111 for (i = 0; i <= nargs; i++)
112 argv[1 + i] = (char *) args[i];
116 char *command = shell_quote_argv (argv);
117 printf ("%s\n", command);
121 err = executer (class_name, exe_pathname, argv, private_data);
123 /* Reset CLASSPATH. */
124 reset_classpath (old_classpath);
132 const char *java = getenv ("JAVA");
133 if (java != NULL && java[0] != '\0')
135 /* Because $JAVA may consist of a command and options, we use the
136 shell. Because $JAVA has been set by the user, we leave all
137 all environment variables in place, including JAVA_HOME, and
138 we don't erase the user's CLASSPATH. */
140 unsigned int command_length;
143 const char * const *arg;
148 set_classpath (classpaths, classpaths_count, false,
151 command_length = strlen (java);
152 command_length += 1 + shell_quote_length (class_name);
153 for (arg = args; *arg != NULL; arg++)
154 command_length += 1 + shell_quote_length (*arg);
157 command = (char *) xallocsa (command_length);
159 /* Don't shell_quote $JAVA, because it may consist of a command
161 memcpy (p, java, strlen (java));
164 p = shell_quote_copy (p, class_name);
165 for (arg = args; *arg != NULL; arg++)
168 p = shell_quote_copy (p, *arg);
171 /* Ensure command_length was correctly calculated. */
172 if (p - command > command_length)
176 printf ("%s\n", command);
182 err = executer (java, "/bin/sh", argv, private_data);
186 /* Reset CLASSPATH. */
187 reset_classpath (old_classpath);
193 /* Unset the JAVA_HOME environment variable. */
194 old_JAVA_HOME = getenv ("JAVA_HOME");
195 if (old_JAVA_HOME != NULL)
197 old_JAVA_HOME = xstrdup (old_JAVA_HOME);
198 unsetenv ("JAVA_HOME");
202 static bool gij_tested;
203 static bool gij_present;
207 /* Test for presence of gij: "gij --version > /dev/null" */
212 argv[1] = "--version";
214 exitstatus = execute ("gij", "gij", argv, false, false, true, true,
216 gij_present = (exitstatus == 0);
223 char **argv = (char **) xallocsa ((2 + nargs + 1) * sizeof (char *));
228 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
232 argv[1] = (char *) class_name;
233 for (i = 0; i <= nargs; i++)
234 argv[2 + i] = (char *) args[i];
238 char *command = shell_quote_argv (argv);
239 printf ("%s\n", command);
243 err = executer ("gij", "gij", argv, private_data);
245 /* Reset CLASSPATH. */
246 reset_classpath (old_classpath);
255 static bool java_tested;
256 static bool java_present;
260 /* Test for presence of java: "java -version 2> /dev/null" */
265 argv[1] = "-version";
267 exitstatus = execute ("java", "java", argv, false, false, true, true,
269 java_present = (exitstatus == 0);
276 char **argv = (char **) xallocsa ((2 + nargs + 1) * sizeof (char *));
279 /* Set CLASSPATH. We don't use the "-classpath ..." option because
280 in JDK 1.1.x its argument should also contain the JDK's classes.zip,
281 but we don't know its location. (In JDK 1.3.0 it would work.) */
283 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
287 argv[1] = (char *) class_name;
288 for (i = 0; i <= nargs; i++)
289 argv[2 + i] = (char *) args[i];
293 char *command = shell_quote_argv (argv);
294 printf ("%s\n", command);
298 err = executer ("java", "java", argv, private_data);
300 /* Reset CLASSPATH. */
301 reset_classpath (old_classpath);
310 static bool jre_tested;
311 static bool jre_present;
315 /* Test for presence of jre: "jre 2> /dev/null ; test $? = 1" */
321 exitstatus = execute ("jre", "jre", argv, false, false, true, true,
323 jre_present = (exitstatus == 0 || exitstatus == 1);
330 char **argv = (char **) xallocsa ((2 + nargs + 1) * sizeof (char *));
333 /* Set CLASSPATH. We don't use the "-classpath ..." option because
334 in JDK 1.1.x its argument should also contain the JDK's classes.zip,
335 but we don't know its location. */
337 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
341 argv[1] = (char *) class_name;
342 for (i = 0; i <= nargs; i++)
343 argv[2 + i] = (char *) args[i];
347 char *command = shell_quote_argv (argv);
348 printf ("%s\n", command);
352 err = executer ("jre", "jre", argv, private_data);
354 /* Reset CLASSPATH. */
355 reset_classpath (old_classpath);
363 #if defined _WIN32 || defined __WIN32__
366 static bool jview_tested;
367 static bool jview_present;
371 /* Test for presence of jview: "jview -? >nul ; test $? = 1" */
378 exitstatus = execute ("jview", "jview", argv, false, false, true, true,
380 jview_present = (exitstatus == 0 || exitstatus == 1);
387 char **argv = (char **) xallocsa ((2 + nargs + 1) * sizeof (char *));
392 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
396 argv[1] = (char *) class_name;
397 for (i = 0; i <= nargs; i++)
398 argv[2 + i] = (char *) args[i];
402 char *command = shell_quote_argv (argv);
403 printf ("%s\n", command);
407 err = executer ("jview", "jview", argv, private_data);
409 /* Reset CLASSPATH. */
410 reset_classpath (old_classpath);
420 error (0, 0, _("Java virtual machine not found, try installing gij or set $JAVA"));
424 if (old_JAVA_HOME != NULL)
426 xsetenv ("JAVA_HOME", old_JAVA_HOME, 1);
427 free (old_JAVA_HOME);