Akash senta Akash senta - 2 months ago 14
Groovy Question

How to pass XML file content into java command as command line argument?

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

Answer

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])
Comments