Khoi Tran Khoi Tran - 3 months ago 15
Java Question

How do I pass a string into Ant java? Somehow my program does not take arguments with white spaces

I am trying to get a string to be passed into ant in order for it to be ran as a parameter inside of a java function. Does anyone know how to do this?

Defining a string, this string has to have white spaces in between. For example, "Hello world hi"

<target name = "run" depends="compile">
<property name="input" value="report" />
<java classname="assn4">
<classpath>
<pathelement path="."/>
</classpath>
<arg line="${input}"/>
</java>
</target>

Rao Rao
Answer

Here is the complete working example.

Test.java

public class Test
{
   public static void main(String[] args)
   {
      String userString = args[0];      
      System.out.println("*****"+ userString+"*****");
   }
}

build.xml

<?xml version="1.0"?>
<project name="sample" basedir="." default="testJavaArgument">
   <target name="testJavaArgument">
      <java fork="true" failonerror="yes" classname="Test">
        <classpath>
            <pathelement path="."/>
        </classpath>
         <arg value="Hello World!"/>
      </java>
   </target>
</project>

Output

testJavaArgument:
     [java] *****Hello World!*****

BUILD SUCCESSFUL
Total time: 1 second

The simple problem you have is: change from <arg line="some value"> to <arg value="some value">