0

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>
Khoi Tran
  • 45
  • 8
  • This is not a duplicate – Khoi Tran Dec 01 '15 at 23:14
  • 1
    Why is it not a duplicate?! – Kalle Richter Dec 02 '15 at 00:47
  • @KhoiTran if it's not a duplicate, then please edit your question to clarify exactly what you're after, and why it's _not_ the same thing as in that other question. – Jeen Broekstra Dec 02 '15 at 01:34
  • In the other solution, the function call allows for strings that are not separated by white space. ant -Darg0=Hello -Darg1=World run. However, the implementation I want can be separated by white space: ant -Darg0="Hello john" -Darg1="World of cheese" run – Khoi Tran Dec 02 '15 at 02:14
  • So, doesn't quoting work? – ivan_pozdeev Dec 02 '15 at 02:18
  • Somehow it doesn't. Yeah I am not sure why it is not working. I posted the code and changed the question. So at this point, I am assuming that the other code does work with quotes and somehow mines doesn't. – Khoi Tran Dec 02 '15 at 02:19
  • I don't know if quoting works or not but in my program it is not working and I believe I use the same method as described in the other thread. – Khoi Tran Dec 02 '15 at 02:26
  • Do you want to pass argument to main method of class assn4? See if this helps http://www.tutorialspoint.com/ant/ant_executing_java_code.htm – Rao Dec 02 '15 at 07:50
  • Yes, I want to pass a string with spaces into the main method of assn4. The String can be of variable length and has a variable amount of white spaces. – Khoi Tran Dec 02 '15 at 07:51
  • Please see this http://www.tutorialspoint.com/ant/ant_executing_java_code.htm – Rao Dec 02 '15 at 07:55
  • I read that, the string that they have is hard coded. I need to be able to run ant and send the argument through there. – Khoi Tran Dec 02 '15 at 07:57
  • @KhoiTran, http://stackoverflow.com/questions/34028083/java-ant-passing-whole-argument-line-to-ant-and-to-program --appears same as this? – Rao Dec 02 '15 at 07:57
  • It is the same because it was made by me. I accidentally double clicked too fast and it made two? I have no idea how that happened. – Khoi Tran Dec 02 '15 at 07:58
  • @KhoiTran, can you tell what is happening? – Rao Dec 02 '15 at 07:58
  • The string that they have in their example in the link you gave me is hard coded. Even more, that string doesn't even contain spaces. – Khoi Tran Dec 02 '15 at 08:00
  • @KhoiTran, that is ok. But what is happening when you run your target with spaces in the string? try running `ant -debug`. – Rao Dec 02 '15 at 08:01
  • nvm I got it. Sorry about that. I wasn't using the run target. – Khoi Tran Dec 02 '15 at 08:14
  • Not sure how to end this thread since it was a silly and not enlightening discovery. – Khoi Tran Dec 02 '15 at 08:14
  • @KhoiTran, The simple problem you have is: change from to . Also added the complete answer. – Rao Dec 02 '15 at 08:24

1 Answers1

0

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">

Rao
  • 20,781
  • 11
  • 57
  • 77