Ruby code to JAR
JRuby allows you to compile to .class files, which you can jar up normally. You'll just have to include jruby.jar as well. From here:
The typical way to run the AOT
compiler is to run
jrubyc <script name>
Or, on Microsoft Windows:
jruby -S jrubyc <script name>
This command outputs a .class file in
the current directory with parent
directories and package matching where
the file lives. So the following
command
jrubyc foo/bar/test.rb
will output
foo/bar/test.class
To run the file produced by the
compiler, use the -cp (class
searchpath) parameter with both the
current directory (parent directory
for foo/bar/test.class above) and the
path to jruby.jar, and execute it as
you would a normal Java class named
foo.bar.test:
java -cp .:/path/to/jruby.jar
foo.bar.test
Importing .jar file in Ruby: how to use the class
In jruby, you can access java classes by package name and class name.
See https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby
Your java main method is static, so no need for new
. So this should work.
jruby.Jruby.main()
But I suggest to rename your java class and package. You may get name clashes, as it is likely that something inside jruby is also called jruby
.
Also the name is just wrong, because it is a java class and not a jruby class.
Edit:
After consulting the document, the above is only correct for standard java packages. There are several ways to access a java class:
use Ruby Module Syntax:
Java::Foo::Foo.main()
the dot syntax, as I tried first.
"Second way: for the top-level Java packages java, javax, javafx, org,
and com you can type in a fully qualified class name basically as in
Java, for example, java.lang.System or org.abc.def.className ..."
You either need to put your java class in the package com.foo
or write an extra method to access the package:
def edu
Java::Edu
end
"And then you can use use usual Java package names like
edu.abc.def.ClassName"
after java_import, the imported Class "will be available in the global name space"
java_import 'foo.Foo'
Foo.main()
In all cases, you need to make sure that jruby sees the your jar file.
" loading jar-files via require searches along the $LOAD_PATH for
them, like it would for normal ruby files."
JRuby to jar executable
Type this in the command line:
cd C:/Users/Someone/Path/To/Class/
jar cvfm executable.jar manifest.mf Foo.class
Where manifest.mf
is something like
Main-Class: Foo
Change C:/Users/Someone/Path/To/Class/ to the path to your files and change Foo
to the name of your class. This will create a jar file called "executable.jar."
Use Ruby to copy files into an jar file
If it was me I would almost certainly just call the jar
command within Ruby to do this:
system 'jar uf jar_file.jar input_file(s).class'
# or
%x[ 'jar uf jar_file.jar input_file(s).class' ]
Reference here.
If you still want to do this without calling jar
you should be able to do it with rubyzip, since JAR files are just ZIP files with a particular structure. Something like this:
require 'zip/zip'
filename = 'class_file.class'
Zip::ZipOutputStream::open "jar_file.jar" do |zip|
zip.put_next_entry 'dest/path/in/jar/' + filename # don't forget the path
File.open filename, 'rb' {|f| zip.write f.read }
end
There are also a few Ruby wrappers for libarchive that could do this. E.g.
JRuby requires jar file in runtime
You can also execute the require
statement whenever you want -- ruby is dynamic like that, you don't need to execute it on program boot or source file load.
def create_and_require_jar
do_something_to_create_jar
require 'the/jar/i/created.jar'
end
If your problem is that the jar doesn't exist yet at the moment you execute the require
-- simply change when you execute the require, to be after you've created the .jar.
While I have never created a .jar dynamically to be used by the same program that created it (and I'm not sure why you'd want to), I have done a require
of a .jar in Jruby, as part of dynamic logic where sometimes I require it and sometimes I don't, and do the require inside conditional logic, not immediately on program boot. It's worked fine.
And I've found it super nice that you can do this in Jruby, conditional loading of a Jar based on program logic (first checking to see if the jar exists, or based on arguments or configuration) -- if you can do that in straight Java, I don't know how, although you might be able to!
Related Topics
Android Getintent().Getextras() Returns Null
Android 6.0 (Marshmallow): How to Play Midi Notes
How to Print to the Console in Android Studio
How to Update Information in an Android Activity from a Background Service
How to Fix Install_Parse_Failed_Manifest_Malformed in My Android Application
Theme.Appcompat.Light.Darkactionbar - No Resource Found
How to Override Action Bar Back Button in Android
Does Proguard Work to Obfuscate Static String Constants
What Does '->' (Arrow) Mean in Gradle's Dependency Graph
Why Do We Have to Call Super in Android Sometimes
Android (Java) Simple Send and Receive with Server - Fast Setup Challenge
How to Crop a Bitmap for Imageview
Could Not Find Method in Parent or Ancestor Context
Encrypt with Node.Js Crypto Module and Decrypt with Java (In Android App)
Getlocationonscreen() VS Getlocationinwindow()