cootcraig
Posts: 15
Joined: Sun Jan 13, 2013 4:56 pm

Some success with Java/JRuby

Fri Mar 07, 2014 8:32 pm

Greetings,

I'm exploring Jruby/Java as a platform for projects on Raspberry Pi.
I've a got a proof of concept that is in progress at github:

https://github.com/CootCraig/reel_https_auth_websock

The first step was to demonstrate websockets, which is working.

I want to evolve this into a radio connected Arduino gateway.

The versions of jruby and jdk I'm using:

$ jruby -v
jruby 1.7.11 (1.9.3p392) 2014-02-24 86339bb on Java HotSpot(TM) Client VM 1.8.0-b129 +indy [linux-arm]

I seem to be fairly bleeding edge here, but that is fine for now. I think
this can turn out to be a nice platform.

cootcraig
Posts: 15
Joined: Sun Jan 13, 2013 4:56 pm

Re: Some success with Java/JRuby

Wed Mar 12, 2014 5:23 pm

I've now got a demo that shows a bidirectional gateway to
an Arduino. This Repository has the Java based HTTPS
server and the Arduino sketch

https://github.com/CootCraig/pi_arduino_io_1

PeterWAWood
Posts: 12
Joined: Thu Jul 03, 2014 4:40 am

Re: Some success with Java/JRuby

Thu Jul 10, 2014 5:41 am

May I ask how you installed JRuby 1.7.11 ?

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Some success with Java/JRuby

Thu Apr 02, 2015 8:28 pm

I did a search for jruby, found your thread. You might find this of some interest?
Latest Oracle Java and JRuby...

http://www.raspberrypi.org/forums/viewt ... 19#p732719

RichR

PeterWAWood
Posts: 12
Joined: Thu Jul 03, 2014 4:40 am

Re: Some success with Java/JRuby

Thu Apr 02, 2015 11:31 pm

Many thanks for that link Rich. Actually I did finally manage to install JRuby but I didn't leave a note here.

This is how I did it: http://peterwawood.blogspot.com/2014/09 ... ry-pi.html

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Some success with Java/JRuby

Fri Apr 03, 2015 7:59 pm

It would be good to know how many have an interest in jruby, and in what way.
Is interest as a scripting front end for Java, or are people looking for the multi-threading capabilities of the Ruby/Java implementation? :?
Also if anyone cares, how many like rails (or other frameworks), and who prefer without?

RichR
p.s. The jrubyc compiler appears to be broken at the moment.
https://github.com/jruby/jruby/issues/2499

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Some success with Java/JRuby

Tue May 12, 2015 7:29 pm

Not sure if this shouldn't be under java, 8-) or even if anyone will care, but hopefully it will help someone...
Jruby, using JDBC to Apache derby embedded database. I tested this on Linux mint first, then applied it to my pi2b. (took a lot of getting here as it crosses over different documentation paths and is therefore very obscure (was to me anyway!!) lots of research, lots to read.)
I have Java oracle8 version installed. Downloaded the latest jruby (jruby-bin-9.0.0.0.pre2.tar.gz) and Apache derby packages (db-derby-10.11.1.1-bin.tar.gz) (same files on mint and on rpi).
sudo tar -xvf the packages and moved them to /opt/jruby and /opt/Apache/db-derby-10.11.1.1-bin
Next, added the following to /etc/profile ->
export JRUBY_HOME=/opt/jruby
export PATH=$JRUBY_HOME/bin:$PATH
export DERBY_INSTALL=/opt/Apache/db-derby-10.11.1.1-bin
export CLASSPATH=$DERBY_INSTALL/lib/derby.jar:$DERBY_INSTALL/lib/derbytools.jar:.
(I also copied derbyclient.jar from the lib subdirectory of derby to my ruby directory. See second line of code)

This (probably quite primitive jruby code now works...) jruby name.rb
require 'java'
require './derbyclient.jar'
import 'org.apache.derby.jdbc.EmbeddedDriver'
connString = "jdbc:derby:MyGraphDb;create=true"
conn = Java::JavaSql::DriverManager.getConnection(connString)
sql = "CREATE TABLE test (NUM INT PRIMARY KEY, ADDR VARCHAR(12))"
stmt = conn.createStatement()
begin
stmt.execute(sql)
rescue
puts "\n table already exists.\n"
end
stmt.close()

# change the data in the next line to insert different info
sql = "INSERT INTO test values (160, 'silly square')"
stmt = conn.createStatement()
begin
s = stmt.execute_update(sql)
rescue
puts "\n error in insert (duplicate primary key?) \n"
end
stmt.close()

sql = "select * from test"
stmt = conn.createStatement()
rs = stmt.executeQuery(sql)

while (rs.next) do
puts "#{rs.getString("NUM")} -- #{rs.getString("ADDR")}"
end
stmt.close()
conn.close()

puts "\n\n"
It creates the database in your execution directory.
Hope this helps someone. Note that with sqlite3, I failed miserably, because (it said) c based sqlite wasn't supported.
RichR
p.s. I should add: thanks to this links for the major clue that got me there
http://www.tompurl.com/2009/08/21/testi ... ruby-jdbc/

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Some success with Java/JRuby

Tue May 12, 2015 8:42 pm

The second line in my code (previous post) can be replaced by:-
require ENV['DERBY_INSTALL'] + '/lib/derbyclient.jar'
which means you don't have to copy the jar file into your own folder.

RichR

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Some success with Java/JRuby

Fri May 15, 2015 6:32 pm

I had some fun installing sequel for jruby on raspbian (wheezy) - with jruby pre2 as in my previous post.
sudo /opt/jruby/bin/jruby -S gem install sequel
Fetching: sequel-4.22.0.gem (100%)
NotImplementedError: waitpid unsupported or native support failed to load
flock at org/jruby/RubyFile.java:298
block in cache_update_path at /opt/jruby/lib/ruby/stdlib/rubygems/remote_fetcher.rb:310
open at org/jruby/RubyIO.java:1126
I don't know what the problem (is)/was, I suspect a missing library. I cheated it by copying the file /opt/jruby/lib/ruby/gems/shared/cache/sequel-4.22.0.gem from my mint linux.... on rerunning the install gem command the gem is now installed. sidestepped the error? - (not recommended for the sane?) Anyone know what causes it?

I now can run sequel database jruby code (somewhat like activerecord) as follows:-

Code: Select all

#require 'java'
#require ENV['DERBY_INSTALL'] + '/lib/derbyclient.jar'
#require "rubygems"
require "sequel"

#connstring = "jdbc:derby:memory:myDb;create=true"
#changed to save results in myDb
connstring = "jdbc:derby:myDb;create=true"
DB = Sequel.connect( connstring )

# create an items table
DB.create_table :items do
  primary_key :id
  String :name
  Float :price
end

# create a dataset from the items table
items = DB[:items]

# populate the table
items.insert(:name => 'abc', :price => rand * 100)
items.insert(:name => 'def', :price => rand * 100)
items.insert(:name => 'ghi', :price => rand * 100)

# print out the number of records
puts "Item count: #{items.count}"

# print out the average price
puts "The average price is: #{items.avg(:price)}"
The example is from the sequel sitehttp://sequel.jeremyevans.net/ slightly modded to use Apache derby. Note the memory option in conn string (is hashed out) to use power of db in memory...

RichR
p.s. I hate Rails....
p.p.s you don't need the two java lines now - it is all ruby code. (have hashed them out)

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Some success with Java/JRuby

Wed May 20, 2015 11:53 am

For anyone interested: This is to show a more realistic example of jruby code using the java db (Apache Derby) and sequel:-

Code: Select all

# Embryonic Graph style micro-service scheduler (?)
require "sequel"
connstring = "jdbc:derby:memory:myDb;create=true"
DB = Sequel.connect( connstring )

class Ents
  attr_accessor :nm, :id, :to, :rship
  def initialize
     DB.create_table :ents do
       primary_key :id
       String :name
     end
     @@ents = DB[:ents]
     
    DB.create_table :relations do
      primary_key :id
      foreign_key :name, :ents
      String :rship
      Int :to
    end
    @@relations = DB[:relations]

  end # of initialize

  def introduce( nm )
    begin
      @@ents.insert( :name => nm )
    rescue
      puts "Unable to insert ent."
    end
  end # of introduce

  def relate( nm, rship, to )
     @nm = self.getId( nm )
     @to = self.getId( to )
   begin
     @@relations.insert( :name => @nm, :rship => rship, :to => @to)
   rescue
     puts "Unable to insert relationship."
   end
  end # of relate

  def getNm( id )
   @a = @@ents[ :id => id ]
   return @a.fetch(:name)
  end # of getNm

  def getId( nm )
    @a = @@ents[ :name => nm ]
    return @a[:id]
  end # of getId

  def showAll
    puts "\nAll entities :\n"
    puts @@ents.all
    puts "End of list.\n"
    puts "\nAll relationships :\n"
    puts @@relations.all
    puts "End of list.\n"
  end # of showAll
end # of ents

ents = Ents.new
ents.introduce("fred")
ents.introduce("george")
ents.introduce("peter")

ents.relate("fred", "brother", "peter")
ents.relate("peter","father", "george")

ents.showAll
It is probably a bit primitive, I am learning JRuby, and sequel , and boosting my JDBC skills as I go!
result of running on pi2 (after a delay while the JVM loads):-
All entities :
{:id=>1, :name=>"fred"}
{:id=>2, :name=>"george"}
{:id=>3, :name=>"peter"}
End of list.

All relationships :
{:id=>1, :name=>1, :rship=>"brother", :to=>3}
{:id=>2, :name=>3, :rship=>"father", :to=>2}
End of list.
Note that if you want to you can also access java.....
RichR

p.s. I have since modified getId above to "introduce" a new name if the name is not found. (so doing a relate with new names will add those names as well as the relationship)....fun?
p.p.s. Properties also added .....

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Some success with Java/JRuby

Thu May 21, 2015 6:59 am

I logged my jruby gem issue - it looks like it is a known problem, so hopefully will be sorted soon. Then jruby will be a valuable language to use on the pi. Will add a post when the fix is available.
RichR

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Some success with Java/JRuby

Sun Jun 14, 2015 6:15 pm

I logged this on the JRUBY support site (problems with jffi)

I think I have found a solution: (not fully tested yet) - I found a similar problem - #1561
so on my raspberry pi (pi 2 model) : -

sudo apt-get install ant
git clone https://github.com/jnr/jffi.git
ant jar

then copy the libjffi-1.2.so from jffi/dist/jni/arm-Linux to jruby/lib/jni/arm-linux replacing the one already there (rename that first!!!)
jgem install couch_potato has now worked, and my program runs.....

RichR
p.s. this was with the very latest jruby release candidate......
p.p.s also celluloid gem has installed without problem.

p.p.s see also under general discussion viewtopic.php?f=63&t=101480&p=771066#p771066

Return to “Java”