1

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

3
  • What does sys.path contain? Commented Nov 29, 2010 at 21:54
  • Now when you mentioned it I have added sys.path.append('WEB-INF/classes') to highlighter.py. Testing Commented Nov 29, 2010 at 22:43
  • Now I have a different problem - I edited the original post Commented Nov 30, 2010 at 0:41

1 Answer 1

1

All right. I figured it out. Made few beginner errors but if you are wondering how to make Pygments work in your Java project here's full tutorial link

Highlights:

  • Using bare jython.jar is insufficient.
  • Adding Jython/Lib to your classpath doesn't work. I ended up unjarring jython.jar and adding Lib directory + all pygments files to the resulting structure and then jarring it up again
  • Factory example form the link above is convoluted. Much better way is to use Jython book chapter 10
  • For examples of changed highlighter.py, Highlighter.java and HighlighterFactory.java see the tutorial
Sign up to request clarification or add additional context in comments.

2 Comments

And it performs like hell. Abandoned, but the tutorial is good for general setup of Jython in Java webapp
Hi, how was performance just for code snippets of 10-20 lines max?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.