diff --git a/core/src/main/java/org/jruby/RubyKernel.java b/core/src/main/java/org/jruby/RubyKernel.java index 61a0ccd6ec7..7b767109cc9 100644 --- a/core/src/main/java/org/jruby/RubyKernel.java +++ b/core/src/main/java/org/jruby/RubyKernel.java @@ -43,6 +43,16 @@ package org.jruby; +import java.io.ByteArrayOutputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import jnr.constants.platform.Errno; +import jnr.posix.POSIX; + import org.jruby.anno.FrameField; import org.jruby.anno.JRubyMethod; import org.jruby.anno.JRubyModule; @@ -78,13 +88,6 @@ import org.jruby.util.io.OpenFile; import org.jruby.util.io.PopenExecutor; -import java.io.ByteArrayOutputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - import static org.jruby.RubyBasicObject.UNDEF; import static org.jruby.RubyEnumerator.enumeratorizeWithSize; import static org.jruby.RubyInteger.singleCharByteList; @@ -1804,25 +1807,27 @@ private static IRubyObject execCommon(ThreadContext context, IRubyObject env, IR // attempt to shut down the JMX server jmxStopped = runtime.getBeanManager().tryShutdownAgent(); - runtime.getPosix().chdir(System.getProperty("user.dir")); + final POSIX posix = runtime.getPosix(); + + posix.chdir(System.getProperty("user.dir")); if (Platform.IS_WINDOWS) { // Windows exec logic is much more elaborate; exec() in jnr-posix attempts to duplicate it - runtime.getPosix().exec(progStr, argv); + posix.exec(progStr, argv); } else { // TODO: other logic surrounding this call? In jnr-posix? @SuppressWarnings("unchecked") final Map ENV = (Map) runtime.getENV(); - ArrayList envStrings = new ArrayList(ENV.size() + 1); + ArrayList envStrings = new ArrayList<>(ENV.size() + 1); for ( Map.Entry envEntry : ENV.entrySet() ) { envStrings.add( envEntry.getKey() + '=' + envEntry.getValue() ); } envStrings.add(null); - int status = runtime.getPosix().execve(progStr, argv, envStrings.toArray(new String[envStrings.size()])); - if (Platform.IS_WSL && status == -1) { - if (runtime.getErrno(runtime.getPosix().errno()) == runtime.getErrno().getClass("ENOMEM")) { - runtime.getPosix().exec(progStr, argv); + int status = posix.execve(progStr, argv, envStrings.toArray(new String[envStrings.size()])); + if (Platform.IS_WSL && status == -1) { // work-around a bug in Windows Subsystem for Linux + if (posix.errno() == Errno.ENOMEM.intValue()) { + posix.exec(progStr, argv); } } }