如何在ant上显示system.out(在控制台中)?

clj7thdc  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(411)

我能够在ant上构建java程序,并且已经正确配置。但是,我想知道如何在使用ant的控制台上看到system.out.println/system.out.print?
注意:我没有使用ant,我正在尝试探索。我希望我的问题不是愚蠢的?
简单程序

package antTesting;
    public class Helloworld
    {

        /**
         * @param args
         */
        public static void main( String[] args )
        {
            System.out.println( " hello there !!!! " );

        }

    }

相同的build.xml

<?xml version="1.0"?>
<project name="Ant-Test" default="main" basedir=".">
  <!-- Sets variables which can later be used. -->
  <!-- The value of a property is accessed via ${} -->
  <property name="src.dir" location="src" />
  <property name="build.dir" location="build" />
  <property name="dist.dir" location="dist" />
  <property name="docs.dir" location="docs" />

  <!-- Deletes the existing build, docs and dist directory-->
  <target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${docs.dir}" />
    <delete dir="${dist.dir}" />
  </target>

  <!-- Creates the  build, docs and dist directory-->
  <target name="makedir">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${docs.dir}" />
    <mkdir dir="${dist.dir}" />
  </target>

  <!-- Compiles the java code (including the usage of library for JUnit -->
  <target name="compile" depends="clean, makedir">
    <javac srcdir="${src.dir}" destdir="${build.dir}">
    </javac>

  </target>

  <!-- Creates Javadoc -->
  <target name="docs" depends="compile">
    <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
      <!-- Define which files / directory should get included, we include all -->
       <fileset dir="${src.dir}">
                <include name="**" />
           </fileset>
    </javadoc>
  </target>

  <!--Creates the deployable jar file  -->
  <target name="jar" depends="compile">
    <jar destfile="${dist.dir}\de.vogella.build.test.ant.jar" basedir="${build.dir}">
      <manifest>
        <attribute name="Main-Class" value="test.Main" />
      </manifest>
    </jar>
  </target>

  <target name="main" depends="compile, jar, docs">
    <description>Main target</description>
  </target>

</project>

当我 run > ant 按命令

Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook
Picked up _JAVA_OPTIONS: -Xrunjvmhook -Xbootclasspath/a:C:\PROGRA~2\HP\Sprinter\
bin\JAVA_S~1\classes;C:\PROGRA~2\HP\Sprinter\bin\JAVA_S~1\classes\jasmine.jar
Buildfile: C:\Users\ms025226\workspace\TestingWorld\build.xml

clean:
   [delete] Deleting directory C:\Users\ms025226\workspace\TestingWorld\build
   [delete] Deleting directory C:\Users\ms025226\workspace\TestingWorld\docs
   [delete] Deleting directory C:\Users\ms025226\workspace\TestingWorld\dist

makedir:
    [mkdir] Created dir: C:\Users\ms025226\workspace\TestingWorld\build
    [mkdir] Created dir: C:\Users\ms025226\workspace\TestingWorld\docs
    [mkdir] Created dir: C:\Users\ms025226\workspace\TestingWorld\dist

compile:
    [javac] C:\Users\ms025226\workspace\TestingWorld\build.xml:26: warning: 'inc
ludeantruntime' was not set, defaulting to build.sysclasspath=last; set to false
 for repeatable builds
    [javac] Compiling 1 source file to C:\Users\ms025226\workspace\TestingWorld\
build

jar:
      [jar] Building jar: C:\Users\ms025226\workspace\TestingWorld\dist\de.vogel
la.build.test.ant.jar

docs:
  [javadoc] Generating Javadoc
  [javadoc] Javadoc execution
  [javadoc] Loading source file C:\Users\ms025226\workspace\TestingWorld\src\Hel
loworld.java...
  [javadoc] Constructing Javadoc information...
  [javadoc] Standard Doclet version 1.6.0_31
  [javadoc] Building tree for all the packages and classes...
  [javadoc] Building index for all the packages and classes...
  [javadoc] Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook
  [javadoc] Picked up _JAVA_OPTIONS: -Xrunjvmhook -Xbootclasspath/a:C:\PROGRA~2\
HP\Sprinter\bin\JAVA_S~1\classes;C:\PROGRA~2\HP\Sprinter\bin\JAVA_S~1\classes\ja
smine.jar
  [javadoc] Building index for all classes...

main:

BUILD SUCCESSFUL
Total time: 2 seconds

当我为类路径定义目标时,我收到了一个错误

<!-- to printoutputs -->    
  <target name="Helloworld" description="Hello world">
        <java classname="antTesting.Helloworld">
            <arg value="arg1"/>
            <arg value="arg2"/>         
        </java>
 </target>

  <target name="main" depends="compile, jar, docs, Helloworld">
    <description>Main target</description>
  </target>

错误:

Helloworld:
     [java] Could not find antTesting.Helloworld. Make sure you have it in your classpath
     [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:138)
     [java]     at org.apache.tools.ant.taskdefs.Java.run(Java.java:764)
     [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:218)
     [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:132)
     [java]     at org.apache.tools.ant.taskdefs.Java.execute(Java.java:105)
     [java]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     [java]     at java.lang.reflect.Method.invoke(Method.java:597)
     [java]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
     [java]     at org.apache.tools.ant.Task.perform(Task.java:348)
     [java]     at org.apache.tools.ant.Target.execute(Target.java:357)
     [java]     at org.apache.tools.ant.Target.performTasks(Target.java:385)
     [java]     at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
     [java]     at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
     [java]     at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
     [java]     at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
     [java]     at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
     [java]     at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:423)
     [java]     at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:137)
     [java] Java Result: -1
btqmn9zl

btqmn9zl1#

您需要system.out还是使用日志(apachecommons,slf4j…)更有趣?

lrl1mhuk

lrl1mhuk2#

如果您想从ant执行类,您没有在任何地方指定它,请在脚本中添加如下内容:

<target name="Helloworld" description="Hello world">
    <java classname="your.package.Helloworld">
        <arg value="arg1"/>
        <arg value="arg2"/>
        ...
    </java>
</target>

元素 <arg value="..."/> 传递给主方法数组参数: main( String[] args ) 现在添加目标 Helloworld 作为对其他目标的依赖关系,或从运行选项中选择它。

相关问题