FileDocCategorySizeDatePackage
CommandlineJava.javaAPI DocApache Ant 1.7022520Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.types

CommandlineJava

public class CommandlineJava extends Object implements Cloneable
A representation of a Java command line that is a composite of 2 Commandlines. One is used for the vm/options and one for the classname/arguments. It provides specific methods for a Java command line.

Fields Summary
private Commandline
vmCommand
commands to the JVM
private Commandline
javaCommand
actual java commands
private SysProperties
sysProperties
properties to add using -D
private Path
classpath
private Path
bootclasspath
private String
vmVersion
private String
maxMemory
private Assertions
assertions
any assertions to make? Currently only supported in forked JVMs
private boolean
executeJar
Indicate whether it will execute a jar file or not, in this case the first vm option must be a -jar and the 'executable' is a jar file.
private boolean
cloneVm
Whether system properties and bootclasspath shall be cloned.
Constructors Summary
public CommandlineJava()
Constructor uses the VM we are running on now.

        setVm(JavaEnvUtils.getJreExecutable("java"));
        setVmversion(JavaEnvUtils.getJavaVersion());
    
Methods Summary
private voidaddCommandsToList(java.util.ListIterator listIterator)
Add all the commands to a list identified by the iterator passed in.

param
listIterator an iterator that supports the add method.
since
Ant 1.6

        //create the command to run Java, including user specified options
        getActualVMCommand().addCommandToList(listIterator);
        // properties are part of the vm options...
        sysProperties.addDefinitionsToList(listIterator);

        if (isCloneVm()) {
            SysProperties clonedSysProperties = new SysProperties();
            PropertySet ps = new PropertySet();
            PropertySet.BuiltinPropertySetName sys =
                new PropertySet.BuiltinPropertySetName();
            sys.setValue("system");
            ps.appendBuiltin(sys);
            clonedSysProperties.addSyspropertyset(ps);
            clonedSysProperties.addDefinitionsToList(listIterator);
        }
        //boot classpath
        Path bcp = calculateBootclasspath(true);
        if (bcp.size() > 0) {
            listIterator.add("-Xbootclasspath:" + bcp.toString());
        }
        //main classpath
        if (haveClasspath()) {
            listIterator.add("-classpath");
            listIterator.add(
                    classpath.concatSystemClasspath("ignore").toString());
        }
        //now any assertions are added
        if (getAssertions() != null) {
            getAssertions().applyAssertions(listIterator);
        }
        // JDK usage command line says that -jar must be the first option, as there is
        // a bug in JDK < 1.4 that forces the jvm type to be specified as the first
        // option, it is appended here as specified in the docs even though there is
        // in fact no order.
        if (executeJar) {
            listIterator.add("-jar");
        }
        // this is the classname to run as well as its arguments.
        // in case of 'executeJar', the executable is a jar file.
        javaCommand.addCommandToList(listIterator);
    
public voidaddSysproperties(org.apache.tools.ant.types.CommandlineJava$SysProperties sysp)
Add a set of system properties.

param
sysp a set of properties.
since
Ant 1.6.3

        sysProperties.addSysproperties(sysp);
    
public voidaddSysproperty(Environment.Variable sysp)
Add a system property.

param
sysp a property to be set in the JVM.

        sysProperties.addVariable(sysp);
    
public voidaddSyspropertyset(PropertySet sysp)
Add a set of system properties.

param
sysp a set of properties.

        sysProperties.addSyspropertyset(sysp);
    
private PathcalculateBootclasspath(boolean log)
Calculate the bootclasspath based on the bootclasspath specified, the build.sysclasspath and ant.build.clonevm magic properties as well as the cloneVm attribute.

param
log whether to write messages to the log.
since
Ant 1.7

        if (vmVersion.startsWith("1.1")) {
            if (bootclasspath != null && log) {
                bootclasspath.log("Ignoring bootclasspath as "
                                  + "the target VM doesn't support it.");
            }
        } else {
            if (bootclasspath != null) {
                return bootclasspath.concatSystemBootClasspath(isCloneVm()
                                                               ? "last"
                                                               : "ignore");
            } else if (isCloneVm()) {
                return Path.systemBootClasspath;
            }
        }
        return new Path(null);
    
public voidclearJavaArgs()
Clear out the java arguments.

        javaCommand.clearArgs();
    
public java.lang.Objectclone()
Deep clone the object.

return
a CommandlineJava object.
throws
BuildException if anything went wrong.
throws
CloneNotSupportedException never.

        try {
            CommandlineJava c = (CommandlineJava) super.clone();
            c.vmCommand = (Commandline) vmCommand.clone();
            c.javaCommand = (Commandline) javaCommand.clone();
            c.sysProperties = (SysProperties) sysProperties.clone();
            if (classpath != null) {
                c.classpath = (Path) classpath.clone();
            }
            if (bootclasspath != null) {
                c.bootclasspath = (Path) bootclasspath.clone();
            }
            if (assertions != null) {
                c.assertions = (Assertions) assertions.clone();
            }
            return c;
        } catch (CloneNotSupportedException e) {
            throw new BuildException(e);
        }
    
public Commandline.ArgumentcreateArgument()
Create a new argument to the java program.

return
an argument to be configured.

        return javaCommand.createArgument();
    
public PathcreateBootclasspath(org.apache.tools.ant.Project p)
Create a boot classpath.

param
p the project to use to create the path.
return
a path to be configured.
since
Ant 1.6

        if (bootclasspath == null) {
            bootclasspath = new Path(p);
        }
        return bootclasspath;
    
public PathcreateClasspath(org.apache.tools.ant.Project p)
Create a classpath.

param
p the project to use to create the path.
return
a path to be configured.

        if (classpath == null) {
            classpath = new Path(p);
        }
        return classpath;
    
public Commandline.ArgumentcreateVmArgument()
Create a new JVM argument.

return
an argument to be configured.

        return vmCommand.createArgument();
    
public java.lang.StringdescribeCommand()
Return a String that describes the command and arguments suitable for verbose output before a call to Runtime.exec(String[]).

return
the description string.
since
Ant 1.5

        return Commandline.describeCommand(getCommandline());
    
public java.lang.StringdescribeJavaCommand()
Return a String that describes the java command and arguments for in-VM executions.

The class name is the executable in this context.

return
the description string.
since
Ant 1.5

        return Commandline.describeCommand(getJavaCommand());
    
protected CommandlinegetActualVMCommand()
Get the VM command parameters, including memory settings.

return
the VM command parameters.

        Commandline actualVMCommand = (Commandline) vmCommand.clone();
        if (maxMemory != null) {
            if (vmVersion.startsWith("1.1")) {
                actualVMCommand.createArgument().setValue("-mx" + maxMemory);
            } else {
                actualVMCommand.createArgument().setValue("-Xmx" + maxMemory);
            }
        }
        return actualVMCommand;
    
public AssertionsgetAssertions()
Get the current assertions.

return
assertions or null.

        return assertions;
    
public PathgetBootclasspath()
Get the boot classpath.

return
boot classpath or null.

        return bootclasspath;
    
public java.lang.StringgetClassname()
Get the name of the class to be run.

return
the name of the class to run or null if there is no class.
see
#getJar()

        if (!executeJar) {
            return javaCommand.getExecutable();
        }
        return null;
    
public PathgetClasspath()
Get the classpath for the command.

return
the classpath or null.

        return classpath;
    
public java.lang.String[]getCommandline()
Get the command line to run a Java vm.

return
the list of all arguments necessary to run the vm.

        //create the list
        List commands = new LinkedList();
        final ListIterator listIterator = commands.listIterator();
        //fill it
        addCommandsToList(listIterator);
        //convert to an array
        return (String[]) commands.toArray(new String[commands.size()]);
    
public java.lang.StringgetJar()
Get the name of the jar to be run.

return
the pathname of the jar file to run via -jar option or null if there is no jar to run.
see
#getClassname()

        if (executeJar) {
            return javaCommand.getExecutable();
        }
        return null;
    
public CommandlinegetJavaCommand()
Get the Java command to be used.

return
the java command--not a clone.

        return javaCommand;
    
public org.apache.tools.ant.types.CommandlineJava$SysPropertiesgetSystemProperties()
Get the system properties object.

return
The system properties object.

        return sysProperties;
    
public CommandlinegetVmCommand()
Get the VM command, including memory.

return
A deep clone of the instance's VM command, with memory settings added.

        return getActualVMCommand();
    
public java.lang.StringgetVmversion()
Get the vm version.

return
the vm version.

        return vmVersion;
    
protected booleanhaveBootclasspath(boolean log)
Determine whether the bootclasspath has been specified, and whether it shall really be used (build.sysclasspath could be set or the VM may not support it).

param
log whether to log a warning if a bootclasspath has been specified but will be ignored.
return
true if the bootclasspath is to be used.
since
Ant 1.6

        return calculateBootclasspath(log).size() > 0;
    
protected booleanhaveClasspath()
Determine whether the classpath has been specified, and whether it shall really be used or be nulled by build.sysclasspath.

return
true if the classpath is to be used.
since
Ant 1.6

        Path fullClasspath = classpath != null
            ? classpath.concatSystemClasspath("ignore") : null;
        return fullClasspath != null
            && fullClasspath.toString().trim().length() > 0;
    
private booleanisCloneVm()
Find out whether either of the cloneVm attribute or the magic property ant.build.clonevm has been set.

return
boolean.
since
1.7

        return cloneVm
            || "true".equals(System.getProperty("ant.build.clonevm"));
    
public voidrestoreSystemProperties()
Restore the cached system properties.

throws
BuildException if Security prevented this operation, or there was no system properties to restore

        sysProperties.restoreSystem();
    
public voidsetAssertions(Assertions assertions)
Add an assertion set to the command.

param
assertions assertions to make.

        this.assertions = assertions;
    
public voidsetClassname(java.lang.String classname)
Set the classname to execute.

param
classname the fully qualified classname.

        javaCommand.setExecutable(classname);
        executeJar = false;
    
public voidsetCloneVm(boolean cloneVm)
Set whether system properties will be copied to the cloned VM--as well as the bootclasspath unless you have explicitly specified a bootclasspath.

param
cloneVm if true copy the system properties.
since
Ant 1.7

        this.cloneVm = cloneVm;
    
public voidsetJar(java.lang.String jarpathname)
Set a jar file to execute via the -jar option.

param
jarpathname the pathname of the jar to execute.

        javaCommand.setExecutable(jarpathname);
        executeJar = true;
    
public voidsetMaxmemory(java.lang.String max)
Specify max memory of the JVM. -mx or -Xmx depending on VM version.

param
max the string to pass to the jvm to specifiy the max memory.

        this.maxMemory = max;
    
public voidsetSystemProperties()
Cache current system properties and set them to those in this Java command.

throws
BuildException if Security prevented this operation.

        sysProperties.setSystem();
    
public voidsetVm(java.lang.String vm)
Set the executable used to start the new JVM.

param
vm the executable to use.

        vmCommand.setExecutable(vm);
    
public voidsetVmversion(java.lang.String value)
Set the JVM version required.

param
value the version required.

        vmVersion = value;
    
public intsize()
Get the size of the java command line. This is a fairly intensive operation, as it has to evaluate the size of many components.

return
the total number of arguments in the java command line.
see
#getCommandline()
deprecated
since 1.7. Please dont use this, it effectively creates the entire command.

        int size = getActualVMCommand().size() + javaCommand.size()
            + sysProperties.size();
        // cloned system properties
        if (isCloneVm()) {
            size += System.getProperties().size();
        }
        // classpath is "-classpath <classpath>" -> 2 args
        if (haveClasspath()) {
            size += 2;
        }
        // bootclasspath is "-Xbootclasspath:<classpath>" -> 1 arg
        if (calculateBootclasspath(true).size() > 0) {
            size++;
        }
        // jar execution requires an additional -jar option
        if (executeJar) {
            size++;
        }
        //assertions take up space too
        if (getAssertions() != null) {
            size += getAssertions().size();
        }
        return size;
    
public java.lang.StringtoString()
Get a string description.

return
the command line as a string.

        return Commandline.toString(getCommandline());