Assigning a Java package to a JRuby class

坚强是说给别人听的谎言 提交于 2019-12-10 17:29:48

问题


(This isn't a homework question, as it's above and beyond the class just for my personal interest.)

In my Java class this semester, our instructor is giving us the compiled JUnit tests that our completed labs should pass. For example, our first lab was to design this class:

package java112.labs1;

public class MysteryClassOne {

    public int mysteryMethodOne() {
        return 1;
    }
}

This is very trivial Java code and to give myself a bit more of a challenge as I've had previous Java experience, I'd like to do all of the assignments in JRuby, after completing them in Java. My only issue is all of the compiled tests are in the java112.labsX package and I can find no reference for how to assign a JRuby class to a Java package.

There you have it, in my typical long-winded fashion.

EDIT

Thanks to headius below for solving this issue. Here's the code in JRuby if anyone's interested:

require 'java'
java_package 'java112.labs1'

class MysteryClassOne
  java_signature 'int mysteryMethodOne()'
  def mysteryMethodOne
    return 1
  end
end

回答1:


Perhaps you're interested in something like this:

https://gist.github.com/789131

This gist shows using JRuby's "javac" compile mode to generate real Java classes. You can specify packages, interfaces to implement, signatures, and so on.



来源:https://stackoverflow.com/questions/4733125/assigning-a-java-package-to-a-jruby-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!