Skip to content

Instantly share code, notes, and snippets.

@dmikurube
Last active July 5, 2017 05:13
Show Gist options
  • Save dmikurube/398a728c4700a71698d5622cf39b0aaa to your computer and use it in GitHub Desktop.
Save dmikurube/398a728c4700a71698d5622cf39b0aaa to your computer and use it in GitHub Desktop.
Run a Ruby script file from Java with JRuby's ScriptingContainer -- both "out of a JAR" and "inside a JAR"
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
}
}
repositories {
mavenCentral()
}
apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'
dependencies {
compile 'org.jruby:jruby-complete:9.1.5.0'
testCompile 'junit:junit:4.+'
}
sourceSets {
main {
java {
srcDir '.'
exclude 'Test*.java'
}
resources {
srcDir '.'
include '*.rb'
}
}
test {
java {
srcDir '.'
include 'Test*.java'
}
}
}
test {
testLogging {
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
}
}
shadowJar {
manifest {
attributes 'Main-Class': 'JRubyRunScript'
}
}
task run(type: JavaExec) {
main = project.hasProperty('main') ? project.getProperty('main') : 'JRubyRunScript'
classpath = sourceSets.main.runtimeClasspath
args 'main'
}
import java.net.URL;
import java.net.URISyntaxException;
import org.jruby.embed.AttributeName;
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.PathType;
import org.jruby.embed.ScriptingContainer;
/*
* Out-of-jar:
* $ ./gradlew run
*
* In-jar:
* $ ./gradlew shadowJar
* $ java -jar build/libs/JRubyRunScript-all.jar
*/
public class JRubyRunScript {
public static void main(final String[] args) {
final ScriptingContainer jruby = new ScriptingContainer(LocalContextScope.SINGLETON);
final String[] argsJRuby = { "foo", "bar", "baz", "qux" };
jruby.setArgv(argsJRuby);
final URL classUrl = JRubyRunScript.class.getResource("JRubyRunScript.class");
if (classUrl.getProtocol().equals("jar")) {
System.out.println("Running in a jar file.");
// jruby.runScriptlet(PathType.ABSOLUTE, "file:/.../something.jar!/script.rb") does not work!
// jruby.runScriptlet(PathType.ABSOLUTE, "uri:classloader/script.rb") does not work!
jruby.runScriptlet(PathType.CLASSPATH, "script.rb");
} else if (classUrl.getProtocol().equals("file")) {
System.out.println("Running out of a jar file.");
// "user.dir" (working directory) should be the same as "script.rb".
jruby.setAttribute(AttributeName.BASE_DIR, System.getProperty("user.dir"));
// jruby.setScriptFilename("script.rb") does not work!
jruby.runScriptlet(PathType.RELATIVE, "script.rb");
} else {
System.out.println("Running at an unrecognized location!");
System.exit(1);
}
}
}
#!/usr/bin/env ruby
ARGV.each { |arg| puts "in-script: " + arg }
@dmikurube
Copy link
Author

dmikurube commented Jul 5, 2017

$ ./gradlew run
:compileJava
:processResources
:classes
:run
Running out of a jar file.
in-script: foo
in-script: bar
in-script: baz
in-script: qux

BUILD SUCCESSFUL

Total time: 3.762 secs

@dmikurube
Copy link
Author

dmikurube commented Jul 5, 2017

$ ./gradlew shadowJar
:compileJava
:processResources
:classes
:shadowJar

BUILD SUCCESSFUL

Total time: 3.415 secs
$ java -jar build/libs/JRubyRunScript-all.jar
Running in a jar file.
in-script: foo
in-script: bar
in-script: baz
in-script: qux

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment