I'm trying to use Pygments in Java project by including Jython.jar. In my Java project I have src/main/python source directory where I placed pygments files into pygments folder so they end up on the classpath. Now I created highlighter.py file in the src/main/python with the following content:
import sys
sys.path.append('WEB-INF/classes')
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
class Highlighter:
def colorize(code, lexername):
lexer = get_lexer_by_name(lexername, stripall=True)
formatter = HtmlFormatter(linenos=True, cssclass="source")
return highlight(code, lexer, formatter)
Then I defined Java interface Highlighter.java
public interface Highlighter {
String colorize(String rawText, String lexer);
}
And lastly I created factory class that uses PythonInterpreter to expose highlighter.py as a Jython object as described here
Now, when I run this line of code
Highlighter hl = (Highlighter) jf.getJythonObject(Highlighter.class.getName(),
"WEB-INF/classes/highlighter.py");
I get PyException as follows
Traceback (most recent call last):
File "WEB-INF/classes/highlighter.py", line 3, in
from pygments import highlight
LookupError: no codec search functions registered: can't find encoding
This occurs if I use Jython v. 2.5.0. In 2.1 I had to copy Jython/Lib directory under src/main/python but then I end up missing IOString module
Or maybe there's a better way to achieve what I'm trying to achieve? I'm curious if anyone was able to use Pygments in Java project. I would greatly appreciate any suggestions
sys.pathcontain?