Last night I spent several hours hacking in "real" Java class support
for the JRuby compiler. You can access it using jrubyc --java.
Here's a simple example of creating a "main" class that's implemented in Ruby:
require 'java'
class Main
java_signature ["String[]"] => "void"
def self.main(args)
args.each {|arg| puts arg}
end
end
Compile this with jruby --java:
~/projects/jruby ➔ jrubyc --java mymain.rb
Compiling mymain.rb to class mymain
Generating Java class MyMain to MyMain.java
javac -cp /Users/headius/projects/jruby/lib/jruby-complete.jar:. MyMain.java
~/projects/jruby ➔ ls -l *.class
-rw-r--r-- 1 headius staff 1447 Jan 15 10:23 Main.class
-rw-r--r-- 1 headius staff 1442 Jan 15 17:30 mymain.class
And running with java directly:
~/projects/jruby ➔ java -cp lib/jruby-complete.jar:. Main Hello from Ruby
Hello
from
Ruby
This is the long-promised merge of ruby2java support into JRuby
proper. Not all features have been reworked for jrubyc yet, but it's
been easy so far. The entirety of this functionality adds only about
400 lines of code to jrubyc itself.
Here's a summary of the support so far:
* Static and instance methods work
* Signatures can be specified with strings or constants if they exist.
The signature format is up for debate, but ideally needs to be a no-op
when running normally
* Imports specified with java_import will be added to the resulting Java code
* The compiler first outputs .java source for all generated classes,
and then runs javac. This was a quick way to get it working
The following features are not there yet:
* Interface implementation
* Extending classes
* Annotations
* Public fields or constants
* Other Java 5 features (generics, enums)
* Packages (not sure the best way to do this yet)
If you'd like to help out, the code is in master under
lib/ruby/site_ruby/shared/jruby/compiler.rb, and it's pretty simple to
follow. I'm pretty excited about it.
Here's a longer example that includes bundling Rake and building a
free-standing Java application out of it:
http://gist.github.com/278497
Happy hacking!
- Charlie
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email