1 /* Execute a Java program.
2 Copyright (C) 2001-2003, 2006-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
30 #include "classpath.h"
39 #define _(str) gettext (str)
42 /* Survey of Java virtual machines.
44 A = does it work without CLASSPATH being set
45 B = does it work with CLASSPATH being set to empty
46 C = option to set CLASSPATH, other than setting it in the environment
51 $JAVA unknown N Y n/a true
52 gij GCC 3.0 Y Y n/a gij --version >/dev/null
53 java JDK 1.1.8 Y Y -classpath P java -version 2>/dev/null
54 jre JDK 1.1.8 N Y -classpath P jre 2>/dev/null; test $? = 1
55 java JDK 1.3.0 Y Y -classpath P java -version 2>/dev/null
56 jview MS IE Y Y -cp P jview -? >nul; %errorlevel% = 1
58 The CLASSPATH is a colon separated list of pathnames. (On Windows: a
59 semicolon separated list of pathnames.)
61 We try the Java virtual machines in the following order:
62 1. getenv ("JAVA"), because the user must be able to override our
64 2. "gij", because it is a completely free JVM,
65 3. "java", because it is a standard JVM,
66 4. "jre", comes last because it requires a CLASSPATH environment variable,
67 5. "jview", on Windows only, because it is frequently installed.
69 We unset the JAVA_HOME environment variable, because a wrong setting of
70 this variable can confuse the JDK's javac.
74 execute_java_class (const char *class_name,
75 const char * const *classpaths,
76 unsigned int classpaths_count,
77 bool use_minimal_classpath,
79 const char * const *args,
80 bool verbose, bool quiet,
81 execute_fn *executer, void *private_data)
89 const char * const *arg;
91 for (nargs = 0, arg = args; *arg != NULL; nargs++, arg++)
95 /* First, try a class compiled to a native code executable. */
98 char *exe_pathname = concatenated_filename (exe_dir, class_name, EXEEXT);
100 char **argv = (char **) xallocsa ((1 + nargs + 1) * sizeof (char *));
105 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
108 argv[0] = exe_pathname;
109 for (i = 0; i <= nargs; i++)
110 argv[1 + i] = (char *) args[i];
114 char *command = shell_quote_argv (argv);
115 printf ("%s\n", command);
119 err = executer (class_name, exe_pathname, argv, private_data);
121 /* Reset CLASSPATH. */
122 reset_classpath (old_classpath);
130 const char *java = getenv ("JAVA");
131 if (java != NULL && java[0] != '\0')
133 /* Because $JAVA may consist of a command and options, we use the
134 shell. Because $JAVA has been set by the user, we leave all
135 all environment variables in place, including JAVA_HOME, and
136 we don't erase the user's CLASSPATH. */
138 unsigned int command_length;
141 const char * const *arg;
146 set_classpath (classpaths, classpaths_count, false,
149 command_length = strlen (java);
150 command_length += 1 + shell_quote_length (class_name);
151 for (arg = args; *arg != NULL; arg++)
152 command_length += 1 + shell_quote_length (*arg);
155 command = (char *) xallocsa (command_length);
157 /* Don't shell_quote $JAVA, because it may consist of a command
159 memcpy (p, java, strlen (java));
162 p = shell_quote_copy (p, class_name);
163 for (arg = args; *arg != NULL; arg++)
166 p = shell_quote_copy (p, *arg);
169 /* Ensure command_length was correctly calculated. */
170 if (p - command > command_length)
174 printf ("%s\n", command);
180 err = executer (java, "/bin/sh", argv, private_data);
184 /* Reset CLASSPATH. */
185 reset_classpath (old_classpath);
191 /* Unset the JAVA_HOME environment variable. */
192 old_JAVA_HOME = getenv ("JAVA_HOME");
193 if (old_JAVA_HOME != NULL)
195 old_JAVA_HOME = xstrdup (old_JAVA_HOME);
196 unsetenv ("JAVA_HOME");
200 static bool gij_tested;
201 static bool gij_present;
205 /* Test for presence of gij: "gij --version > /dev/null" */
210 argv[1] = "--version";
212 exitstatus = execute ("gij", "gij", argv, false, false, true, true,
214 gij_present = (exitstatus == 0);
221 char **argv = (char **) xallocsa ((2 + nargs + 1) * sizeof (char *));
226 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
230 argv[1] = (char *) class_name;
231 for (i = 0; i <= nargs; i++)
232 argv[2 + i] = (char *) args[i];
236 char *command = shell_quote_argv (argv);
237 printf ("%s\n", command);
241 err = executer ("gij", "gij", argv, private_data);
243 /* Reset CLASSPATH. */
244 reset_classpath (old_classpath);
253 static bool java_tested;
254 static bool java_present;
258 /* Test for presence of java: "java -version 2> /dev/null" */
263 argv[1] = "-version";
265 exitstatus = execute ("java", "java", argv, false, false, true, true,
267 java_present = (exitstatus == 0);
274 char **argv = (char **) xallocsa ((2 + nargs + 1) * sizeof (char *));
277 /* Set CLASSPATH. We don't use the "-classpath ..." option because
278 in JDK 1.1.x its argument should also contain the JDK's classes.zip,
279 but we don't know its location. (In JDK 1.3.0 it would work.) */
281 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
285 argv[1] = (char *) class_name;
286 for (i = 0; i <= nargs; i++)
287 argv[2 + i] = (char *) args[i];
291 char *command = shell_quote_argv (argv);
292 printf ("%s\n", command);
296 err = executer ("java", "java", argv, private_data);
298 /* Reset CLASSPATH. */
299 reset_classpath (old_classpath);
308 static bool jre_tested;
309 static bool jre_present;
313 /* Test for presence of jre: "jre 2> /dev/null ; test $? = 1" */
319 exitstatus = execute ("jre", "jre", argv, false, false, true, true,
321 jre_present = (exitstatus == 0 || exitstatus == 1);
328 char **argv = (char **) xallocsa ((2 + nargs + 1) * sizeof (char *));
331 /* Set CLASSPATH. We don't use the "-classpath ..." option because
332 in JDK 1.1.x its argument should also contain the JDK's classes.zip,
333 but we don't know its location. */
335 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
339 argv[1] = (char *) class_name;
340 for (i = 0; i <= nargs; i++)
341 argv[2 + i] = (char *) args[i];
345 char *command = shell_quote_argv (argv);
346 printf ("%s\n", command);
350 err = executer ("jre", "jre", argv, private_data);
352 /* Reset CLASSPATH. */
353 reset_classpath (old_classpath);
361 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
364 static bool jview_tested;
365 static bool jview_present;
369 /* Test for presence of jview: "jview -? >nul ; test $? = 1" */
376 exitstatus = execute ("jview", "jview", argv, false, false, true, true,
378 jview_present = (exitstatus == 0 || exitstatus == 1);
385 char **argv = (char **) xallocsa ((2 + nargs + 1) * sizeof (char *));
390 set_classpath (classpaths, classpaths_count, use_minimal_classpath,
394 argv[1] = (char *) class_name;
395 for (i = 0; i <= nargs; i++)
396 argv[2 + i] = (char *) args[i];
400 char *command = shell_quote_argv (argv);
401 printf ("%s\n", command);
405 err = executer ("jview", "jview", argv, private_data);
407 /* Reset CLASSPATH. */
408 reset_classpath (old_classpath);
418 error (0, 0, _("Java virtual machine not found, try installing gij or set $JAVA"));
422 if (old_JAVA_HOME != NULL)
424 xsetenv ("JAVA_HOME", old_JAVA_HOME, 1);
425 free (old_JAVA_HOME);