I have a Java program that creates a list of filepaths I want to run checkstyle on. I was curious if there is a way to directly run checkstyle from within my program or whether I will need to shell out to the CLI.
-
heres a link that might help you : https://www.baeldung.com/checkstyle-java – Mustahsan May 01 '19 at 16:54
-
1@Mustahsan that appears to be about setting it up inside of an IDE or as part of a Maven build - I am interested in running it directly from within a Java program – Andrew May 01 '19 at 18:05
-
you can run terminal commands from java as well. – Mustahsan May 02 '19 at 03:07
-
1If you ask how to simply run a process with checkstyle, see https://stackoverflow.com/questions/792024/how-to-execute-system-commands-linux-bsd-using-java for example. If you are asking whether checkstyle has an API so you can run it programmatically, well, that would be off topic here. – GhostCat May 02 '19 at 07:06
3 Answers
Yes, that is possible, although it's not so much a documented API, but rather a set of calls that works, and that's kept reasonably stable. In fact, IDE plugins go that way.
For example, the Checkstyle plugin for IntelliJ has some code you can look at to get an idea:
https://github.com/jshiell/checkstyle-idea/tree/5.26.0/src/csaccess/java/org/infernus/idea/checkstyle/service/cmd
It may, however, be easier to just call Checkstyle as a command-line program (via zt-exec, for example) and parse its XML report. That is if you don't need to have the direct feedback provided via in-process AuditListeners.

- 16,865
- 10
- 85
- 132
I have a Java program
is a way to directly run checkstyle from within my program
You can call any Java program from within another Java program. When a Java program is called from the command line, it's main
method is called that passes in all the command line parameters that is not for the java
program itself. All you need to do in your Java program is call the same main
that the command line calls. For checkstyle this is com.puppycrawl.tools.checkstyle.Main
. See https://github.com/checkstyle/checkstyle/blob/bd7621fae3b1b887d46b8a678600db7e6d03185c/src/main/java/com/puppycrawl/tools/checkstyle/Main.java#L100 .
The downside to checkstyle however is it calls System.exit
when it is done so you will never return from your call. To prevent a System.exit
from terminating the JVM completely see Java: How to test methods that call System.exit()? for the SecurityManager example.
You can avoid all this System.exit
business but it would require you to duplicate a bunch of Checkstyle's code, which is also in the Main
class. See https://github.com/checkstyle/checkstyle/blob/bd7621fae3b1b887d46b8a678600db7e6d03185c/src/main/java/com/puppycrawl/tools/checkstyle/Main.java#L332 . It is up to you how you want to handle it.

- 2,081
- 15
- 25
See Invoking Checkstyle directly from Java, which points a user to the class com.puppycrawl.tools.checkstyle.api, which is the suggested way to interface with checkstyle programmatically.

- 6,576
- 9
- 50
- 101
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/34682372) – Abra Jul 13 '23 at 13:19
-
@Abra I added detail, per your request. The other answers heavily rely on links too, and I state the class that is recommended as the API. I understand from your downvote that you still don't like it, but I'll take the hit because I think it could be useful to other programmers in my position. – Ellen Spertus Jul 14 '23 at 20:03
-
For your information, I did **not** down-vote your answer. Your answer came up on the [low quality answers](https://stackoverflow.com/help/review-low-quality) queue and since it basically contains links only, I recommended it for deletion. I think you should post code that demonstrates how to use the API. – Abra Jul 15 '23 at 07:32
-
@Abra I apologize for incorrectly saying that you had downvoted it. I think the answer has value, so will leave it. I do not think writing code for a years' old post would be a good use of my time. The accepted answer does not have code either. Thank you for spending time reviewing content, even if I disagree with you in this case. – Ellen Spertus Jul 15 '23 at 21:00