简体   繁体   中英

Run java executable from a groovy script on linux

I know it's a bit strange but i'm trying to run the java executable from a groovy script:

def mycommand = "java -version"
def env = System.getenv().collect { k, v -> "$k=$v" }
def mycommandOut = mycommand.execute(env,null).text
println mycommandOut

this work well on windows but when I run it on Linux it seems to does nothing.

If i set mycommand = "ls -la" it works like a charm.

If i set mycommand = "foobar" i get: java.io.IOException: Cannot run program "foobar": error=2

Any hints?

This is because java -version prints the info to STDERR on Linux. But you are capturing the STDOUT.

def proc = "java -version".execute()
proc.waitFor()
def version = proc.err.text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM