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.

KaHinCostner
  • 167
  • 3
  • 17
  • javax.swing.JButton and javax.swing.JFileChooser are classes. What are you trying to do? – jaudo Jul 24 '18 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 – KaHinCostner Jul 24 '18 at 19:51
  • You should compile your java classes. – Johannes Kuhn Jul 24 '18 at 19:52
  • @JohannesKuhn do you mean something like this here ? [hot to compile a java file in java](https://stackoverflow.com/questions/2279451/how-to-compile-a-java-file-in-java) – KaHinCostner Jul 24 '18 at 20:10

1 Answers1

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

Robert Brown
  • 10,888
  • 7
  • 34
  • 40