in reply to taking an output in a variable

That should work fine. Did you check $& for an error? In any case, my $res = $ENV{JAVA_HOME}; should also do the trick (and do it much more efficiently).

Update: mickeyn found the problem.