On Mon, 1 Jul 2013, Johan Jonkers wrote:

Hello,

I have been playing around with JCC to see if it would provide in the needs we have here at work to interface Java with Python. I have encountered one issue in which I hope someone on this mailinglist might be able to help me with. If this is not the right place to ask then I apologize in advance.

This issue I am having is that I would like to create two packages compiled with JCC in which classes from one package are used by classes in the other pacakge. I would like to use those classes in Python but am having problems doing so that I don't understand yet.

In package 1 is the class shown below:

package nl.seecr.freestyle;

public class Sum {
   private int _sum;

   public Sum() {
       _sum = 0;
   }

   public void add(int value) {
       _sum += value;
   }

   public int value() {
       return _sum;
   }
}

The second package holds a class what uses the Sum Class:

package org.cq2.freestyle;

import nl.seecr.freestyle.Sum;

public class SumWrapper {

   private Sum total;

   public SumWrapper() {
       this(new Sum());
       System.out.println("Empty constructor");
   }

   public SumWrapper(Sum sum) {
       total = sum;
   }

   public void add(int value) {
       total.add(value);
   }

   public int value() {
       return total.value();
   }

   public Sum asSum() {
       Sum sum = new Sum();
       sum.add(value());
       return sum;
   }

   public void printValue() {
       System.out.println(value());
   }
}

I can compile these classes into .class files and put them in jars and have those compiled with JCC:
python -m jcc \
   --root ${ROOT} \
   --use_full_names \
   --shared \
   --arch x86_64 \
   --jar cq2.jar \
   --classpath ./seecr.jar \
   --python cq2 \
   --build \
   --install

export PYTHON_PATH=${ROOT}/usr/local/lib/python2.7/dist-packages
python -m jcc \
   --root ${ROOT} \
   --use_full_names \
   --import cq2 \
   --shared \
   --arch x86_64 \
   --jar seecr.jar \
   --python seecr \
   --build \
   --install

In my understanding the "--import cq2" argument should prevent jcc from creating a wrapper for the Sum class in the seecr package itself but use the one in the cq2 package.

This all compiles without errors but when I run the following python program:

import seecr
seecr.initVM()

import cq2
cq2.initVM()

from nl.seecr.freestyle import Sum
from org.cq2.freestyle import SumWrapper

sum = Sum()
sum.add(5)
print "Sum value", sum.value()

wrapper = SumWrapper(sum)
print wrapper.value()

The 1st print shows the value 5 as expected. The 2nd print however shows 0 and I had not expected that. If I run the same program in Java and use the jars I created earlier, the 2nd print shows 5 (as expected). The "Empty constructor" message is also shown when running this python program and I had not expected that to happen.

The "asSum" method in the SumWrapper class is not available in the Python version of the class. I do not understand yet why that is.

I haven't been able to find many examples or documentation on the options for compiling with JCC. I am hoping that someone here on the mailinglist can point me in the right direction. Any help would be really appreciated.

I haven't had time yet to reproduce the problem but you may want to try to add an explicit request to wrap the Sum class - by just listing it on the second jcc call command line. No wrapper will be generated for it, because of the --import statement, but methods in the second jar with Sum in their signature should then get wrapped.

 python -m jcc \
    --root ${ROOT} \
    --use_full_names \
    --import cq2 \
    --shared \
    --arch x86_64 \
    --jar seecr.jar \
    --python seecr \
    --build \
    --install \
    nl.seecr.freestyle.Sum

Andi..

Reply via email to