Java class and Jar

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

you still have to import the classes you want to use.

Thank you Eric West.
Its was really noob question:)

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.

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

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

Thank you guys. Using Java library in Ruby is amazing:)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs