On Tue, 28 Jun 2011, Bill Janssen wrote:

I'm building a Java wrapper for the Python regex module, and thought I'd
use JCC.

I'm using JCC 2.9 on OS X 10.5.8, Java 5, Python 2.5, both 32-bit.  JCC
was also compiled with Java 5.

I wrote the Java interfaces for Regex, Match, and Group, defined in Java
the Python extension classes for those interfaces, wrote in Python the
"native" implementation for the extension classes, ran jcc on the jar
file, installed the Python module and the wrapped jar file module, and
tried a test program:

$ PYTHONPATH=/tmp:. 
/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/bin/java 
-classpath 
.:PythonRegex.jar:/Library/Python/2.5/site-packages/JCC-2.9-py2.5-macosx-10.5-i386.egg/jcc/classes
 
-Djava.library.path=/Library/Python/2.5/site-packages/JCC-2.9-py2.5-macosx-10.5-i386.egg
 test foo bar
org.apache.jcc.PythonException: An error occurred while creating Java VM
Traceback (most recent call last):
 File "/Users/wjanssen/projects/regex-java/Regex.py", line 8, in <module>
   javaregex.initVM(classpath=javaregex.CLASSPATH)
ValueError: An error occurred while creating Java VM

        at org.apache.jcc.PythonVM.instantiate(Native Method)
        at Regex.getRegex(Regex.java:14)
        at test.main(test.java:4)
$

Before calling initVM, the "javaregex" module has the following dir:

['AbstractStringBuilder', 'Appendable', 'Boolean', 'Byte', 'CLASSPATH',
'CharSequence', 'Character', 'Class', 'ClassLoader',
'ClassNotFoundException', 'Comparable', 'ConstVariableDescriptor',
'Double', 'Enumeration', 'Exception', 'FinalizerClass',
'FinalizerProxy', 'Float', 'Group', 'GroupAPI',
'IllegalAccessException', 'IllegalArgumentException',
'InstantiationException', 'Integer', 'InterruptedException',
'InvalidArgsError', 'Iterator', 'JArray', 'JArray_bool', 'JArray_byte',
'JArray_char', 'JArray_double', 'JArray_float', 'JArray_int',
'JArray_long', 'JArray_object', 'JArray_short', 'JArray_string',
'JCCEnv', 'JCC_VERSION', 'JObject', 'JavaError', 'Long', 'Match',
'MatchAPI', 'Number', 'NumberFormatException', 'Object', 'Package',
'PrintWriter', 'Regex', 'RegexAPI', 'RuntimeException',
'SecurityException', 'Short', 'StackTraceElement', 'String',
'StringBuffer', 'StringBuilder', 'StringWriter', 'Throwable', 'VERSION',
'Writer', '__builtins__', '__dir__', '__doc__', '__file__', '__name__',
'__path__', '_javaregex', 'findClass', 'getVMEnv', 'initVM', 'os',
'sys']

Here's the Regex.py code that's throwing the exception:

  import sys, os, re

  import regex
  import javaregex

  javaregex.initVM(classpath=javaregex.CLASSPATH)

Here's the Java code that's invoking it:

   public static Regex getRegex(Object[] patterns, int flags,
                                Map<String,Set<String>> named_lists) {
       PythonVM inst = PythonVM.get();
       if (inst == null) {
           inst = PythonVM.start("/usr/bin/python", new String[] {});
       }
       Regex regex = (Regex) (inst.instantiate("Regex", "PyRegex"));
       regex.initialize(patterns, flags, named_lists);
       return regex;
   }

Any ideas?  I followed the cookbook section in
http://lucene.apache.org/pylucene/jcc/documentation/readme.html#embedding
pretty religiously...

This error implies that PythonVM().init() didn't get called. This method is called from PythonVM.start() and sets the Java vm object for jcc so that when you later call initVM() from Python, it doesn't attempt to create and initialize another JVM since you're already running embedded in one. The native code for these methods is in jcc.cpp and, to debug this, you should run in gdb setting breakpoints to verify that these methods are getting called. You should also ensure that all extensions are built with --shared mode. Last but not least, PythonVM.start() must be called from the main thread.

Your logic above (if (inst == null) ...) is suspicious and unnecessary. The start() method sets up a VM object singleton anyway (see PythonVM.java):

    static public PythonVM start(String programName, String[] args)
    {
        if (vm == null)
        {
            vm = new PythonVM();
            vm.init(programName, args);
        }

        return vm;
    }

By the way, you might want to add a paragraph in that section about
adding the ["-framework", "Python"] flags for building JCC on OS X.  I
tripped over that again.

If you send a paragraph to this effect, I'll integrate it into the docs.

Thanks !

Andi..

Reply via email to