diff --git a/src/main/org/apache/tools/ant/types/CommandlineJava.java b/src/main/org/apache/tools/ant/types/CommandlineJava.java index a00f2a31c..89d0464f0 100644 --- a/src/main/org/apache/tools/ant/types/CommandlineJava.java +++ b/src/main/org/apache/tools/ant/types/CommandlineJava.java @@ -58,9 +58,14 @@ import java.util.*; import org.apache.tools.ant.Project; import org.apache.tools.ant.BuildException; -/* +/** + * A representation of a Java command line that is nothing more + * than a composite of 2 Commandline. 1 for the vm/options and + * 1 for the classname/arguments. It provides specific methods for + * a java command line. * * @author thomas.haas@softwired-inc.com + * @author 0) { - size += 2; - } + String[] result = new String[size()]; + int pos = 0; + String[] vmArgs = vmCommand.getCommandline(); + // first argument is the java.exe path... + result[pos++] = vmArgs[0]; - String[] result = new String[size]; - System.arraycopy(actualVMCommand.getCommandline(), 0, - result, 0, actualVMCommand.size()); - - int pos = actualVMCommand.size(); + // -jar must be the first option in the command line. + if (executeJar){ + result[pos++] = "-jar"; + } + // next follows the vm options + System.arraycopy(vmArgs, 1, result, pos, vmArgs.length - 1); + pos += vmArgs.length - 1; + // properties are part of the vm options... if (sysProperties.size() > 0) { System.arraycopy(sysProperties.getVariables(), 0, result, pos, sysProperties.size()); pos += sysProperties.size(); } + // classpath is a vm option too.. + Path fullClasspath = classpath != null ? classpath.concatSystemClasspath("ignore") : null; if (fullClasspath != null && fullClasspath.toString().trim().length() > 0) { result[pos++] = "-classpath"; result[pos++] = fullClasspath.toString(); } + // this is the classname to run as well as its arguments. + // in case of 'executeJar', the executable is a jar file. System.arraycopy(javaCommand.getCommandline(), 0, result, pos, javaCommand.size()); return result; @@ -223,12 +280,22 @@ public class CommandlineJava implements Cloneable { } return actualVMCommand; } - + + /** + * The size of the java command line. + * @return the total number of arguments in the java command line. + * @see #getCommandline() + */ public int size() { - int size = getActualVMCommand().size() + javaCommand.size(); + int size = vmCommand.size() + javaCommand.size() + sysProperties.size(); + // classpath is "-classpath " -> 2 args if (classpath != null && classpath.size() > 0) { size += 2; } + // jar execution requires an additional -jar option + if (executeJar){ + size++ ; + } return size; } @@ -266,6 +333,7 @@ public class CommandlineJava implements Cloneable { c.classpath = (Path) classpath.clone(); } c.vmVersion = vmVersion; + c.executeJar = executeJar; return c; }