0

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?

2
  • Does it seem to do nothing or there's an error? What happens on the target Linux box when you execute that command in a shell? Is the java executable found? Commented Feb 26, 2019 at 14:36
  • It does nothing. I think java is found because if i set mycommand="foobar" i've go a java.io.IOException: Cannot run program "foobar" Commented Feb 26, 2019 at 14:41

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.