Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble using java class within jruby

Tags:

java

ruby

jruby

I'm trying to use Java Opencl from within jruby, but am encountering a problem which I can't solve, even with much google searching.

require 'java'
require 'JOCL-0.1.7.jar'

platforms = org.jocl.cl_platform_id.new
puts platforms.class
org.jocl.CL.clGetPlatformIDs(1, platforms, nil)

when I run this code using: jruby test.rb I get the following error, when the last line is uncommented:

#<Class:0x10191777e>
TypeError: cannot convert instance of class org.jruby.java.proxies.ConcreteJavaP
roxy to class [Lorg.jocl.cl_platform_id;
  LukeTest at test.rb:29
    (root) at test.rb:4

Just wondering whether anyone has an idea on how to solve this problem?

EDIT: ok so I think I've solved the first part of this problem by making platforms an array:

platforms = org.jocl.cl_platform_id[1].new

but that led to this error when adding the next couple of lines:

context_properties = org.jocl.cl_context_properties.new()
context_properties.addProperty(org.jocl.CL::CL_CONTEXT_PLATFORM, platforms[0])
CodegenUtils.java:98:in `human': java.lang.NullPointerException
    from CodegenUtils.java:152:in `prettyParams'
    from CallableSelector.java:462:in `argumentError'
    from CallableSelector.java:436:in `argTypesDoNotMatch'
    from RubyToJavaInvoker.java:248:in `findCallableArityTwo'
    from InstanceMethodInvoker.java:66:in `call'
    from CachingCallSite.java:332:in `cacheAndCall'
    from CachingCallSite.java:203:in `call'
    from test.rb:36:in `module__0$RUBY$LukeTest'
    from test.rb:-1:in `module__0$RUBY$LukeTest'
    from test.rb:4:in `__file__'
    from test.rb:-1:in `load'
    from Ruby.java:679:in `runScript'
    from Ruby.java:672:in `runScript'
    from Ruby.java:579:in `runNormally'
    from Ruby.java:428:in `runFromMain'
    from Main.java:278:in `doRunFromMain'
    from Main.java:198:in `internalRun'
    from Main.java:164:in `run'
    from Main.java:148:in `run'
    from Main.java:128:in `main'

for some reason when I print the class of platforms[0] it's listed as NilClass!?

like image 919
kellpossible Avatar asked Oct 03 '11 06:10

kellpossible


People also ask

Does JRuby need Java?

To run JRuby you will need a JRE (the Java VM runtime environment) version 8 or higher.

Can Ruby call Java?

You can call Java libraries from Ruby, script Java applications with an embedded interpreter, or even use Ruby libraries from Java. Exactly the same Ruby code runs in JRuby and the standard Ruby interpreter, except where Ruby code calls into native (C-coded) or Java libraries.

Should I use JRuby?

If you are using an application that will stay alive for long, and you want better runtime support, JRuby can be a great way to go. Otherwise, you can safely wait until you need these things to actually make the move (it is likely to go smoothly). Excellent answer, that's exactly what I needed to know.


1 Answers

You are overlooking a very simple mistake. You write

platforms = org.jocl.cl_platform_id.new

but that line creates a single instance of the class org.jocl.cl_platform_id. You then pass that as the second parameter to org.jocl.CL.clGetPlatformIDs in

org.jocl.CL.clGetPlatformIDs(1, platforms, nil)

and that doesn't work, because the second argument of the method requires an (empty) array of org.jocl.cl_platform_id objects.

What the error says is: "I have something that is a proxy for a Java object and I can't turn it into an an array of org.jocl.cl_platform_id objects, as you are asking me to do.

If you just say

platforms = []

and pass that in, it might just work :).

like image 169
Confusion Avatar answered Oct 03 '22 03:10

Confusion