KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Java


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.util.Vector JavaDoc;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.ExitException;
28 import org.apache.tools.ant.Project;
29 import org.apache.tools.ant.Task;
30 import org.apache.tools.ant.ExitStatusException;
31 import org.apache.tools.ant.types.Commandline;
32 import org.apache.tools.ant.types.CommandlineJava;
33 import org.apache.tools.ant.types.Environment;
34 import org.apache.tools.ant.types.Path;
35 import org.apache.tools.ant.types.PropertySet;
36 import org.apache.tools.ant.types.Reference;
37 import org.apache.tools.ant.types.Assertions;
38 import org.apache.tools.ant.types.Permissions;
39 import org.apache.tools.ant.types.RedirectorElement;
40 import org.apache.tools.ant.taskdefs.condition.Os;
41 import org.apache.tools.ant.util.KeepAliveInputStream;
42
43 /**
44  * Launcher for Java applications. Allows use of
45  * the same JVM for the called application thus resulting in much
46  * faster operation.
47  *
48  * @since Ant 1.1
49  *
50  * @ant.task category="java"
51  */

52 public class Java extends Task {
53
54     private CommandlineJava cmdl = new CommandlineJava();
55     private Environment env = new Environment();
56     private boolean fork = false;
57     private boolean newEnvironment = false;
58     private File JavaDoc dir = null;
59     private boolean failOnError = false;
60     private Long JavaDoc timeout = null;
61
62     //include locally for screening purposes
63
private String JavaDoc inputString;
64     private File JavaDoc input;
65     private File JavaDoc output;
66     private File JavaDoc error;
67
68     // CheckStyle:VisibilityModifier OFF - bc
69
protected Redirector redirector = new Redirector(this);
70     protected RedirectorElement redirectorElement;
71     // CheckStyle:VisibilityModifier ON
72

73     private String JavaDoc resultProperty;
74     private Permissions perm = null;
75
76     private boolean spawn = false;
77     private boolean incompatibleWithSpawn = false;
78
79     /**
80      * Normal constructor
81      */

82     public Java() {
83     }
84
85     /**
86      * create a bound task
87      * @param owner owner
88      */

89     public Java(Task owner) {
90         bindToOwner(owner);
91     }
92
93     /**
94      * Do the execution.
95      * @throws BuildException if failOnError is set to true and the application
96      * returns a nonzero result code.
97      */

98     public void execute() throws BuildException {
99         File JavaDoc savedDir = dir;
100         Permissions savedPermissions = perm;
101
102         int err = -1;
103         try {
104             err = executeJava();
105             if (err != 0) {
106                 if (failOnError) {
107                     throw new ExitStatusException("Java returned: " + err,
108                             err,
109                             getLocation());
110                 } else {
111                     log("Java Result: " + err, Project.MSG_ERR);
112                 }
113             }
114             maybeSetResultPropertyValue(err);
115         } finally {
116             dir = savedDir;
117             perm = savedPermissions;
118         }
119     }
120
121     /**
122      * Do the execution and return a return code.
123      *
124      * @return the return code from the execute java class if it was
125      * executed in a separate VM (fork = "yes") or a security manager was
126      * installed that prohibits ExitVM (default).
127      *
128      * @throws BuildException if required parameters are missing.
129      */

130     public int executeJava() throws BuildException {
131         String JavaDoc classname = getCommandLine().getClassname();
132         if (classname == null && getCommandLine().getJar() == null) {
133             throw new BuildException("Classname must not be null.");
134         }
135         if (!fork && getCommandLine().getJar() != null) {
136             throw new BuildException("Cannot execute a jar in non-forked mode."
137                                      + " Please set fork='true'. ");
138         }
139         if (spawn && !fork) {
140             throw new BuildException("Cannot spawn a java process in non-forked mode."
141                                      + " Please set fork='true'. ");
142         }
143         if (getCommandLine().getClasspath() != null
144             && getCommandLine().getJar() != null) {
145             log("When using 'jar' attribute classpath-settings are ignored. "
146                 + "See the manual for more information.", Project.MSG_VERBOSE);
147         }
148         if (spawn && incompatibleWithSpawn) {
149             getProject().log("spawn does not allow attributes related to input, "
150             + "output, error, result", Project.MSG_ERR);
151             getProject().log("spawn also does not allow timeout", Project.MSG_ERR);
152             getProject().log("finally, spawn is not compatible "
153                 + "with a nested I/O <redirector>", Project.MSG_ERR);
154             throw new BuildException("You have used an attribute "
155                 + "or nested element which is not compatible with spawn");
156         }
157         if (getCommandLine().getAssertions() != null && !fork) {
158             log("Assertion statements are currently ignored in non-forked mode");
159         }
160         if (fork) {
161             if (perm != null) {
162                 log("Permissions can not be set this way in forked mode.", Project.MSG_WARN);
163             }
164             log(getCommandLine().describeCommand(), Project.MSG_VERBOSE);
165         } else {
166             if (getCommandLine().getVmCommand().size() > 1) {
167                 log("JVM args ignored when same JVM is used.",
168                     Project.MSG_WARN);
169             }
170             if (dir != null) {
171                 log("Working directory ignored when same JVM is used.",
172                     Project.MSG_WARN);
173             }
174             if (newEnvironment || null != env.getVariables()) {
175                 log("Changes to environment variables are ignored when same "
176                     + "JVM is used.", Project.MSG_WARN);
177             }
178             if (getCommandLine().getBootclasspath() != null) {
179                 log("bootclasspath ignored when same JVM is used.",
180                     Project.MSG_WARN);
181             }
182             if (perm == null) {
183                 perm = new Permissions(true);
184                 log("running " + this.getCommandLine().getClassname()
185                     + " with default permissions (exit forbidden)", Project.MSG_VERBOSE);
186             }
187             log("Running in same VM " + getCommandLine().describeJavaCommand(),
188                 Project.MSG_VERBOSE);
189         }
190         setupRedirector();
191         try {
192             if (fork) {
193                 if (!spawn) {
194                     return fork(getCommandLine().getCommandline());
195                 } else {
196                     spawn(getCommandLine().getCommandline());
197                     return 0;
198                 }
199             } else {
200                 try {
201                     run(getCommandLine());
202                     return 0;
203                 } catch (ExitException ex) {
204                     return ex.getStatus();
205                 }
206             }
207         } catch (BuildException e) {
208             if (e.getLocation() == null && getLocation() != null) {
209                 e.setLocation(getLocation());
210             }
211             if (failOnError) {
212                 throw e;
213             } else {
214                 log(e);
215                 return 0;
216             }
217         } catch (ThreadDeath JavaDoc t) {
218             throw t; // cf. NB #47191
219
} catch (Throwable JavaDoc t) {
220             if (failOnError) {
221                 throw new BuildException(t, getLocation());
222             } else {
223                 log(t);
224                 return 0;
225             }
226         }
227     }
228
229     /**
230      * Set whether or not you want the process to be spawned;
231      * default is not spawned.
232      * @param spawn if true you do not want Ant to wait for the end of the process.
233      * @since Ant 1.6
234      */

235     public void setSpawn(boolean spawn) {
236         this.spawn = spawn;
237     }
238
239     /**
240      * Set the classpath to be used when running the Java class.
241      *
242      * @param s an Ant Path object containing the classpath.
243      */

244     public void setClasspath(Path s) {
245         createClasspath().append(s);
246     }
247
248     /**
249      * Add a path to the classpath.
250      *
251      * @return created classpath.
252      */

253     public Path createClasspath() {
254         return getCommandLine().createClasspath(getProject()).createPath();
255     }
256
257     /**
258      * Add a path to the bootclasspath.
259      * @since Ant 1.6
260      *
261      * @return created bootclasspath.
262      */

263     public Path createBootclasspath() {
264         return getCommandLine().createBootclasspath(getProject()).createPath();
265     }
266
267     /**
268      * Set the permissions for the application run inside the same JVM.
269      * @since Ant 1.6
270      * @return Permissions.
271      */

272     public Permissions createPermissions() {
273         perm = (perm == null) ? new Permissions() : perm;
274         return perm;
275     }
276
277     /**
278      * Set the classpath to use by reference.
279      *
280      * @param r a reference to an existing classpath.
281      */

282     public void setClasspathRef(Reference r) {
283         createClasspath().setRefid(r);
284     }
285
286     /**
287      * Set the location of the JAR file to execute.
288      *
289      * @param jarfile the jarfile to execute.
290      *
291      * @throws BuildException if there is also a main class specified.
292      */

293     public void setJar(File JavaDoc jarfile) throws BuildException {
294         if (getCommandLine().getClassname() != null) {
295             throw new BuildException("Cannot use 'jar' and 'classname' "
296                                      + "attributes in same command.");
297         }
298         getCommandLine().setJar(jarfile.getAbsolutePath());
299     }
300
301     /**
302      * Set the Java class to execute.
303      *
304      * @param s the name of the main class.
305      *
306      * @throws BuildException if the jar attribute has been set.
307      */

308     public void setClassname(String JavaDoc s) throws BuildException {
309         if (getCommandLine().getJar() != null) {
310             throw new BuildException("Cannot use 'jar' and 'classname' "
311                                      + "attributes in same command");
312         }
313         getCommandLine().setClassname(s);
314     }
315
316     /**
317      * Deprecated: use nested arg instead.
318      * Set the command line arguments for the class.
319      *
320      * @param s arguments.
321      *
322      * @ant.attribute ignore="true"
323      */

324     public void setArgs(String JavaDoc s) {
325         log("The args attribute is deprecated. "
326             + "Please use nested arg elements.", Project.MSG_WARN);
327         getCommandLine().createArgument().setLine(s);
328     }
329
330     /**
331      * If set, system properties will be copied to the cloned VM--as
332      * well as the bootclasspath unless you have explicitly specified
333      * a bootclaspath.
334      *
335      * <p>Doesn't have any effect unless fork is true.</p>
336      * @param cloneVm if true copy system properties.
337      * @since Ant 1.7
338      */

339     public void setCloneVm(boolean cloneVm) {
340         getCommandLine().setCloneVm(cloneVm);
341     }
342
343     /**
344      * Add a command-line argument.
345      *
346      * @return created argument.
347      */

348     public Commandline.Argument createArg() {
349         return getCommandLine().createArgument();
350     }
351
352     /**
353      * Set the name of the property in which the return code of the
354      * command should be stored. Only of interest if failonerror=false.
355      *
356      * @param resultProperty name of property.
357      *
358      * @since Ant 1.6
359      */

360     public void setResultProperty(String JavaDoc resultProperty) {
361         this.resultProperty = resultProperty;
362         incompatibleWithSpawn = true;
363     }
364
365     /**
366      * Helper method to set result property to the
367      * passed in value if appropriate.
368      *
369      * @param result the exit code
370      */

371     protected void maybeSetResultPropertyValue(int result) {
372         String JavaDoc res = Integer.toString(result);
373         if (resultProperty != null) {
374             getProject().setNewProperty(resultProperty, res);
375         }
376     }
377
378     /**
379      * If true, execute in a new VM.
380      *
381      * @param s do you want to run Java in a new VM.
382      */

383     public void setFork(boolean s) {
384         this.fork = s;
385     }
386
387     /**
388      * Set the command line arguments for the JVM.
389      *
390      * @param s jvmargs.
391      */

392     public void setJvmargs(String JavaDoc s) {
393         log("The jvmargs attribute is deprecated. "
394             + "Please use nested jvmarg elements.", Project.MSG_WARN);
395         getCommandLine().createVmArgument().setLine(s);
396     }
397
398     /**
399      * Adds a JVM argument.
400      *
401      * @return JVM argument created.
402      */

403     public Commandline.Argument createJvmarg() {
404         return getCommandLine().createVmArgument();
405     }
406
407     /**
408      * Set the command used to start the VM (only if forking).
409      *
410      * @param s command to start the VM.
411      */

412     public void setJvm(String JavaDoc s) {
413         getCommandLine().setVm(s);
414     }
415
416     /**
417      * Add a system property.
418      *
419      * @param sysp system property.
420      */

421     public void addSysproperty(Environment.Variable sysp) {
422         getCommandLine().addSysproperty(sysp);
423     }
424
425     /**
426      * Add a set of properties as system properties.
427      *
428      * @param sysp set of properties to add.
429      *
430      * @since Ant 1.6
431      */

432     public void addSyspropertyset(PropertySet sysp) {
433         getCommandLine().addSyspropertyset(sysp);
434     }
435
436     /**
437      * If true, then fail if the command exits with a
438      * returncode other than zero.
439      *
440      * @param fail if true fail the build when the command exits with a
441      * nonzero returncode.
442      */

443     public void setFailonerror(boolean fail) {
444         failOnError = fail;
445         incompatibleWithSpawn |= fail;
446     }
447
448     /**
449      * Set the working directory of the process.
450      *
451      * @param d working directory.
452      *
453      */

454     public void setDir(File JavaDoc d) {
455         this.dir = d;
456     }
457
458     /**
459      * Set the File to which the output of the process is redirected.
460      *
461      * @param out the output File.
462      */

463     public void setOutput(File JavaDoc out) {
464         this.output = out;
465         incompatibleWithSpawn = true;
466     }
467
468     /**
469      * Set the input to use for the task.
470      *
471      * @param input name of the input file.
472      */

473     public void setInput(File JavaDoc input) {
474         if (inputString != null) {
475             throw new BuildException("The \"input\" and \"inputstring\" "
476                 + "attributes cannot both be specified");
477         }
478         this.input = input;
479         incompatibleWithSpawn = true;
480     }
481
482     /**
483      * Set the string to use as input.
484      *
485      * @param inputString the string which is used as the input source.
486      */

487     public void setInputString(String JavaDoc inputString) {
488         if (input != null) {
489             throw new BuildException("The \"input\" and \"inputstring\" "
490                 + "attributes cannot both be specified");
491         }
492         this.inputString = inputString;
493         incompatibleWithSpawn = true;
494     }
495
496     /**
497      * Set whether error output of exec is logged. This is only useful
498      * when output is being redirected and error output is desired in the
499      * Ant log.
500      *
501      * @param logError get in the ant log the messages coming from stderr
502      * in the case that fork = true.
503      */

504     public void setLogError(boolean logError) {
505         redirector.setLogError(logError);
506         incompatibleWithSpawn |= logError;
507     }
508
509     /**
510      * Set the File to which the error stream of the process is redirected.
511      *
512      * @param error file getting the error stream.
513      *
514      * @since Ant 1.6
515      */

516     public void setError(File JavaDoc error) {
517         this.error = error;
518         incompatibleWithSpawn = true;
519     }
520
521     /**
522      * Set the property name whose value should be set to the output of
523      * the process.
524      *
525      * @param outputProp property name.
526      *
527      */

528     public void setOutputproperty(String JavaDoc outputProp) {
529         redirector.setOutputProperty(outputProp);
530         incompatibleWithSpawn = true;
531     }
532
533     /**
534      * Set the property name whose value should be set to the error of
535      * the process.
536      *
537      * @param errorProperty property name.
538      *
539      * @since Ant 1.6
540      */

541     public void setErrorProperty(String JavaDoc errorProperty) {
542         redirector.setErrorProperty(errorProperty);
543         incompatibleWithSpawn = true;
544     }
545
546     /**
547      * Corresponds to -mx or -Xmx depending on VM version.
548      *
549      * @param max max memory parameter.
550      */

551     public void setMaxmemory(String JavaDoc max) {
552         getCommandLine().setMaxmemory(max);
553     }
554
555     /**
556      * Set the JVM version.
557      * @param value JVM version.
558      */

559     public void setJVMVersion(String JavaDoc value) {
560         getCommandLine().setVmversion(value);
561     }
562
563     /**
564      * Add an environment variable.
565      *
566      * <p>Will be ignored if we are not forking a new VM.
567      *
568      * @param var new environment variable.
569      *
570      * @since Ant 1.5
571      */

572     public void addEnv(Environment.Variable var) {
573         env.addVariable(var);
574     }
575
576     /**
577      * If true, use a completely new environment.
578      *
579      * <p>Will be ignored if we are not forking a new VM.
580      *
581      * @param newenv if true, use a completely new environment.
582      *
583      * @since Ant 1.5
584      */

585     public void setNewenvironment(boolean newenv) {
586         newEnvironment = newenv;
587     }
588
589     /**
590      * If true, append output to existing file.
591      *
592      * @param append if true, append output to existing file.
593      *
594      * @since Ant 1.5
595      */

596     public void setAppend(boolean append) {
597         redirector.setAppend(append);
598         incompatibleWithSpawn = true;
599     }
600
601     /**
602      * Set the timeout in milliseconds after which the process will be killed.
603      *
604      * @param value timeout in milliseconds.
605      *
606      * @since Ant 1.5
607      */

608     public void setTimeout(Long JavaDoc value) {
609         timeout = value;
610         incompatibleWithSpawn |= timeout != null;
611     }
612
613     /**
614      * Add assertions to enable in this program (if fork=true).
615      * @param asserts assertion set.
616      * @since Ant 1.6
617      */

618     public void addAssertions(Assertions asserts) {
619         if (getCommandLine().getAssertions() != null) {
620             throw new BuildException("Only one assertion declaration is allowed");
621         }
622         getCommandLine().setAssertions(asserts);
623     }
624
625     /**
626      * Add a <code>RedirectorElement</code> to this task.
627      * @param redirectorElement <code>RedirectorElement</code>.
628      */

629     public void addConfiguredRedirector(RedirectorElement redirectorElement) {
630         if (this.redirectorElement != null) {
631             throw new BuildException("cannot have > 1 nested redirectors");
632         }
633         this.redirectorElement = redirectorElement;
634         incompatibleWithSpawn = true;
635     }
636
637     /**
638      * Pass output sent to System.out to specified output file.
639      *
640      * @param output a string of output on its way to the handlers.
641      *
642      * @since Ant 1.5
643      */

644     protected void handleOutput(String JavaDoc output) {
645         if (redirector.getOutputStream() != null) {
646             redirector.handleOutput(output);
647         } else {
648             super.handleOutput(output);
649         }
650     }
651
652     /**
653      * Handle an input request by this task.
654      *
655      * @param buffer the buffer into which data is to be read.
656      * @param offset the offset into the buffer at which data is stored.
657      * @param length the amount of data to read.
658      *
659      * @return the number of bytes read.
660      *
661      * @exception IOException if the data cannot be read.
662      * @since Ant 1.6
663      */

664     public int handleInput(byte[] buffer, int offset, int length)
665         throws IOException JavaDoc {
666         // Should work whether or not redirector.inputStream == null:
667
return redirector.handleInput(buffer, offset, length);
668     }
669
670     /**
671      * Pass output sent to System.out to specified output file.
672      *
673      * @param output string of output on its way to its handlers.
674      *
675      * @since Ant 1.5.2
676      */

677     protected void handleFlush(String JavaDoc output) {
678         if (redirector.getOutputStream() != null) {
679             redirector.handleFlush(output);
680         } else {
681             super.handleFlush(output);
682         }
683     }
684
685     /**
686      * Handle output sent to System.err.
687      *
688      * @param output string of stderr.
689      *
690      * @since Ant 1.5
691      */

692     protected void handleErrorOutput(String JavaDoc output) {
693         if (redirector.getErrorStream() != null) {
694             redirector.handleErrorOutput(output);
695         } else {
696             super.handleErrorOutput(output);
697         }
698     }
699
700     /**
701      * Handle output sent to System.err and flush the stream.
702      *
703      * @param output string of stderr.
704      *
705      * @since Ant 1.5.2
706      */

707     protected void handleErrorFlush(String JavaDoc output) {
708         if (redirector.getErrorStream() != null) {
709             redirector.handleErrorFlush(output);
710         } else {
711             super.handleErrorOutput(output);
712         }
713     }
714
715     /**
716      * Set up properties on the redirector that we needed to store locally.
717      */

718     protected void setupRedirector() {
719         redirector.setInput(input);
720         redirector.setInputString(inputString);
721         redirector.setOutput(output);
722         redirector.setError(error);
723         if (redirectorElement != null) {
724             redirectorElement.configure(redirector);
725         }
726         if (!spawn && input == null && inputString == null) {
727             // #24918: send standard input to the process by default.
728
redirector.setInputStream(
729                 new KeepAliveInputStream(getProject().getDefaultInputStream()));
730         }
731     }
732
733     /**
734      * Executes the given classname with the given arguments as it
735      * were a command line application.
736      * @param command CommandlineJava.
737      */

738     private void run(CommandlineJava command) throws BuildException {
739         try {
740             ExecuteJava exe = new ExecuteJava();
741             exe.setJavaCommand(command.getJavaCommand());
742             exe.setClasspath(command.getClasspath());
743             exe.setSystemProperties(command.getSystemProperties());
744             exe.setPermissions(perm);
745             exe.setTimeout(timeout);
746             redirector.createStreams();
747             exe.execute(getProject());
748             redirector.complete();
749             if (exe.killedProcess()) {
750                 throw new BuildException("Timeout: killed the sub-process");
751             }
752         } catch (IOException JavaDoc e) {
753             throw new BuildException(e);
754         }
755     }
756
757     /**
758      * Executes the given classname with the given arguments in a separate VM.
759      * @param command String[] of command-line arguments.
760      */

761     private int fork(String JavaDoc[] command) throws BuildException {
762         Execute exe
763             = new Execute(redirector.createHandler(), createWatchdog());
764         setupExecutable(exe, command);
765
766         try {
767             int rc = exe.execute();
768             redirector.complete();
769             if (exe.killedProcess()) {
770                 throw new BuildException("Timeout: killed the sub-process");
771             }
772             return rc;
773         } catch (IOException JavaDoc e) {
774             throw new BuildException(e, getLocation());
775         }
776     }
777
778     /**
779      * Executes the given classname with the given arguments in a separate VM.
780      * @param command String[] of command-line arguments.
781      */

782     private void spawn(String JavaDoc[] command) throws BuildException {
783         Execute exe = new Execute();
784         setupExecutable(exe, command);
785         try {
786             exe.spawn();
787         } catch (IOException JavaDoc e) {
788             throw new BuildException(e, getLocation());
789         }
790     }
791
792     /**
793      * Do all configuration for an executable that
794      * is common across the {@link #fork(String[])} and
795      * {@link #spawn(String[])} methods.
796      * @param exe executable.
797      * @param command command to execute.
798      */

799     private void setupExecutable(Execute exe, String JavaDoc[] command) {
800         exe.setAntRun(getProject());
801         setupWorkingDir(exe);
802         setupEnvironment(exe);
803         setupCommandLine(exe, command);
804     }
805
806     /**
807      * Set up our environment variables.
808      * @param exe executable.
809      */

810     private void setupEnvironment(Execute exe) {
811         String JavaDoc[] environment = env.getVariables();
812         if (environment != null) {
813             for (int i = 0; i < environment.length; i++) {
814                 log("Setting environment variable: " + environment[i],
815                     Project.MSG_VERBOSE);
816             }
817         }
818         exe.setNewenvironment(newEnvironment);
819         exe.setEnvironment(environment);
820     }
821
822     /**
823      * Set the working dir of the new process.
824      * @param exe executable.
825      * @throws BuildException if the dir doesn't exist.
826      */

827     private void setupWorkingDir(Execute exe) {
828         if (dir == null) {
829             dir = getProject().getBaseDir();
830         } else if (!dir.exists() || !dir.isDirectory()) {
831             throw new BuildException(dir.getAbsolutePath()
832                                      + " is not a valid directory",
833                                      getLocation());
834         }
835         exe.setWorkingDirectory(dir);
836     }
837
838     /**
839      * Set the command line for the exe.
840      * On VMS, hands off to {@link #setupCommandLineForVMS(Execute, String[])}.
841      * @param exe executable.
842      * @param command command to execute.
843      */

844     private void setupCommandLine(Execute exe, String JavaDoc[] command) {
845         //On VMS platform, we need to create a special java options file
846
//containing the arguments and classpath for the java command.
847
//The special file is supported by the "-V" switch on the VMS JVM.
848
if (Os.isFamily("openvms")) {
849             setupCommandLineForVMS(exe, command);
850         } else {
851             exe.setCommandline(command);
852         }
853     }
854
855     /**
856      * On VMS platform, we need to create a special java options file
857      * containing the arguments and classpath for the java command.
858      * The special file is supported by the "-V" switch on the VMS JVM.
859      *
860      * @param exe executable.
861      * @param command command to execute.
862      */

863     private void setupCommandLineForVMS(Execute exe, String JavaDoc[] command) {
864         ExecuteJava.setupCommandLineForVMS(exe, command);
865     }
866
867     /**
868      * Executes the given classname with the given arguments as if it
869      * were a command line application.
870      *
871      * @param classname the name of the class to run.
872      * @param args arguments for the class.
873      * @throws BuildException in case of IOException in the execution.
874      */

875     protected void run(String JavaDoc classname, Vector JavaDoc args) throws BuildException {
876         CommandlineJava cmdj = new CommandlineJava();
877         cmdj.setClassname(classname);
878         for (int i = 0; i < args.size(); i++) {
879             cmdj.createArgument().setValue((String JavaDoc) args.elementAt(i));
880         }
881         run(cmdj);
882     }
883
884     /**
885      * Clear out the arguments to this java task.
886      */

887     public void clearArgs() {
888         getCommandLine().clearJavaArgs();
889     }
890
891     /**
892      * Create the Watchdog to kill a runaway process.
893      *
894      * @return new watchdog.
895      *
896      * @throws BuildException under unknown circumstances.
897      *
898      * @since Ant 1.5
899      */

900     protected ExecuteWatchdog createWatchdog() throws BuildException {
901         if (timeout == null) {
902             return null;
903         }
904         return new ExecuteWatchdog(timeout.longValue());
905     }
906
907     /**
908      * Log the specified Throwable.
909      * @param t the Throwable to log.
910      * @since 1.6.2
911      */

912     private void log(Throwable JavaDoc t) {
913         StringWriter JavaDoc sw = new StringWriter JavaDoc();
914         PrintWriter JavaDoc w = new PrintWriter JavaDoc(sw);
915         t.printStackTrace(w);
916         w.close();
917         log(sw.toString(), Project.MSG_ERR);
918     }
919
920     /**
921      * Accessor to the command line.
922      *
923      * @return the current command line.
924      * @since 1.6.3
925      */

926     public CommandlineJava getCommandLine() {
927         return cmdl;
928     }
929
930     /**
931      * Get the system properties of the command line.
932      *
933      * @return the current properties of this java invocation.
934      * @since 1.6.3
935      */

936     public CommandlineJava.SysProperties getSysProperties() {
937         return getCommandLine().getSystemProperties();
938     }
939 }
940
Popular Tags