@@ -457,8 +457,17 @@ public static int runExternalAndWait(Ruby runtime, IRubyObject[] rawArgs, Map me
457457 } else {
458458 log (runtime , "Launching directly (no shell)" );
459459 cfg .verifyExecutableForDirect ();
460- aProcess = buildProcess (runtime , cfg .getExecArgs (), getCurrentEnv (runtime , mergeEnv ), pwd );
461460 }
461+ String [] args = cfg .getExecArgs ();
462+ // only if we inside a jar and spawning org.jruby.Main we
463+ // change to the current directory inside the jar
464+ if (runtime .getCurrentDirectory ().startsWith ("uri:classloader:" ) &&
465+ args [args .length - 1 ].contains ("org.jruby.Main" )) {
466+ pwd = new File (System .getProperty ("user.dir" ));
467+ args [args .length - 1 ] = args [args .length - 1 ].replace ("org.jruby.Main" ,
468+ "org.jruby.Main -C " + runtime .getCurrentDirectory ());
469+ }
470+ aProcess = buildProcess (runtime , args , getCurrentEnv (runtime , mergeEnv ), pwd );
462471 } catch (SecurityException se ) {
463472 throw runtime .newSecurityError (se .getLocalizedMessage ());
464473 }
@@ -497,12 +506,20 @@ public static long runExternal(Ruby runtime, IRubyObject env, IRubyObject prog,
497506 // execute command with sh -c
498507 // this does shell expansion of wildcards
499508 cfg .verifyExecutableForShell ();
500- aProcess = buildProcess (runtime , cfg .getExecArgs (), getCurrentEnv (runtime , (Map )env ), pwd );
501509 } else {
502510 log (runtime , "Launching directly (no shell)" );
503511 cfg .verifyExecutableForDirect ();
504- aProcess = buildProcess (runtime , cfg .getExecArgs (), getCurrentEnv (runtime , (Map )env ), pwd );
505512 }
513+ String [] finalArgs = cfg .getExecArgs ();
514+ // only if we inside a jar and spawning org.jruby.Main we
515+ // change to the current directory inside the jar
516+ if (runtime .getCurrentDirectory ().startsWith ("uri:classloader:" ) &&
517+ finalArgs [finalArgs .length - 1 ].contains ("org.jruby.Main" )) {
518+ pwd = new File ("." );
519+ finalArgs [finalArgs .length - 1 ] = finalArgs [finalArgs .length - 1 ].replace ("org.jruby.Main" ,
520+ "org.jruby.Main -C " + runtime .getCurrentDirectory ());
521+ }
522+ aProcess = buildProcess (runtime , finalArgs , getCurrentEnv (runtime , (Map )env ), pwd );
506523 } catch (SecurityException se ) {
507524 throw runtime .newSecurityError (se .getLocalizedMessage ());
508525 }
@@ -1432,7 +1449,7 @@ public static Process run(Ruby runtime, IRubyObject[] rawArgs, boolean doExecuta
14321449 log (runtime , "Launching directly (no shell)" );
14331450 cfg .verifyExecutableForDirect ();
14341451 }
1435- String [] args = cfg .execArgs ;
1452+ String [] args = cfg .getExecArgs () ;
14361453 // only if we inside a jar and spawning org.jruby.Main we
14371454 // change to the current directory inside the jar
14381455 if (runtime .getCurrentDirectory ().startsWith ("uri:classloader:" ) &&
0 commit comments