You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Java.java 24 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "Ant" and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import java.io.File;
  56. import java.io.IOException;
  57. import java.util.Vector;
  58. import org.apache.tools.ant.BuildException;
  59. import org.apache.tools.ant.ExitException;
  60. import org.apache.tools.ant.Project;
  61. import org.apache.tools.ant.Task;
  62. import org.apache.tools.ant.types.Commandline;
  63. import org.apache.tools.ant.types.CommandlineJava;
  64. import org.apache.tools.ant.types.Environment;
  65. import org.apache.tools.ant.types.Path;
  66. import org.apache.tools.ant.types.PropertySet;
  67. import org.apache.tools.ant.types.Reference;
  68. import org.apache.tools.ant.types.Assertions;
  69. import org.apache.tools.ant.types.Permissions;
  70. /**
  71. * Launcher for Java applications. Allows use of
  72. * the same JVM for the called application thus resulting in much
  73. * faster operation.
  74. *
  75. * @author Stefano Mazzocchi
  76. * <a href="mailto:stefano@apache.org">stefano@apache.org</a>
  77. * @author Stefan Bodewig
  78. * @author <a href="mailto:donal@savvion.com">Donal Quinlan</a>
  79. * @author <a href="mailto:martijn@kruithof.xs4all.nl">Martijn Kruithof</a>
  80. *
  81. * @since Ant 1.1
  82. *
  83. * @ant.task category="java"
  84. */
  85. public class Java extends Task {
  86. private CommandlineJava cmdl = new CommandlineJava();
  87. private Environment env = new Environment();
  88. private boolean fork = false;
  89. private boolean newEnvironment = false;
  90. private File dir = null;
  91. private boolean failOnError = false;
  92. private boolean append = false;
  93. private Long timeout = null;
  94. private Redirector redirector = new Redirector(this);
  95. private String resultProperty;
  96. private Permissions perm;
  97. private boolean spawn = false;
  98. private boolean incompatibleWithSpawn = false;
  99. /**
  100. * Do the execution.
  101. * @throws BuildException if failOnError is set to true and the application
  102. * returns a non 0 result code
  103. */
  104. public void execute() throws BuildException {
  105. File savedDir = dir;
  106. int err = -1;
  107. try {
  108. err = executeJava();
  109. if (err != 0) {
  110. if (failOnError) {
  111. throw new BuildException("Java returned: " + err, getLocation());
  112. } else {
  113. log("Java Result: " + err, Project.MSG_ERR);
  114. }
  115. }
  116. maybeSetResultPropertyValue(err);
  117. } finally {
  118. dir = savedDir;
  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").
  126. *
  127. * @throws BuildException if required parameters are missing
  128. */
  129. public int executeJava() throws BuildException {
  130. String classname = cmdl.getClassname();
  131. if (classname == null && cmdl.getJar() == null) {
  132. throw new BuildException("Classname must not be null.");
  133. }
  134. if (!fork && cmdl.getJar() != null) {
  135. throw new BuildException("Cannot execute a jar in non-forked mode."
  136. + " Please set fork='true'. ");
  137. }
  138. if (spawn && !fork) {
  139. throw new BuildException("Cannot spawn a java process in non-forked mode."
  140. + " Please set fork='true'. ");
  141. }
  142. if (spawn && incompatibleWithSpawn) {
  143. getProject().log("spawn does not allow attributes related to input, "
  144. + "output, error, result", Project.MSG_ERR);
  145. getProject().log("spawn does not also not allow timeout", Project.MSG_ERR);
  146. throw new BuildException("You have used an attribute which is "
  147. + "not compatible with spawn");
  148. }
  149. if (fork) {
  150. if(perm != null) {
  151. log("Permissions can not be set this way in forked mode.",Project.MSG_WARN);
  152. }
  153. log(cmdl.describeCommand(), Project.MSG_VERBOSE);
  154. } else {
  155. if (cmdl.getVmCommand().size() > 1) {
  156. log("JVM args ignored when same JVM is used.",
  157. Project.MSG_WARN);
  158. }
  159. if (dir != null) {
  160. log("Working directory ignored when same JVM is used.",
  161. Project.MSG_WARN);
  162. }
  163. if (newEnvironment || null != env.getVariables()) {
  164. log("Changes to environment variables are ignored when same "
  165. + "JVM is used.", Project.MSG_WARN);
  166. }
  167. if (cmdl.getBootclasspath() != null) {
  168. log("bootclasspath ignored when same JVM is used.",
  169. Project.MSG_WARN);
  170. }
  171. log("Running in same VM " + cmdl.describeJavaCommand(),
  172. Project.MSG_VERBOSE);
  173. }
  174. try {
  175. if (fork) {
  176. if (!spawn) {
  177. return fork(cmdl.getCommandline());
  178. } else {
  179. spawn(cmdl.getCommandline());
  180. return 0;
  181. }
  182. } else {
  183. try {
  184. run(cmdl);
  185. return 0;
  186. } catch (ExitException ex) {
  187. return ex.getStatus();
  188. }
  189. }
  190. } catch (BuildException e) {
  191. if (failOnError) {
  192. throw e;
  193. } else {
  194. log(e.getMessage(), Project.MSG_ERR);
  195. return 0;
  196. }
  197. } catch (Throwable t) {
  198. if (failOnError) {
  199. throw new BuildException(t);
  200. } else {
  201. log(t.getMessage(), Project.MSG_ERR);
  202. return 0;
  203. }
  204. }
  205. }
  206. /**
  207. * set whether or not you want the process to be spawned
  208. * default is not spawned
  209. * @param spawn if true you do not want ant to wait for the end of the process
  210. * @since ant 1.6
  211. */
  212. public void setSpawn(boolean spawn) {
  213. this.spawn = spawn;
  214. }
  215. /**
  216. * Set the classpath to be used when running the Java class
  217. *
  218. * @param s an Ant Path object containing the classpath.
  219. */
  220. public void setClasspath(Path s) {
  221. createClasspath().append(s);
  222. }
  223. /**
  224. * Adds a path to the classpath.
  225. *
  226. * @return created classpath
  227. */
  228. public Path createClasspath() {
  229. return cmdl.createClasspath(getProject()).createPath();
  230. }
  231. /**
  232. * Adds a path to the bootclasspath.
  233. * @since Ant 1.6
  234. *
  235. * @return created bootclasspath
  236. */
  237. public Path createBootclasspath() {
  238. return cmdl.createBootclasspath(getProject()).createPath();
  239. }
  240. /**
  241. * Sets the permissions for the application run inside the same JVM.
  242. * @since Ant 1.6
  243. * @return .
  244. */
  245. public Permissions createPermissions() {
  246. if (perm == null) {
  247. perm = new Permissions();
  248. }
  249. return perm;
  250. }
  251. /**
  252. * Classpath to use, by reference.
  253. *
  254. * @param r a reference to an existing classpath
  255. */
  256. public void setClasspathRef(Reference r) {
  257. createClasspath().setRefid(r);
  258. }
  259. /**
  260. * The location of the JAR file to execute.
  261. *
  262. * @param jarfile the jarfile that one wants to execute
  263. *
  264. * @throws BuildException if there is also a main class specified
  265. */
  266. public void setJar(File jarfile) throws BuildException {
  267. if (cmdl.getClassname() != null) {
  268. throw new BuildException("Cannot use 'jar' and 'classname' "
  269. + "attributes in same command.");
  270. }
  271. cmdl.setJar(jarfile.getAbsolutePath());
  272. }
  273. /**
  274. * Sets the Java class to execute.
  275. *
  276. * @param s the name of the main class
  277. *
  278. * @throws BuildException if the jar attribute has been set
  279. */
  280. public void setClassname(String s) throws BuildException {
  281. if (cmdl.getJar() != null) {
  282. throw new BuildException("Cannot use 'jar' and 'classname' "
  283. + "attributes in same command");
  284. }
  285. cmdl.setClassname(s);
  286. }
  287. /**
  288. * Deprecated: use nested arg instead.
  289. * Set the command line arguments for the class.
  290. *
  291. * @param s arguments
  292. *
  293. * @ant.attribute ignore="true"
  294. */
  295. public void setArgs(String s) {
  296. log("The args attribute is deprecated. "
  297. + "Please use nested arg elements.", Project.MSG_WARN);
  298. cmdl.createArgument().setLine(s);
  299. }
  300. /**
  301. * Adds a command-line argument.
  302. *
  303. * @return created argument
  304. */
  305. public Commandline.Argument createArg() {
  306. return cmdl.createArgument();
  307. }
  308. /**
  309. * The name of a property in which the return code of the
  310. * command should be stored. Only of interest if failonerror=false.
  311. *
  312. * @param resultProperty name of property
  313. *
  314. * @since Ant 1.6
  315. */
  316. public void setResultProperty(String resultProperty) {
  317. this.resultProperty = resultProperty;
  318. }
  319. /**
  320. * helper method to set result property to the
  321. * passed in value if appropriate
  322. *
  323. * @param result the exit code
  324. */
  325. protected void maybeSetResultPropertyValue(int result) {
  326. String res = Integer.toString(result);
  327. if (resultProperty != null) {
  328. getProject().setNewProperty(resultProperty, res);
  329. }
  330. }
  331. /**
  332. * If true, execute in a new VM.
  333. *
  334. * @param s do you want to run Java in a new VM.
  335. */
  336. public void setFork(boolean s) {
  337. this.fork = s;
  338. }
  339. /**
  340. * Set the command line arguments for the JVM.
  341. *
  342. * @param s jvmargs
  343. */
  344. public void setJvmargs(String s) {
  345. log("The jvmargs attribute is deprecated. "
  346. + "Please use nested jvmarg elements.", Project.MSG_WARN);
  347. cmdl.createVmArgument().setLine(s);
  348. }
  349. /**
  350. * Adds a JVM argument.
  351. *
  352. * @return JVM argument created
  353. */
  354. public Commandline.Argument createJvmarg() {
  355. return cmdl.createVmArgument();
  356. }
  357. /**
  358. * Set the command used to start the VM (only if not forking).
  359. *
  360. * @param s command to start the VM
  361. */
  362. public void setJvm(String s) {
  363. cmdl.setVm(s);
  364. }
  365. /**
  366. * Adds a system property.
  367. *
  368. * @param sysp system property
  369. */
  370. public void addSysproperty(Environment.Variable sysp) {
  371. cmdl.addSysproperty(sysp);
  372. }
  373. /**
  374. * Adds a set of properties as system properties.
  375. *
  376. * @param sysp set of properties to add
  377. *
  378. * @since Ant 1.6
  379. */
  380. public void addSyspropertyset(PropertySet sysp) {
  381. cmdl.addSyspropertyset(sysp);
  382. }
  383. /**
  384. * If true, then fail if the command exits with a
  385. * returncode other than 0
  386. *
  387. * @param fail if true fail the build when the command exits with a non
  388. * zero returncode
  389. */
  390. public void setFailonerror(boolean fail) {
  391. failOnError = fail;
  392. incompatibleWithSpawn = true;
  393. }
  394. /**
  395. * The working directory of the process
  396. *
  397. * @param d working directory
  398. *
  399. */
  400. public void setDir(File d) {
  401. this.dir = d;
  402. }
  403. /**
  404. * File the output of the process is redirected to.
  405. *
  406. * @param out name of the output file
  407. */
  408. public void setOutput(File out) {
  409. redirector.setOutput(out);
  410. incompatibleWithSpawn = true;
  411. }
  412. /**
  413. * Set the input to use for the task
  414. *
  415. * @param input name of the input file
  416. */
  417. public void setInput(File input) {
  418. redirector.setInput(input);
  419. incompatibleWithSpawn = true;
  420. }
  421. /**
  422. * Set the string to use as input
  423. *
  424. * @param inputString the string which is used as the input source
  425. */
  426. public void setInputString(String inputString) {
  427. redirector.setInputString(inputString);
  428. incompatibleWithSpawn = true;
  429. }
  430. /**
  431. * Controls whether error output of exec is logged. This is only useful
  432. * when output is being redirected and error output is desired in the
  433. * Ant log
  434. *
  435. * @param logError get in the ant log the messages coming from stderr
  436. * in the case that fork = true
  437. */
  438. public void setLogError(boolean logError) {
  439. redirector.setLogError(logError);
  440. incompatibleWithSpawn = true;
  441. }
  442. /**
  443. * File the error stream of the process is redirected to.
  444. *
  445. * @param error file getting the error stream
  446. *
  447. * @since ant 1.6
  448. */
  449. public void setError(File error) {
  450. redirector.setError(error);
  451. incompatibleWithSpawn = true;
  452. }
  453. /**
  454. * Property name whose value should be set to the output of
  455. * the process.
  456. *
  457. * @param outputProp property name
  458. *
  459. */
  460. public void setOutputproperty(String outputProp) {
  461. redirector.setOutputProperty(outputProp);
  462. incompatibleWithSpawn = true;
  463. }
  464. /**
  465. * Property name whose value should be set to the error of
  466. * the process.
  467. *
  468. * @param errorProperty property name
  469. *
  470. * @since ant 1.6
  471. */
  472. public void setErrorProperty(String errorProperty) {
  473. redirector.setErrorProperty(errorProperty);
  474. incompatibleWithSpawn = true;
  475. }
  476. /**
  477. * Corresponds to -mx or -Xmx depending on VM version.
  478. *
  479. * @param max max memory parameter
  480. */
  481. public void setMaxmemory(String max) {
  482. cmdl.setMaxmemory(max);
  483. }
  484. /**
  485. * Sets the JVM version.
  486. * @param value JVM version
  487. */
  488. public void setJVMVersion(String value) {
  489. cmdl.setVmversion(value);
  490. }
  491. /**
  492. * Adds an environment variable.
  493. *
  494. * <p>Will be ignored if we are not forking a new VM.
  495. *
  496. * @param var new environment variable
  497. *
  498. * @since Ant 1.5
  499. */
  500. public void addEnv(Environment.Variable var) {
  501. env.addVariable(var);
  502. }
  503. /**
  504. * If true, use a completely new environment.
  505. *
  506. * <p>Will be ignored if we are not forking a new VM.
  507. *
  508. * @param newenv if true, use a completely new environment.
  509. *
  510. * @since Ant 1.5
  511. */
  512. public void setNewenvironment(boolean newenv) {
  513. newEnvironment = newenv;
  514. }
  515. /**
  516. * If true, append output to existing file.
  517. *
  518. * @param append if true, append output to existing file
  519. *
  520. * @since Ant 1.5
  521. */
  522. public void setAppend(boolean append) {
  523. this.append = append;
  524. incompatibleWithSpawn = true;
  525. }
  526. /**
  527. * Timeout in milliseconds after which the process will be killed.
  528. *
  529. * @param value time out in milliseconds
  530. *
  531. * @since Ant 1.5
  532. */
  533. public void setTimeout(Long value) {
  534. timeout = value;
  535. incompatibleWithSpawn = true;
  536. }
  537. /**
  538. * assertions to enable in this program (if fork=true)
  539. * @since Ant 1.6
  540. * @param asserts assertion set
  541. */
  542. public void setAssertions(Assertions asserts) {
  543. cmdl.setAssertions(asserts);
  544. }
  545. /**
  546. * Pass output sent to System.out to specified output file.
  547. *
  548. * @param output a string of output on its way to the handlers
  549. *
  550. * @since Ant 1.5
  551. */
  552. protected void handleOutput(String output) {
  553. if (redirector.getOutputStream() != null) {
  554. redirector.handleOutput(output);
  555. } else {
  556. super.handleOutput(output);
  557. }
  558. }
  559. /**
  560. * Handle an input request by this task
  561. *
  562. * @param buffer the buffer into which data is to be read.
  563. * @param offset the offset into the buffer at which data is stored.
  564. * @param length the amount of data to read
  565. *
  566. * @return the number of bytes read
  567. *
  568. * @exception IOException if the data cannot be read
  569. * @since Ant 1.6
  570. */
  571. public int handleInput(byte[] buffer, int offset, int length)
  572. throws IOException {
  573. if (redirector.getInputStream() != null) {
  574. return redirector.handleInput(buffer, offset, length);
  575. } else {
  576. return super.handleInput(buffer, offset, length);
  577. }
  578. }
  579. /**
  580. * Pass output sent to System.out to specified output file.
  581. *
  582. * @param output string of output on its way to its handlers
  583. *
  584. * @since Ant 1.5.2
  585. */
  586. protected void handleFlush(String output) {
  587. if (redirector.getOutputStream() != null) {
  588. redirector.handleFlush(output);
  589. } else {
  590. super.handleFlush(output);
  591. }
  592. }
  593. /**
  594. * Pass output sent to System.err to specified output file.
  595. *
  596. * @param output string of stderr
  597. *
  598. * @since Ant 1.5
  599. */
  600. protected void handleErrorOutput(String output) {
  601. if (redirector.getErrorStream() != null) {
  602. redirector.handleErrorOutput(output);
  603. } else {
  604. super.handleErrorOutput(output);
  605. }
  606. }
  607. /**
  608. * Pass output sent to System.err to specified output file.
  609. *
  610. * @param output string of stderr
  611. *
  612. * @since Ant 1.5.2
  613. */
  614. protected void handleErrorFlush(String output) {
  615. if (redirector.getErrorStream() != null) {
  616. redirector.handleErrorFlush(output);
  617. } else {
  618. super.handleErrorOutput(output);
  619. }
  620. }
  621. /**
  622. * Executes the given classname with the given arguments as it
  623. * was a command line application.
  624. */
  625. private void run(CommandlineJava command) throws BuildException {
  626. try {
  627. ExecuteJava exe = new ExecuteJava();
  628. exe.setJavaCommand(command.getJavaCommand());
  629. exe.setClasspath(command.getClasspath());
  630. exe.setSystemProperties(command.getSystemProperties());
  631. exe.setPermissions(perm);
  632. exe.setTimeout(timeout);
  633. redirector.createStreams();
  634. exe.execute(getProject());
  635. redirector.complete();
  636. } catch (IOException e) {
  637. throw new BuildException(e);
  638. }
  639. }
  640. /**
  641. * Executes the given classname with the given arguments in a separate VM.
  642. */
  643. private int fork(String[] command) throws BuildException {
  644. Execute exe
  645. = new Execute(redirector.createHandler(), createWatchdog());
  646. exe.setAntRun(getProject());
  647. if (dir == null) {
  648. dir = getProject().getBaseDir();
  649. } else if (!dir.exists() || !dir.isDirectory()) {
  650. throw new BuildException(dir.getAbsolutePath()
  651. + " is not a valid directory",
  652. getLocation());
  653. }
  654. exe.setWorkingDirectory(dir);
  655. String[] environment = env.getVariables();
  656. if (environment != null) {
  657. for (int i = 0; i < environment.length; i++) {
  658. log("Setting environment variable: " + environment[i],
  659. Project.MSG_VERBOSE);
  660. }
  661. }
  662. exe.setNewenvironment(newEnvironment);
  663. exe.setEnvironment(environment);
  664. exe.setCommandline(command);
  665. try {
  666. int rc = exe.execute();
  667. if (exe.killedProcess()) {
  668. log("Timeout: killed the sub-process", Project.MSG_WARN);
  669. }
  670. redirector.complete();
  671. return rc;
  672. } catch (IOException e) {
  673. throw new BuildException(e, getLocation());
  674. }
  675. }
  676. /**
  677. * Executes the given classname with the given arguments in a separate VM.
  678. */
  679. private void spawn(String[] command) throws BuildException {
  680. Execute exe
  681. = new Execute();
  682. exe.setAntRun(getProject());
  683. if (dir == null) {
  684. dir = getProject().getBaseDir();
  685. } else if (!dir.exists() || !dir.isDirectory()) {
  686. throw new BuildException(dir.getAbsolutePath()
  687. + " is not a valid directory",
  688. getLocation());
  689. }
  690. exe.setWorkingDirectory(dir);
  691. String[] environment = env.getVariables();
  692. if (environment != null) {
  693. for (int i = 0; i < environment.length; i++) {
  694. log("Setting environment variable: " + environment[i],
  695. Project.MSG_VERBOSE);
  696. }
  697. }
  698. exe.setNewenvironment(newEnvironment);
  699. exe.setEnvironment(environment);
  700. exe.setCommandline(command);
  701. try {
  702. exe.spawn();
  703. } catch (IOException e) {
  704. throw new BuildException(e, getLocation());
  705. }
  706. }
  707. /**
  708. * Executes the given classname with the given arguments as it
  709. * was a command line application.
  710. *
  711. * @param classname the name of the class to run
  712. * @param args arguments for the class
  713. * @throws BuildException in case of IO Exception in the execution
  714. */
  715. protected void run(String classname, Vector args) throws BuildException {
  716. CommandlineJava cmdj = new CommandlineJava();
  717. cmdj.setClassname(classname);
  718. for (int i = 0; i < args.size(); i++) {
  719. cmdj.createArgument().setValue((String) args.elementAt(i));
  720. }
  721. run(cmdj);
  722. }
  723. /**
  724. * Clear out the arguments to this java task.
  725. */
  726. public void clearArgs() {
  727. cmdl.clearJavaArgs();
  728. }
  729. /**
  730. * Create the Watchdog to kill a runaway process.
  731. *
  732. * @return new watchdog
  733. *
  734. * @throws BuildException under unknown circumnstances
  735. *
  736. * @since Ant 1.5
  737. */
  738. protected ExecuteWatchdog createWatchdog() throws BuildException {
  739. if (timeout == null) {
  740. return null;
  741. }
  742. return new ExecuteWatchdog(timeout.longValue());
  743. }
  744. }