Deploying LanguageTool as a Java Servlet for Grammar Checking

This guide walks through cloning the LanguageTool repository, building it with Maven, extracting the required JARs, creating a servlet that uses JLanguageTool to check English text, handling language‑code issues by packaging META‑INF resources, and finally testing the service with curl.

Hujiang Technology
Hujiang Technology
Hujiang Technology
Deploying LanguageTool as a Java Servlet for Grammar Checking

Using LanguageTool for grammar and spelling checks is convenient; the following steps demonstrate how to deploy a LanguageTool servlet.

First, clone the source repository:

git clone [email protected]:languagetool/languagetool.git

Because the project is large, the clone may take a while. After cloning, compile it with Maven:

$ mvn clean test
$ ./build.sh languagetool-standalone -DskipTests

When the build finishes (typically 5–30 minutes), the target directory contains three main JAR files: languagetool.jar, languagetool-server.jar, and languagetool-commandline.jar, plus many supporting files.

You can verify the build by running a command‑line test:

$ echo "I was do book a tickt last night" | java -jar languagetool-commandline.jar -l en-US -m en

The intentionally incorrect sentence triggers a JSON response describing the grammar and spelling issues, e.g.:

{
  "software": {"name":"LanguageTool","version":"4.0-SNAPSHOT"},
  "matches":[{
    "message":"Consider using a past participle here: \"done\".",
    "shortMessage":"Possible agreement error",
    "offset":6,"length":2,
    "rule":{"id":"BEEN_PART_AGREEMENT","description":"Agreement: 'been' or 'was' + past tense"}
  }]
}

Next, create a servlet project and copy all files from the lib directory, together with the three main JARs, into WEB-INF/lib. Inside the servlet, add the following code to invoke LanguageTool:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    JLanguageTool lt = new JLanguageTool(Languages.getLanguageForShortCode("en-US"));
    List<RuleMatch> list = lt.check(request.getParameter("text"));
    StringBuilder s = new StringBuilder();
    for (RuleMatch rm : list) {
        s += String.format("message => %s, from => %d, to => %d<br>", rm.getMessage(), rm.getFromPos(), rm.getToPos());
    }
    response.setContentType("text/plain");
    PrintWriter pw = response.getWriter();
    pw.write(s.toString());
}

When running the servlet, an error may appear stating that 'en-US' is not a language code known to LanguageTool. The cause is that the META‑INF resources containing language definitions were not copied.

Inspect the compiled output: language classes reside under org/languagetool/language together with many .properties files. To make them available, package the META‑INF files into a separate JAR: $ zip -r languages.jar META-INF/org/ Copy languages.jar into WEB-INF/lib. After adding this JAR, the servlet works correctly. The servlet code can be simplified by using the concrete language class:

JLanguageTool lt = new JLanguageTool(new AmericanEnglish());

Finally, test the deployed servlet with curl:

$ curl -d "text=I was do book a tickt last night" http://localhost:8080/demo

The response contains messages such as "Consider using a past participle here: done " and "Possible spelling mistake found", confirming that the LanguageTool servlet is operational.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavamavenServletGrammarCheckLanguageTool
Hujiang Technology
Written by

Hujiang Technology

We focus on the real-world challenges developers face, delivering authentic, practical content and a direct platform for technical networking among developers.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.