-
Notifications
You must be signed in to change notification settings - Fork 0
FAQs
Somewhat. See JRuby on JME.
Here is a page dedicated to answering this question: JRuby on Web Start
Include a "hash bang" line on the top of your jruby script like this:
#!/usr/bin/env jruby
That will use the system wide installed jruby interpreter to run your script.
If you would like to use a local copy of JRuby to run your script, give the relative or absolute path to the jruby program:
#!/usr/bin/env jruby-1.6.8/bin/jruby
To run the script directly, you need to set the "x" flag on it:
chmod +x your_script.rb
You can now start your script with
./your_script.rb
You can also try the jruby-launcher gem
, which installs a native jruby
program.
jruby -S gem install jruby-launcher
Now you can place the full path to JRuby in the shebang line, along with parameters if you wish:
#!/usr/local/jruby/bin/jruby -w
You can try passing the usual -J-Xmx<size>
to the sub-JRuby. But beware:
$ jruby -e '%x{jruby -J-Xmx256m -e true}'
warning: -J-Xmx256m argument ignored (launched in same VM?)
Historically, JRuby has been very slow to start up, so we have tried to recognize some commands that look like they may be launching more Ruby programs and we start those in the same JVM. In that case, you actually can't adjust the heap.
We have a special system property that controls this behavior VM-wide (pass on the command-line as -J-Djruby.launch.inproc=false
):
jruby.launch.inproc=true|false
#Set in-process launching of e.g. system('ruby ...'). Default is true
And you can also control this from Ruby code for the current JRuby runtime:
require 'jruby'
JRuby.runtime.instance_config.run_ruby_in_process = false
With the inproc
setting disabled, you can now pass -J
arguments to the sub-JRuby as it will be launching a new JVM.
In the future, as JRuby and JVM startup performance increases, we may flip the default in-process launching behavior to false
to match most peoples' expectations. See also this thread on the JRuby mailing list for a discussion of jruby.launch.inproc
.
See Troubleshooting Memory Use.
On Windows, the native jruby.exe
launcher usually loads the JVM's DLL directly and spins up the JVM in
the same process, rather than launching an external process using the java.exe
command. However, -splash
and a few other options are only supported by the java.exe
command and are unrecognized by the DLL.
Because we have no way of knowing what options are or are not supported by the DLL, we provide a flag for the
jruby.exe
command to force using java.exe
and spinning an external process: -Xfork-java
. Passing this flag to the jruby
command line will allow those other java.exe
-only flags to work correctly.
The Ruby Language Home Page has links to a wide range of resources for learning about Ruby, downloading releases of the C implementation of Ruby, documentation on Ruby, and community outlets for talking about Ruby.
The Pragmatic Programmers are the publishers of the de facto standard Ruby text, "Programming Ruby". It is considered a must-have manual to the Ruby language and libraries.
Almost everything you learn about the Ruby language is directly applicable to working with JRuby. JRuby aims to be a drop-in replacement for the C implementation of Ruby.
See Differences Between MRI and JRuby.
Yes. JRuby officially started supporting 1.9.2 features as of the 1.6.x line of releases, though it
is not default in those versions. You must specify the --1.9
flag at the command line or in a JRUBY_OPTS
environment variable, or add "compat.version=1.9" to .jrubyrc (on 1.6.5 or higher).
Ruby 1.9 mode is default in JRuby 1.7.0 and later, 1.8.7 compatibility can still be used with the --1.8
flag.
Frequently users want to run JRuby from NetBeans, from Eclipse, launched from an existing Java application, or in other ways that don't use the command-line scripts. In addition to having jruby.jar
and its dependency jars available in CLASSPATH, you must also have the following system property set:
-Djruby.home=<path-to-root-of-JRuby-installation>
You might also want to ensure the maximum memory is set higher than the JVM default. JRuby's own command-line scripts use a maximum of 256MB.
You have two options:
- Always invoke JRuby with e.g.,
jruby -S gem
. - Put a handy bash snippet like this in your .bashrc to create
j
aliases to all the available commands (rails
becomesjrails
,rake
becomesjrake
, etc.) for f in $JRUBY_HOME/bin/; do f=$(basename $f) case $f in jruby|jirb*|.bat|.rb|_*) continue ;; esac eval "alias j$f='jruby -S $f'" done
JRuby can only see the gems installed with the gem command shipped with JRuby. The gems installed this way will be stored under $JRUBY_HOME/lib/ruby
. If you try to run Rails and get the error, "Cannot find gem for Rails ~>1.2.2.0:" (or whatever version you are using), the problem is probably that you haven't installed the Rails-gem and its dependencies with the JRuby gem-command.
When I implement a Java interface and provide my own initialize method, I can't construct it anymore.
In JRuby 0.9.0, any class implementing a Java interface must explicitly call super
in their initializers to set up the interface proxy. Add a super
call to your implementation's initialize
method and it should work.
If you don't want to launch JRuby as a separate process, we recommend you use the Bean Scripting Framework (BSF) or Java 6's Scripting Support (JSR223). We do not recommend calling directly into the JRuby runtime, since that code is subject to change.
You need to do something along the lines of:
try {
rubyCode.doSomething();
} catch (RaiseException e) {
e.getException().printBacktrace(System.err);
}
Note that Java 6's scripting via the BSF libraries might not preserve stack traces, and also launches more slowly. It's often preferable (as of February 2007) to use JRuby's own integration.
Why do I get a java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.visit()
error when running JRuby 1.X with a custom classpath?
The problem stems from an ASM jar conflict. It was found by setting a custom classpath, which includes a version of Spring that has a conflicting ASM jar, and then invoking JRuby. The easiest workaround is to build jruby-complete
as a single jar and edit the JRuby config file (not sure about Windows, but it should be similar) to source only the jruby-complete
jar. You will need to add ALL other jars, which I believe includes your jdbc driver as well.
svn co http://svn.codehaus.org/jruby/tags/jruby-1_0/
cd jruby-1_0
ant jar-complete
vi bin/jruby (see diff for edit)
#diff bin/jruby bin/jruby.orig
#89c89
#< for j in "$JRUBY_HOME"/lib/jruby-complete.jar; do
#---
#> for j in "$JRUBY_HOME"/lib/*.jar; do
How come Java can't find resources in class folders that I've appended to the $CLASSPATH global variable at runtime?
JRubyClassLoader extends java.net.URLClassLoader
, which treats any URL that doesn't end with a slash as a jar. See URLClassLoader.
The file encoding for the .rb
file containing the special characters must be set to UTF-8.
% jruby -rjava -e "puts java.lang.System.get_property('java.version')"
This happens if you require ActiveRecord-JDBC in your environment, but you haven't installed the gem. Make sure you have the ActiveRecord-JDBC gem installed.
You just need to set GEM_HOME to point to your CRuby's gem directory. For example, in tcsh:
setenv GEM_HOME /usr/local/lib/ruby/gems/1.8
Note that on Windows, you should specify the path using unix-style directory separators. For example, if ruby is installed in C:\ruby
:
set GEM_HOME=c:/ruby/lib/ruby/gems/1.8
I get the error "undefined method 'cattr_accessor' for ActiveRecord::Base:Class (NoMethodError)" after configuring activerecord-jdbc. What is wrong?
You're not requiring activerecord-jdbc properly. Try requiring activerecord-jdbc in config/environment.rb
this way:
require 'jdbc_adapter'
You might not have set the production DB settings in database.yml
properly. Goldspike by default builds a war that runs the app in production mode.
You can confirm the problem by finding the production.log
file. If you're using JBoss, then it lives in $JBOSS_HOME/server/my_app/tmp/my_rails_war
''1234''.war
, where ''1234'' is a random number generated by JBoss.
In production.log
, you'll see an error this:
RuntimeError (The driver encountered an error: can't convert nil into String)
To fix this, just set the production DB settings in database.yml
properly.
You can add the config directory to the JRuby classpath in config/environment.rb
:
require 'java'
$CLASSPATH << "file:///#{File.expand_path(File.join(RAILS_ROOT, 'config'))}/"
Now, because ResourceBundle.getBundle
doesn't seem to use the JRuby class loader, we need to pass it explicitly. In the file where you want to use the resource bundle do :
include Java
require 'jruby'
...
bundle = java.util.ResourceBundle::getBundle
("i18n", java.util.Locale::FRENCH, JRuby.runtime.jruby_class_loader)
Now you just need to put your i18n[_*].properties
files in config/
, et voilà!
When JRuby runs at the command line, it loads into the bootstrap class loader. This class loader lives above CLASSPATH, so libraries you add to CLASSPATH are not normally visible. Because Class.forName
uses the class loader of the nearest non-system class that called it, it ends up seeing JRuby as the caller and looking for classes only in CLASSPATH.
The workaround for this is simple: Uuse JRuby's import or include_class methods to load the class, or just reference it directly as in Java::my.package.MyClass
or org.postgresql.Driver
.
Sometimes, developers name their inner classes beginning with a lowercase letter. This prevents JRuby from accessing it the "normal way" (it thinks it's a method). For example:
In Java (from the com.sun.enterprise.ee.cms.core
package of the Shoal project)
GMSConstants.shutdownType.INSTANCE_SHUTDOWN
Whereas in JRuby, you can get at that using the following:
>> ShutdownType = JavaUtilities.get_proxy_class('com.sun.enterprise.ee.cms.core.GMSConstants$shutdownType')
You can convert existing ruby arrays to primitive Java arrays very simply:
[1,2,3].to_java # makes an object array
[1,2,3].to_java :byte # makes a byte array
[1,2,3].to_java :String # makes a String array
To create empty arrays:
Java::byte[12].new # makes a new byte[]
java.lang.String[12].new # makes a new String[]
There are two separate ways:
- Include an interface into the class.
class MyListener
include java.awt.event.ActionListener
def actionPerformed(event)
button.text = "I have been pressed"
end
end
- Use implicit closure conversion.
button.add_action_listener { |event| button.text = "I have been pressed" }
You can do an Ahead-Of-Time (AOT) compile by using these instructions.
Add .bat
extension to JRuby scripts.
If you try to run JRuby on cygwin for Windows and use the commands without the .bat
extension, it will default to the bourne shell versions, which might not work properly.
Example using gems:*
$JRUBY_HOME/bin/gem.bat install rails -y --no-ri --no-rdoc
Why does my db:migrate fail with "undefined method 'create_database' for class '#Class:01x1e30857'"?
In this case, try to run the migrations with:
jruby -S rake db:migrate SKIP_AR_JDBC_RAKE_REDEFINES=1
You can also just put the following line somewhere in your Rakefile, or in a custom Rakefile in lib/tasks/*.rake
:
ENV['SKIP_AR_JDBC_RAKE_REDEFINES'] = '1'
There are two scenarios in which JNI problems occur:
- When the jar is in JRuby 1.1.2's lib.
- When the jar is loaded using ruby's "require" statement in JRuby < 1.1.
These two scenarios are described below.
JRuby 1.1.2 +
JRuby 1.1.2 changed how it set up the classpath. Earlier versions merged the CLASSPATH environment variable with all .jar files in JRUBY_HOME/lib
and passed them to the JVM via the -classpath
option. JRuby 1.1.2 instead passes all .jar
files in lib
to the JVM via the -Xbootclasspath/a:
option, while CLASSPATH is passed using -classpath
. Jar files that use JNI (e.g. sqljdbc.jar
) don't work correctly when they're in the boot classpath, so they should not be placed in JRuby's lib
directory. Instead, they should be loaded with require
or by setting the CLASSPATH environment variable.
You tried the performance tuning option --server but received an error like "Error: no server' JVM at
c:\Program Files\Java\jre6\bin\server\jvm.dll'."
On windows the Java Runtime Environment (JRE) inclues the client VM by default but does not include the server VM. You should download an install the Java Development Kit (JDK).
If you want to use the server VM with the JRE, you can copy JDK's jre\bin\server folder to a bin\server directory in the Java SE Runtime Environment.
Some JDKs (like OpenJDK/Sun/Oracle/Hotspot) try to use IPv6 addresses for e.g. "localhost" when possible. If using only similar JDKs to connect, this isn't a problem. However, other tools usually prefer IPv4 addresses and may have trouble connecting.
You can adjust how the JDK (at least OpenJDK/Sun/Oracle/Hotspot) decides to use IPv6 versus IPv4 addresses via these networking properties. You can add them to the Java startup options like this:
export JAVA_OPTS="-Djava.net.preferIPv4Stack=true"
jruby ...
Or pass directly to JRuby using -J-D like this:
jruby -J-Djava.net.preferIPv4Stack=true ...