Different encoding in Jython's Java and Python level
Question: I'm using Sikuli (see sikuli.org) which uses jython2.5.2.

Here is a summary of the class Region on the Java level:

    
    
    public class Region {
    
        // < other class methods >
    
        public int type(String text) {
            System.out.println("javadebug: "+text); // debug output
            // do actual typing
        }
    }
    

On the Pythonlevel there is a Wrapperclass:

    
    
    import Region as JRegion            // import java class
    class Region(JRegion):
    
        # < other class methods >
    
        def type(self, text):
            print "pythondebug: "+text  // debug output
            JRegion.type(self, text)
    

This works as intended for ascii chars, but when I use ö, ä or ü as text, this
happens:

    
    
    // python input:
    # -*- encoding: utf-8 -*-
    someregion = Region()
    someregion.type("ä")
    
    // output:
    pythondebug: ä
    javadebug: Ã¤
    

The character seems to be converted wrongly when passed to the Java object.

I would like to know what exactly is going wrong here and how to fix this, so
that the characters entered in the pythonmethod are the same in the
javamethod. Thanks for your help


Answer: Looking from the Jython code you have to tell Java, that the string is UTF-8
encoded:

    
    
    def type(self, text):
        jtext = java.lang.String(text, "utf-8")
        print "pythondebug: " + text  // debug output
        JRegion.type(self, jtext)
    

