📄 java.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.taskdefs;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.io.StringWriter;import java.util.Vector;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.ExitException;import org.apache.tools.ant.Project;import org.apache.tools.ant.Task;import org.apache.tools.ant.ExitStatusException;import org.apache.tools.ant.types.Commandline;import org.apache.tools.ant.types.CommandlineJava;import org.apache.tools.ant.types.Environment;import org.apache.tools.ant.types.Path;import org.apache.tools.ant.types.PropertySet;import org.apache.tools.ant.types.Reference;import org.apache.tools.ant.types.Assertions;import org.apache.tools.ant.types.Permissions;import org.apache.tools.ant.types.RedirectorElement;import org.apache.tools.ant.taskdefs.condition.Os;import org.apache.tools.ant.util.KeepAliveInputStream;/** * Launcher for Java applications. Allows use of * the same JVM for the called application thus resulting in much * faster operation. * * @since Ant 1.1 * * @ant.task category="java" */public class Java extends Task { private CommandlineJava cmdl = new CommandlineJava(); private Environment env = new Environment(); private boolean fork = false; private boolean newEnvironment = false; private File dir = null; private boolean failOnError = false; private Long timeout = null; //include locally for screening purposes private String inputString; private File input; private File output; private File error; // CheckStyle:VisibilityModifier OFF - bc protected Redirector redirector = new Redirector(this); protected RedirectorElement redirectorElement; // CheckStyle:VisibilityModifier ON private String resultProperty; private Permissions perm = null; private boolean spawn = false; private boolean incompatibleWithSpawn = false; /** * Normal constructor */ public Java() { } /** * create a bound task * @param owner owner */ public Java(Task owner) { bindToOwner(owner); } /** * Do the execution. * @throws BuildException if failOnError is set to true and the application * returns a nonzero result code. */ public void execute() throws BuildException { File savedDir = dir; Permissions savedPermissions = perm; int err = -1; try { checkConfiguration(); err = executeJava(); if (err != 0) { if (failOnError) { throw new ExitStatusException("Java returned: " + err, err, getLocation()); } else { log("Java Result: " + err, Project.MSG_ERR); } } maybeSetResultPropertyValue(err); } finally { dir = savedDir; perm = savedPermissions; } } /** * 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") or a security manager was * installed that prohibits ExitVM (default). * * @throws BuildException if required parameters are missing. */ public int executeJava() throws BuildException { return executeJava(getCommandLine()); } /** * Check configuration. * @throws BuildException if required parameters are missing. */ protected void checkConfiguration() throws BuildException { String classname = getCommandLine().getClassname(); if (classname == null && getCommandLine().getJar() == null) { throw new BuildException("Classname must not be null."); } if (!fork && getCommandLine().getJar() != null) { throw new BuildException("Cannot execute a jar in non-forked mode." + " Please set fork='true'. "); } if (spawn && !fork) { throw new BuildException("Cannot spawn a java process in non-forked mode." + " Please set fork='true'. "); } if (getCommandLine().getClasspath() != null && getCommandLine().getJar() != null) { log("When using 'jar' attribute classpath-settings are ignored. " + "See the manual for more information.", Project.MSG_VERBOSE); } if (spawn && incompatibleWithSpawn) { getProject().log("spawn does not allow attributes related to input, " + "output, error, result", Project.MSG_ERR); getProject().log("spawn also does not allow timeout", Project.MSG_ERR); getProject().log("finally, spawn is not compatible " + "with a nested I/O <redirector>", Project.MSG_ERR); throw new BuildException("You have used an attribute " + "or nested element which is not compatible with spawn"); } if (getCommandLine().getAssertions() != null && !fork) { log("Assertion statements are currently ignored in non-forked mode"); } if (fork) { if (perm != null) { log("Permissions can not be set this way in forked mode.", Project.MSG_WARN); } log(getCommandLine().describeCommand(), Project.MSG_VERBOSE); } else { if (getCommandLine().getVmCommand().size() > 1) { log("JVM args ignored when same JVM is used.", Project.MSG_WARN); } if (dir != null) { log("Working directory ignored when same JVM is used.", Project.MSG_WARN); } if (newEnvironment || null != env.getVariables()) { log("Changes to environment variables are ignored when same " + "JVM is used.", Project.MSG_WARN); } if (getCommandLine().getBootclasspath() != null) { log("bootclasspath ignored when same JVM is used.", Project.MSG_WARN); } if (perm == null) { perm = new Permissions(true); log("running " + this.getCommandLine().getClassname() + " with default permissions (exit forbidden)", Project.MSG_VERBOSE); } log("Running in same VM " + getCommandLine().describeJavaCommand(), Project.MSG_VERBOSE); } setupRedirector(); } /** * Execute the specified CommandlineJava. * @param commandLine CommandLineJava instance. * @return the exit value of the process if forked, 0 otherwise. */ protected int executeJava(CommandlineJava commandLine) { try { if (fork) { if (!spawn) { return fork(commandLine.getCommandline()); } else { spawn(commandLine.getCommandline()); return 0; } } else { try { run(commandLine); return 0; } catch (ExitException ex) { return ex.getStatus(); } } } catch (BuildException e) { if (e.getLocation() == null && getLocation() != null) { e.setLocation(getLocation()); } if (failOnError) { throw e; } else { log(e); return -1; } } catch (ThreadDeath t) { throw t; // cf. NB #47191 } catch (Throwable t) { if (failOnError) { throw new BuildException(t, getLocation()); } else { log(t); return -1; } } } /** * Set whether or not you want the process to be spawned; * default is not spawned. * @param spawn if true you do not want Ant to wait for the end of the process. * @since Ant 1.6 */ public void setSpawn(boolean spawn) { this.spawn = spawn; } /** * Set the classpath to be used when running the Java class. * * @param s an Ant Path object containing the classpath. */ public void setClasspath(Path s) { createClasspath().append(s); } /** * Add a path to the classpath. * * @return created classpath. */ public Path createClasspath() { return getCommandLine().createClasspath(getProject()).createPath(); } /** * Add a path to the bootclasspath. * @since Ant 1.6 * * @return created bootclasspath. */ public Path createBootclasspath() { return getCommandLine().createBootclasspath(getProject()).createPath(); } /** * Set the permissions for the application run inside the same JVM. * @since Ant 1.6 * @return Permissions. */ public Permissions createPermissions() { perm = (perm == null) ? new Permissions() : perm; return perm; } /** * Set the classpath to use by reference. * * @param r a reference to an existing classpath. */ public void setClasspathRef(Reference r) { createClasspath().setRefid(r); } /** * Set the location of the JAR file to execute. * * @param jarfile the jarfile to execute. * * @throws BuildException if there is also a main class specified. */ public void setJar(File jarfile) throws BuildException { if (getCommandLine().getClassname() != null) { throw new BuildException("Cannot use 'jar' and 'classname' " + "attributes in same command."); } getCommandLine().setJar(jarfile.getAbsolutePath()); } /** * Set the Java class to execute. * * @param s the name of the main class. * * @throws BuildException if the jar attribute has been set. */ public void setClassname(String s) throws BuildException { if (getCommandLine().getJar() != null) { throw new BuildException("Cannot use 'jar' and 'classname' " + "attributes in same command"); } getCommandLine().setClassname(s); } /** * Deprecated: use nested arg instead. * Set the command line arguments for the class. * * @param s arguments. * * @ant.attribute ignore="true" */ public void setArgs(String s) { log("The args attribute is deprecated. " + "Please use nested arg elements.", Project.MSG_WARN); getCommandLine().createArgument().setLine(s); } /** * If set, system properties will be copied to the cloned VM--as * well as the bootclasspath unless you have explicitly specified * a bootclaspath. * * <p>Doesn't have any effect unless fork is true.</p> * @param cloneVm if true copy system properties. * @since Ant 1.7 */ public void setCloneVm(boolean cloneVm) { getCommandLine().setCloneVm(cloneVm); } /** * Add a command-line argument. * * @return created argument. */ public Commandline.Argument createArg() { return getCommandLine().createArgument(); } /** * Set the name of the property in which the return code of the * command should be stored. Only of interest if failonerror=false. * * @param resultProperty name of property. * * @since Ant 1.6 */ public void setResultProperty(String resultProperty) { this.resultProperty = resultProperty; incompatibleWithSpawn = true; } /** * Helper method to set result property to the * passed in value if appropriate. * * @param result the exit code */ protected void maybeSetResultPropertyValue(int result) { String res = Integer.toString(result); if (resultProperty != null) { getProject().setNewProperty(resultProperty, res); } } /** * If true, execute in a new VM. * * @param s do you want to run Java in a new VM. */ public void setFork(boolean s) { this.fork = s; } /** * Set the command line arguments for the JVM. * * @param s jvmargs. */ public void setJvmargs(String s) { log("The jvmargs attribute is deprecated. " + "Please use nested jvmarg elements.", Project.MSG_WARN); getCommandLine().createVmArgument().setLine(s); } /** * Adds a JVM argument. * * @return JVM argument created. */ public Commandline.Argument createJvmarg() { return getCommandLine().createVmArgument(); } /** * Set the command used to start the VM (only if forking). * * @param s command to start the VM. */ public void setJvm(String s) { getCommandLine().setVm(s); } /** * Add a system property. * * @param sysp system property. */ public void addSysproperty(Environment.Variable sysp) { getCommandLine().addSysproperty(sysp); } /** * Add a set of properties as system properties. * * @param sysp set of properties to add. * * @since Ant 1.6 */ public void addSyspropertyset(PropertySet sysp) { getCommandLine().addSyspropertyset(sysp); } /** * If true, then fail if the command exits with a * returncode other than zero. * * @param fail if true fail the build when the command exits with a * nonzero returncode. */ public void setFailonerror(boolean fail) { failOnError = fail; incompatibleWithSpawn |= fail; } /** * Set the working directory of the process. * * @param d working directory. * */ public void setDir(File d) { this.dir = d; } /** * Set the File to which the output of the process is redirected. * * @param out the output File. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -