macisamuele / language-formatters-pre-commit-hooks

Collection of custom pre-commit hooks.
Apache License 2.0
115 stars 58 forks source link

Java format hook does not work on Java 1.8 #171

Closed vitalca closed 1 year ago

vitalca commented 1 year ago

The --add-exports command line option was added in Java 9. When running on Java 1.8, you don't need to add these exports. Java fails to start.

Unfortunately, a lot of enterprise legacy code still uses JDK 1.8, This version is going to be supported till the end of 2030.

As a workaround I've added an alias to my ~/.bashrc, and I run gitc instead of git commit:

alias gitc='PATH=~/.sdkman/candidates/java/17/bin:$PATH git commit'
macisamuele commented 1 year ago

I'm not a bit Java expert and I'll try to document myself a bit more. In the meantime on the link you have referenced there is no trace of 1.8 version. Something worth mentioning is that recent versions of Google Java Formatter might not work without the add exports, which implies that you wouldn't be able to run them on your java version.

vitalca commented 1 year ago

Hi Samuele,

In the meantime on the link you have referenced there is no trace of 1.8 version.

Java 1.8 == Java 8, so it is mentioned over there.

Please look at the code in the pretty_format_java.py.

According to the google-java-format/README, the --add-exports JVM flags are required when running on JDK 16 and newer.

What needs to be done is the following. 1) check java version (there is a function for this) 2) for any versions >= 16, you add the --add-exports arguments. 3) for all versions below 16 you do not add them.

That's it. Thanks 🙂

vitalca commented 1 year ago

Google Formatter does not start on JDK 8. Sorry for bothering.

macisamuele commented 1 year ago

Thanks for the the report and carrying the check already.