4

I have one xml file. I want to pass the xml content into java command line argument.

abc.xml :

   <a>
    <block1>abc</block1>
    <block2>xyz</block2>
    <block3>pqr</block3>
    </a>

Below is my groovy/java code to get data from file and pass it into java command line argument.

File fl = new File("PATH/abc.xml")
String filecontent = fl.getText()
String cmd = "Java -cp abc.jar package.CLASSNAME "+filecontent 
Process proc = Runtime.getRuntime().exec(cmd);
proc.waitFor()

This command is not executing just comes out of process. Why??

eddie
  • 1,252
  • 3
  • 15
  • 20
Akash senta
  • 483
  • 7
  • 16
  • 1
    `java -cp abc.jar package.CLASSNAME "$(cat PATH/abc.xml)"` is enough. Don't write programs. – laune Feb 13 '15 at 13:12
  • Sorry i forget to mention i am doing it in windows machine, and when i get text which contains \n and spaces in between so it doesn't consider it as single argument, i tried [this](http://stackoverflow.com/questions/5511096/java-convert-formatted-xml-file-to-one-line-string) but not working... – Akash senta Feb 13 '15 at 13:37
  • Quoting should make spaces harmless. But on windows (unless it has improved in recent versions) the length restriction for command lines may hurt you. – laune Feb 13 '15 at 14:22
  • 1
    Not sure why you are sending xml string as argument? How about send the file name and read file inside java program? – Rao Feb 13 '15 at 17:57

1 Answers1

1

Untested code off the top of my head, so take it for what it's worth:

File fl = new File("PATH/abc.xml")
String filecontent = fl.readLines().*trim().join(' ')
String cmd = "java -cp abc.jar package.CLASSNAME \"${filecontent}\"" 
Process proc = Runtime.getRuntime().exec(cmd);
proc.waitFor()

Since this is Groovy code, I would also change it to invoke package.Classname.main() directly, rather than spinning off a process and another JVM:

File fl = new File("PATH/abc.xml")
String filecontent = fl.readLines().*trim().join(' ')
package.CLASSNAME.main([filecontent])
BalRog
  • 3,216
  • 23
  • 30
  • Yes , you are right, but i suppose to run that another JVM code is on server and earlier code on my client side. So i need to start another JVM process independently. – Akash senta Feb 13 '15 at 15:26