📄 commandlinejava.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.types;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.util.JavaEnvUtils;import java.util.Enumeration;import java.util.LinkedList;import java.util.List;import java.util.ListIterator;import java.util.Properties;import java.util.Vector;/** * A representation of a Java command line that is * a composite of 2 <tt>Commandline</tt>s. One is used for the * vm/options and one for the classname/arguments. It provides * specific methods for a Java command line. * */public class CommandlineJava implements Cloneable { /** * commands to the JVM */ private Commandline vmCommand = new Commandline(); /** * actual java commands */ private Commandline javaCommand = new Commandline(); /** * properties to add using -D */ private SysProperties sysProperties = new SysProperties(); private Path classpath = null; private Path bootclasspath = null; private String vmVersion; private String maxMemory = null; /** * any assertions to make? Currently only supported in forked JVMs */ private Assertions assertions = null; /** * 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 executeJar = false; /** * Whether system properties and bootclasspath shall be cloned. * @since Ant 1.7 */ private boolean cloneVm = false; /** * Specialized Environment class for System properties. */ public static class SysProperties extends Environment implements Cloneable { // CheckStyle:VisibilityModifier OFF - bc /** the system properties. */ Properties sys = null; // CheckStyle:VisibilityModifier ON private Vector propertySets = new Vector(); /** * Get the properties as an array; this is an override of the * superclass, as it evaluates all the properties. * @return the array of definitions; may be null. * @throws BuildException on error. */ public String[] getVariables() throws BuildException { List definitions = new LinkedList(); ListIterator list = definitions.listIterator(); addDefinitionsToList(list); if (definitions.size() == 0) { return null; } else { return (String[]) definitions.toArray(new String[definitions.size()]); } } /** * Add all definitions (including property sets) to a list. * @param listIt list iterator supporting add method. */ public void addDefinitionsToList(ListIterator listIt) { String[] props = super.getVariables(); if (props != null) { for (int i = 0; i < props.length; i++) { listIt.add("-D" + props[i]); } } Properties propertySetProperties = mergePropertySets(); for (Enumeration e = propertySetProperties.keys(); e.hasMoreElements();) { String key = (String) e.nextElement(); String value = propertySetProperties.getProperty(key); listIt.add("-D" + key + "=" + value); } } /** * Get the size of the sysproperties instance. This merges all * property sets, so is not an O(1) operation. * @return the size of the sysproperties instance. */ public int size() { Properties p = mergePropertySets(); return variables.size() + p.size(); } /** * Cache the system properties and set the system properties to the * new values. * @throws BuildException if Security prevented this operation. */ public void setSystem() throws BuildException { try { sys = System.getProperties(); Properties p = new Properties(); for (Enumeration e = sys.propertyNames(); e.hasMoreElements();) { String name = (String) e.nextElement(); p.put(name, sys.getProperty(name)); } p.putAll(mergePropertySets()); for (Enumeration e = variables.elements(); e.hasMoreElements();) { Environment.Variable v = (Environment.Variable) e.nextElement(); v.validate(); p.put(v.getKey(), v.getValue()); } System.setProperties(p); } catch (SecurityException e) { throw new BuildException("Cannot modify system properties", e); } } /** * Restore the system properties to the cached value. * @throws BuildException if Security prevented this operation, or * there were no system properties to restore. */ public void restoreSystem() throws BuildException { if (sys == null) { throw new BuildException("Unbalanced nesting of SysProperties"); } try { System.setProperties(sys); sys = null; } catch (SecurityException e) { throw new BuildException("Cannot modify system properties", e); } } /** * Create a deep clone. * @return a cloned instance of SysProperties. * @exception CloneNotSupportedException for signature. */ public Object clone() throws CloneNotSupportedException { try { SysProperties c = (SysProperties) super.clone(); c.variables = (Vector) variables.clone(); c.propertySets = (Vector) propertySets.clone(); return c; } catch (CloneNotSupportedException e) { return null; } } /** * Add a propertyset to the total set. * @param ps the new property set. */ public void addSyspropertyset(PropertySet ps) { propertySets.addElement(ps); } /** * Add a propertyset to the total set. * @param ps the new property set. * @since Ant 1.6.3 */ public void addSysproperties(SysProperties ps) { variables.addAll(ps.variables); propertySets.addAll(ps.propertySets); } /** * Merge all property sets into a single Properties object. * @return the merged object. */ private Properties mergePropertySets() { Properties p = new Properties(); for (Enumeration e = propertySets.elements(); e.hasMoreElements();) { PropertySet ps = (PropertySet) e.nextElement(); p.putAll(ps.getProperties()); } return p; } } /** * Constructor uses the VM we are running on now. */ public CommandlineJava() { setVm(JavaEnvUtils.getJreExecutable("java")); setVmversion(JavaEnvUtils.getJavaVersion()); } /** * Create a new argument to the java program. * @return an argument to be configured. */ public Commandline.Argument createArgument() { return javaCommand.createArgument(); } /** * Create a new JVM argument. * @return an argument to be configured. */ public Commandline.Argument createVmArgument() { return vmCommand.createArgument(); } /** * Add a system property. * @param sysp a property to be set in the JVM. */ public void addSysproperty(Environment.Variable sysp) { sysProperties.addVariable(sysp); } /** * Add a set of system properties. * @param sysp a set of properties. */ public void addSyspropertyset(PropertySet sysp) { sysProperties.addSyspropertyset(sysp); } /** * Add a set of system properties. * @param sysp a set of properties. * @since Ant 1.6.3 */ public void addSysproperties(SysProperties sysp) { sysProperties.addSysproperties(sysp); } /** * Set the executable used to start the new JVM. * @param vm the executable to use. */ public void setVm(String vm) { vmCommand.setExecutable(vm); } /** * Set the JVM version required. * @param value the version required. */ public void setVmversion(String value) { vmVersion = value; } /** * 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 */ public void setCloneVm(boolean cloneVm) { this.cloneVm = cloneVm; } /** * Get the current assertions. * @return assertions or null. */ public Assertions getAssertions() { return assertions; } /** * Add an assertion set to the command. * @param assertions assertions to make. */ public void setAssertions(Assertions assertions) { this.assertions = assertions; } /** * Set a jar file to execute via the -jar option. * @param jarpathname the pathname of the jar to execute. */ public void setJar(String jarpathname) { javaCommand.setExecutable(jarpathname); executeJar = true; } /** * Get the name of the jar to be run. * @return the pathname of the jar file to run via -jar option * or <tt>null</tt> if there is no jar to run. * @see #getClassname() */ public String getJar() { if (executeJar) { return javaCommand.getExecutable(); } return null; } /** * Set the classname to execute. * @param classname the fully qualified classname. */ public void setClassname(String classname) { javaCommand.setExecutable(classname); executeJar = false; } /** * Get the name of the class to be run. * @return the name of the class to run or <tt>null</tt> if there is no class.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -