0

I have a ruby file which contains all the necessary java_imports of my ruby project. That file (named java_import.rb) is located at lib/app/wrapper_classes/java_import.rb.

My java classes are located at lib/app/gallery/*.java, in package gallery;.

I have tried include_package "gallery" and include_class gallery.ThumbnailFileChooser, but I feel that I'm missing crucial knowledge.

This is an example of how I include java librairies in my ruby project.

module Awt
    java_import javax.swing.JButton
    java_import javax.swing.JFileChooser
end

So the question is : How can I do the same for my classes ?

Thanks in advance.

4
  • javax.swing.JButton and javax.swing.JFileChooser are classes. What are you trying to do? Commented Jul 24, 2018 at 19:37
  • oh yes, they are classes I successfully imported. I'm trying to import java classes I've made. For example, I would like to import lib/app/gallery/ThumbnailFileChooser.java Commented Jul 24, 2018 at 19:51
  • You should compile your java classes. Commented Jul 24, 2018 at 19:52
  • @JohannesKuhn do you mean something like this here ? hot to compile a java file in java Commented Jul 24, 2018 at 20:10

1 Answer 1

1

I would firstly suggest packaging your classes into a jar file in the typical java fashion. Then you can simply add require "myjar.jar" to your JRuby script and work with your classes just as you are doing with the javax.swing classes.

If for some reason you cannot do that, apparently you can include .class file directly as follows:

Let's say I have 'myclass.class' which has a class called 'myclass' in 'mypackage.myclass'. This causes two problems because JRuby doesn't like non-Camelcased classes (because Modules have to have cap-Alpha starts I think).

First, create a directory (under lib/ for example - though not necessary) called mypackage and put myclass.class in there.

Then this should work:

require 'java' $CLASSPATH << 'lib'

myclass = JavaUtilities.get_proxy_class('mypackage.myclass')

@myclass = myclass.new

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.