Browse Source

Make <java> handle exceptions gracefully if failonerror="true" in

non-forked mode as well.

PR: 6353


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272297 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 23 years ago
parent
commit
d1549366e5
5 changed files with 62 additions and 22 deletions
  1. +7
    -0
      src/etc/testcases/taskdefs/java.xml
  2. +7
    -3
      src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java
  3. +37
    -14
      src/main/org/apache/tools/ant/taskdefs/Java.java
  4. +1
    -1
      src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java
  5. +10
    -4
      src/testcases/org/apache/tools/ant/taskdefs/JavaTest.java

+ 7
- 0
src/etc/testcases/taskdefs/java.xml View File

@@ -67,6 +67,13 @@
</java> </java>
</target> </target>


<target name="testExceptingFork">
<java classname="${app2}"
classpath="${tests-classpath.value}"
fork="true">
</java>
</target>

<target name="testExceptingFoe"> <target name="testExceptingFoe">
<java classname="${app2}" <java classname="${app2}"
classpath="${tests-classpath.value}" classpath="${tests-classpath.value}"


+ 7
- 3
src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java View File

@@ -134,6 +134,10 @@ public class ExecuteJava implements Runnable, TimeoutObserver {
AntClassLoader.initializeClass(target); AntClassLoader.initializeClass(target);
} }
main = target.getMethod("main", param); main = target.getMethod("main", param);
if (main == null) {
throw new BuildException("Could not find main() method in "
+ classname);
}


if (timeout == null) { if (timeout == null) {
run(); run();
@@ -169,10 +173,10 @@ public class ExecuteJava implements Runnable, TimeoutObserver {
throw caught; throw caught;
} }


} catch (NullPointerException e) {
throw new BuildException("Could not find main() method in " + classname);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new BuildException("Could not find " + classname + ". Make sure you have it in your classpath");
throw new BuildException("Could not find " + classname + "."
+ " Make sure you have it in your"
+ " classpath");
} catch (SecurityException e) { } catch (SecurityException e) {
throw e; throw e;
} catch (Throwable e) { } catch (Throwable e) {


+ 37
- 14
src/main/org/apache/tools/ant/taskdefs/Java.java View File

@@ -98,7 +98,8 @@ public class Java extends Task {
*/ */
public void execute() throws BuildException { public void execute() throws BuildException {
int err = -1; int err = -1;
if ((err = executeJava()) != 0) {

if ((err = executeJava()) != 0) {
if (failOnError) { if (failOnError) {
throw new BuildException("Java returned: "+err, location); throw new BuildException("Java returned: "+err, location);
} else { } else {
@@ -110,43 +111,65 @@ public class Java extends Task {
/** /**
* Do the execution and return a return code. * Do the execution and return a return code.
* *
* @return the return code from the execute java class if it was executed in
* a separate VM (fork = "yes").
* @return the return code from the execute java class if it was
* executed in a separate VM (fork = "yes").
*/ */
public int executeJava() throws BuildException { public int executeJava() throws BuildException {
String classname = cmdl.getClassname(); String classname = cmdl.getClassname();
if (classname == null && cmdl.getJar() == null) { if (classname == null && cmdl.getJar() == null) {
throw new BuildException("Classname must not be null."); throw new BuildException("Classname must not be null.");
} }

if (!fork && cmdl.getJar() != null){ if (!fork && cmdl.getJar() != null){
throw new BuildException("Cannot execute a jar in non-forked mode. Please set fork='true'. ");
throw new BuildException("Cannot execute a jar in non-forked mode."
+ " Please set fork='true'. ");
} }


if (fork) { if (fork) {
log("Forking " + cmdl.toString(), Project.MSG_VERBOSE); log("Forking " + cmdl.toString(), Project.MSG_VERBOSE);
return run(cmdl.getCommandline());
} else { } else {
if (cmdl.getVmCommand().size() > 1) { if (cmdl.getVmCommand().size() > 1) {
log("JVM args ignored when same JVM is used.", Project.MSG_WARN);
log("JVM args ignored when same JVM is used.",
Project.MSG_WARN);
} }
if (dir != null) { if (dir != null) {
log("Working directory ignored when same JVM is used.", Project.MSG_WARN);
log("Working directory ignored when same JVM is used.",
Project.MSG_WARN);
} }


if (newEnvironment || null != env.getVariables()) { if (newEnvironment || null != env.getVariables()) {
log("Changes to environment variables are ignored when same JVM is used.",
Project.MSG_WARN);
log("Changes to environment variables are ignored when same "
+ "JVM is used.", Project.MSG_WARN);
} }


log("Running in same VM " + cmdl.getJavaCommand().toString(), log("Running in same VM " + cmdl.getJavaCommand().toString(),
Project.MSG_VERBOSE); Project.MSG_VERBOSE);
try {
run(cmdl);
}
try {
if (fork) {
return run(cmdl.getCommandline());
} else {
try {
run(cmdl);
return 0;
} catch (ExitException ex) {
return ex.getStatus();
}
}
} catch (BuildException e) {
if (failOnError) {
throw e;
} else {
log(e.getMessage(), Project.MSG_ERR);
return 0; return 0;
} }
catch (ExitException ex) {
return ex.getStatus();
} catch (Throwable t) {
if (failOnError) {
throw new BuildException(t);
} else {
log(t.getMessage(), Project.MSG_ERR);
return 0;
} }
} }
} }


+ 1
- 1
src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java View File

@@ -1,7 +1,7 @@
/* /*
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2000-2001 The Apache Software Foundation. All rights
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without


+ 10
- 4
src/testcases/org/apache/tools/ant/taskdefs/JavaTest.java View File

@@ -144,13 +144,19 @@ public class JavaTest extends BuildFileTest {
} }


public void testExcepting() { public void testExcepting() {
executeTarget("testExcepting");
expectLogContaining("testExcepting",
"Exception raised inside called program");
}
public void testExceptingFork() {
expectLogContaining("testExceptingFork",
"Java Result:");
} }
public void testExceptingFoe() { public void testExceptingFoe() {
//if(runFatalTests) {
executeTarget("testExceptingFoe");
//}
expectBuildExceptionContaining("testExceptingFoe",
"passes exception through",
"Exception raised inside called program");
} }
public void testExceptingFoeFork() { public void testExceptingFoeFork() {


Loading…
Cancel
Save