Executing initialization code before java_import in JRuby

Does java_import always execute before initialize in JRuby?

I need the following code to execute

def initialize vlc_path
@vlc_path = vlc_path || get_vlc_path

before I can use:

java_import ‘uk.co.caprica.vlcj.binding.LibVlc’

The java_import always executes first and fails. How to go about it?

I guess I’m confused. Can you clarify a few things for me?

  • Are you saying that even if the “def initialize” is before the
    “java_import”, the “java_import” runs first? They should happen in the
    order they’re encountered in the code.
  • Why do you need to have this method defined before you import that

I think I might have seen some discussion about this that indicated
you are using a precompiled .rb class, yes? In that case, imports may
be loaded earlier than the rest of the script. You could try to move
the imports to a separate script.

Can you explain more about what you’re doing?

  • Charlie

I may be missing something here, but I think the problemis that
java_import is evaluated when the class is loaded, whereas the
modification to @vlc_path is not happening until the class’ instance is

Is the java_import inside the initialize() method? Also, how is the
java_import going to know anything about @vlc_path? I don’t know
anything about NativeLibrary.addSearchPath(), but did you mean to use
@vlc_path in that call?

  • Keith

Keith R. Bennett

Sorry for a little ambiguity in my question. I am using JNA to access

java_import ‘uk.co.caprica.vlcj.binding.LibVlc’

to work, I need the VLC native files “libvlc” to be in the search path.

I would my code to look for default vlc installation search paths, if
does not supply one. So the code ( updated ) serves this purpose :

def initialize vlc_path=nil
@vlc_path = vlc_path || get_default_vlc_path

So I want that as soon as my class is initialized it adds the search
and then imports the ‘uk.co.caprica.vlcj.binding.LibVlc’.

I understand that, but do you understand my question about the timing of
the evaulation? Where in your code is the import statement?

  • Keith

Keith R. Bennett

You could put the java_import in the initialize…or in a “singleton”
initialization method somewhere in your system you call only once. It
sounds like the it’s loading in expected order based on the way you
have it laid out.

  • Charlie

I have the java_import statement inside the class declaration and not in
initialization method.