Forum: JRuby Java class and Jar.

4054f0e740c39c34d49b1389dd638203?d=identicon&s=25 biotin az (biotin)
on 2013-10-04 20:24
Hello.
I want to load jar file to the ruby projeckt.
When i load class - no problem.
--------------------------------------------
require 'java'
java_import 'FieldDemo'
#Here is FieldDemo.class
class FieldDemo
  field_accessor :somePrivateField => :some_field
end

obj = FieldDemo.new

obj.some_field = 1
a = obj.some_field
puts a
# => 1
--------------------------------------------

But when jar:
--------------------------------------------
require 'java'
#java_import 'FieldDemo'
require '/home/biotin/ruby/field.jar'
class FieldDemo
  field_accessor :somePrivateField => :some_field
end

obj = FieldDemo.new

obj.some_field = 1
a = obj.some_field
puts a
# => 1

-------------------------------------------
NoMethodError: undefined method `field_accessor' for FieldDemo:Class
------------------------------------------
I making jar with:
$jar cvf field.jar FieldDemo.class
C983ad33b47479f17a28e083e1bb3d8b?d=identicon&s=25 Eric West (edubkendo)
on 2013-10-04 21:38
(Received via mailing list)
you still have to import the classes you want to use.
4054f0e740c39c34d49b1389dd638203?d=identicon&s=25 biotin az (biotin)
on 2013-10-04 22:13
Thank you  Eric West.
Its was really noob question:)
C983ad33b47479f17a28e083e1bb3d8b?d=identicon&s=25 Eric West (edubkendo)
on 2013-10-04 22:28
(Received via mailing list)
no, it's not obvious at all. Basically, what require does is put the jar
on
the classpath, import/java_import makes the imported class available
under
its constant name. otherwise, you can access it via its fully qualified
name, with all the package information included. So either:

require 'blah/blah/blah.jar'
java_import 'org.blah.BlahClass'
class BlahClass# your codeend

or

require 'blah/blah/blah.jar'require 'java'
BlahClass = org.blah.BlahClass
class BlahClass# codeend


At least, that's my understanding of how it works.
C37cb1a6a19e23f067aae6ccb2d0de4b?d=identicon&s=25 Manuel Ramos Caro (Guest)
on 2013-10-05 18:30
(Received via mailing list)
Hi,...

Once you include java you can access all classes by it full java name.
You need to put jar on the classpath...

Example...

include Java

class ProgressBar < javax.swing.JFrame
include java.lang.Runnable

   def initialize
           super "Saving progress..."
           self.initUI
   end #def initialize

   def initUI

     panel = javax.swing.JPanel.new
     panel.setLayout nil
     self.getContentPane.add panel

     @progressBar = javax.swing.JProgressBar.new(0, 100)
     @progressBar.setValue(0)
     @progressBar.setStringPainted(true)

@progressBar.setBorder(javax.swing.BorderFactory.createEmptyBorder(20,
20, 20, 20))
     @progressBar.setBounds(0,0,100,100)

     panel.add @progressBar

     self.add(panel)
     self.revalidate()
     self.repaint()
     self.setDefaultCloseOperation javax.swing.JFrame::DISPOSE_ON_CLOSE
     self.setSize java.awt.Dimension.new(300,90)
     self.setResizable(false)
     self.setLocationRelativeTo nil
     self.setVisible true


     progress = 0
     @progressBar.setValue(0)
     while progress < 10000
       progress += 1
       @progressBar.setValue(progress/100)
       self.revalidate()
       self.repaint()
     end #while progress < 100000000

     self.dispose

   end #def initUI



end #class ProgressBarDemo


ProgressBar.new
C37cb1a6a19e23f067aae6ccb2d0de4b?d=identicon&s=25 Manuel Ramos Caro (Guest)
on 2013-10-05 19:26
(Received via mailing list)
Hi

I think that this reading must be useful for this question.

https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby

But I think that make two sides one for Java and another for Ruby its a
good practice because you can always
use Jars on Java way on other stages of development like deployments...
using Ruby way to give Java libraries
on your code could give you a late head hache when you try to put all
together.

Regards
4054f0e740c39c34d49b1389dd638203?d=identicon&s=25 biotin az (biotin)
on 2013-10-06 14:05
Thank you guys. Using Java library in Ruby is amazing:)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.