Jruby: How to use third party java library?

957 Views Asked by At

I want to use java third party qr code libary https://github.com/kenglxn/QRGen library for generating qr code.

Please guide me as i don't know how to integrate java library in jruby.

1

There are 1 best solutions below

5
On BEST ANSWER

The trick in this case is to find how to properly import the 3rd party libs.

  • Please download the needed jars using any dependency system (I use ant with ivy).
  • This will download 5 jars based on the qr code library you mentioned: core-2.0.jar core-3.1.0.jar javase-2.0.jar javase-3.1.0.jar jfreesvg-2.1.jar

build.xml for ant:

<project xmlns:ivy="antlib:org.apache.ivy.ant" name="" default="retrieve">
  <!--setproxy proxyhost="" proxyport="" proxyuser="" proxypassword=""/-->
  <target name="retrieve">
    <ivy:retrieve/>
  </target>
</project>

ivy.xml for ivy:

<ivy-module version="2.0" >
  <info organisation="" module=""/>
  <dependencies defaultconf="default">
    <dependency org="net.glxn.qrgen" name="javase" rev="2.0"/>
  </dependencies>
</ivy-module>

Now typing ant retrieve will download and store 5 jars in lib subfolder.

If you do not have a dependency manager, here are the 5 jars urls you need to download manually and you need to move to a subfolder called lib:

I strongly advise you to use a java dependency manager if you wish one day to upgrade the revisions of the java libraries used in this code snippet.

Next step is to use the following ruby snip.rb code in the same folder. :

require 'java'
%w[
  lib/core-2.0.jar
  lib/core-3.1.0.jar
  lib/javase-2.0.jar
  lib/javase-3.1.0.jar
  lib/jfreesvg-2.1.jar
].map &method(:require)

# this is the CRITICAL line to get it to work...
include_class 'net.glxn.qrgen.javase.QRCode' 

# get QR file from text using defaults
# this will write a file (example: QRCode7247556396487679822.png) in java.io.tmpdir as defined in your JVM
file = QRCode.from("Hello World").file()
p file.name

# get QR stream from text using defaults
# if we redirect the stream as a byte array, we can have a better file control
include_class 'java.io.FileOutputStream'
stream = QRCode.from("Hello World").stream()
fos = FileOutputStream.new("QRCode.png")
fos.write(stream.toByteArray())
fos.close()

# fun with VCards
include_class 'net.glxn.qrgen.core.vcard.VCard' 
johnDoe = VCard.new("John Doe")
johnDoe.setEmail("[email protected]")
johnDoe.setAddress("John Doe Street 1, 5678 Doestown")
johnDoe.setTitle("Mister")
johnDoe.setCompany("John Doe Inc.")
johnDoe.setPhoneNumber("1234")
johnDoe.setWebsite("www.example.org")
stream = QRCode.from(johnDoe).stream()
fos = FileOutputStream.new("VCard.png")
fos.write(stream.toByteArray())
fos.close()

# all generated PNG can be decoded online using http://zxing.org/w/decode.jspx

Please note: jruby and java.io.tmpdir do not play well together in this particular jar case, as any call to QRcode creation with #file() will store file in java.io.tmpdir, whereever it is located. You've little control on the file location.

So I modified the original code answer to use the stream instead and create files with finer control.

You can verify all files generated with this useful URL: http://zxing.org/w/decode.jspx