jashkenas / ruby-processing

Code as Art, Art as Code. Processing and Ruby are meant for each other.
http://github.com/jashkenas/ruby-processing/wikis
Other
1.28k stars 94 forks source link

Strange requiring behavior #20

Closed efoxepstein closed 14 years ago

efoxepstein commented 14 years ago

Given this foo.rb:

require 'java'
require 'foo.jar'
Java::Foo::Bar.baz()

This works:

$ rvm jruby
$ ruby foo.rb

This fails:

rp5 run foo.rb

with:

/Users/__/.rvm/gems/jruby-1.5.2/gems/ruby-processing-1.0.9/lib/ruby-processing/app.rb:6:in `const_missing': cannot load Java class foo.Bar (NameError)

Is rp doing something funky with require?

monkstone commented 14 years ago

My usual approach is to stick to the convention of putting the foo.jar in a folder foo, placing that in a library folder. Then in foo.rb load the library 'foo', works for me. The rp5 does the require 'java' for you.

efoxepstein commented 14 years ago

Tried that. Exact same error. Tried "load_library 'foo'; import 'foo'". Any ideas?

monkstone commented 14 years ago

class Test < Processing::App

load_library :foo

include_package "foo"

def setup

fred = Java::Foo::Bar.new()

fred.baz()

end

end

Worked for me!

Or if you really wanted a class method:-

class Test < Processing::App

load_library :foo

include_package "foo"

def setup

Java::Foo::Bar::baz()

end

end

Where my baz method was System.out.println("baz");

baz got printed

efoxepstein commented 14 years ago

Here is an exact transcript from my Terminal:

$ cat Bar.java
package foo;

public class Bar {
    public Bar(){
        System.out.println("Working");
    }
}
    $ javac Bar.java
    $ jar cvf library/foo/foo.jar Bar.class 
added manifest
adding: Bar.class(in = 344) (out= 258)(deflated 25%)
$ cat foo.rb
load_library :foo
include_package 'foo'

def setup
  Java::Foo::Bar.new
end
    $ rp5 run foo.rb 
Exception in thread "Animation Thread" :1:in `const_missing': cannot load Java class foo.Bar (NameError)
    from foo.rb:6:in `setup'
    from :1
    ...internal jruby stack elided...
    from #<Class:01x2eb80f1c>.const_missing(foo.rb:6)
    from Sketch.setup(:1)
    from (unknown).(unknown)(:1)

Am I packaging things up wrong? Any ideas on how to debug?

monkstone commented 14 years ago

I don't know but I did javac foo/Bar.java? But it wouldn't compile otherwise? Wait a minute its the archiving thats the problem:- jar cvf foo.jar foo (its the folder as source....)

efoxepstein commented 14 years ago

Was your Bar.java inside of a folder called foo? Mine was in the working directory. How did you jar yours up? Could you show me your Java file and all of the commands you executed?

Thanks so much!

monkstone commented 14 years ago

package foo;

public class Bar{

public Bar(){}

public static void baz(){

System.out.println("baz");

}

}

That was the class method version, the other should be obvious yes in a folder foo.

I think we might have just got crossed messages hope thats OK.

efoxepstein commented 14 years ago

Yeah, that's it. I had to include the folder in the jar. I wasn't aware of that. Thanks so much!