I was researching how to do a quick java syntax-checker for usage in vim. I inspired by @Fidel's answer (thanks for the example checker!) but it didn't suffice me as I needed it to work in a standalone way.
Here is a step by step process of what had to be done in order to have runnable command:
# download and generate .java classes
wget https://repo1.maven.org/maven2/org/antlr/antlr4/4.5.3/antlr4-4.5.3.jar -O antlr4-4.5.3.jar
wget https://raw.githubusercontent.com/antlr/grammars-v4/master/java8/Java8.g4 -O Java8.g4
java -jar antlr4-4.5.3.jar ./Java8.g4
Then write a syntax checker, perhaps similar to @Fidel's one and place it under package checker;
place it directly under ./checker
directory
Also place all of the generated .java
classes under that package (ie. put package checker;
to the first line of all the files)
Run:
# prepare directory structure and compile
mkdir -p checker && mv *.java ./checker/
javac -cp antlr4-4.5.3.jar ./checker/*.java
- Prepare a Manifest file that will look similarly to:
Class-Path: antlr4-4.5.3.jar
Main-Class: checker.<name-of-your-checker-class>
# package into a jar file and run to test everything works
jar cfm checker.jar Manifest.txt checker/*.class
java -jar ./checker.jar <filename-you-want-to-run-syntax-check-against>
For my usage in vim I then created a simple shell script to wrap execution of the process of running the java jar that looks like this:
#!/bin/bash
DIR=$(readlink -f $0)
DIR=${DIR:0:(-3)} # put the length of the script name here instead of '3'
java -jar ${DIR}checker.jar $@
Then make it executable and symlink it under $PATH
:
chmod a+x <file-name>
sudo ln -s /path/to/the/script /usr/bin/java-syntax
And finally added a keybinding like this into a .vimrc
file (ie. to map running the syntax-check when F10 key is pressed):
" java syntax validation
map <F10> :!java-syntax % <CR>
I also stored the process into a github repository, where all the commands are prepared in a makefile and it suffices to run make
in order to build and package the checker. Feel free to use it as an inspiration.