A pure Java syntax highlighting library that generates XHTML output from source code. This is a maintained fork of the original jhighlight project with critical bug fixes, particularly for multi-byte character handling.
- Pure Java Implementation - No external dependencies for highlighting
- Multiple Language Support - Java, Groovy, JavaScript, C++, XML, HTML, XHTML, LZX
- XHTML Output - Clean, standards-compliant output
- Multi-byte Character Support - Properly handles UTF-8 and other encodings
- Command-line Tool - Batch process files and directories
- Servlet Filter - Easy integration with web applications
- RIFE Template Support - Highlights RIFE template tags
<dependency>
<groupId>org.codelibs</groupId>
<artifactId>jhighlight</artifactId>
<version>1.1.0</version>
</dependency>implementation 'org.codelibs:jhighlight:1.1.0'import org.codelibs.jhighlight.renderer.XhtmlRendererFactory;
import org.codelibs.jhighlight.renderer.Renderer;
import java.io.*;
// Get a renderer for Java code
Renderer renderer = XhtmlRendererFactory.getRenderer("java");
// Highlight code from an input stream
InputStream input = new FileInputStream("Example.java");
OutputStream output = new FileOutputStream("Example.java.html");
renderer.highlight("Example.java", input, output, "UTF-8", false);# Highlight a single file
java -jar jhighlight.jar MyClass.java
# Highlight all files in a directory
java -jar jhighlight.jar src/
# Specify output directory and encoding
java -jar jhighlight.jar -d output -e UTF-8 src/
# Generate fragments instead of complete HTML documents
java -jar jhighlight.jar --fragment MyClass.java| Option | Description |
|---|---|
--verbose |
Output progress messages |
--fragment |
Generate HTML fragments instead of complete documents |
-d <dir> |
Specify destination directory for output files |
-e <encoding> |
Specify file encoding (default: UTF-8) |
| Language | File Extensions | Renderer Type |
|---|---|---|
| Java | .java |
java |
| Groovy | .groovy |
groovy |
| BeanShell | .bsh, .beanshell |
beanshell / bsh |
| JavaScript | .js |
(via factory) |
| C++ | .cpp, .cxx, .c++ |
cpp / cxx / c++ |
| XML | .xml |
xml |
| HTML | .html, .htm |
html |
| XHTML | .xhtml |
xhtml |
| LZX | .lzx |
lzx |
import org.codelibs.jhighlight.renderer.XhtmlRendererFactory;
Set<String> supportedTypes = XhtmlRendererFactory.getSupportedTypes();
System.out.println("Supported types: " + supportedTypes);The original jhighlight library (from jhighlight.dev.java.net) had critical issues with multi-byte character handling, causing garbled output for source files containing UTF-8 characters, Japanese, Chinese, Korean, and other non-ASCII text. This fork fixes those issues and is actively maintained.
If you're using Apache Tika (which depends on the original jhighlight), you'll need to exclude the old dependency and include this version:
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.6</version>
<exclusions>
<exclusion>
<groupId>com.uwyn</groupId>
<artifactId>jhighlight</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.codelibs</groupId>
<artifactId>jhighlight</artifactId>
<version>1.1.0</version>
</dependency>This fork maintains the original package structure with additions:
- Original packages:
com.uwyn.jhighlight.* - New packages:
org.codelibs.jhighlight.*
Most classes are now available in the org.codelibs.jhighlight package for better namespace management.
- Java 8 or higher
- Maven 3.6 or higher
# Clone the repository
git clone https://github.com/codelibs/jhighlight.git
cd jhighlight
# Build with Maven
mvn clean install
# Run tests
mvn test
# Generate code coverage report
mvn jacoco:reportThe compiled JAR will be available in the target/ directory.
import org.codelibs.jhighlight.servlet.HighlightFilter;
// Configure in web.xml
<filter>
<filter-name>HighlightFilter</filter-name>
<filter-class>org.codelibs.jhighlight.servlet.HighlightFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HighlightFilter</filter-name>
<url-pattern>*.java</url-pattern>
</filter-mapping>The generated XHTML includes CSS classes that you can style:
<span class="java_keyword">public</span>
<span class="java_type">class</span>
<span class="java_plain">Example</span>Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.
- Follow existing code style and conventions
- Add unit tests for new features
- Ensure all tests pass:
mvn test - Update documentation as needed
This project is dual-licensed under:
- Common Development and Distribution License (CDDL) v1.0
- GNU Lesser General Public License (LGPL) v2.1 or later
You may choose either license to govern your use of this software.
- Original JHighlight project by Geert Bevin
- Maintained by CodeLibs Project
- Original project: https://jhighlight.dev.java.net/
- Maven Central: https://central.sonatype.com/artifact/org.codelibs/jhighlight
- GitHub Repository: https://github.com/codelibs/jhighlight
- Issue Tracker: https://github.com/codelibs/jhighlight/issues
- CodeLibs Project: https://www.codelibs.org/