-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add xlint and file.encoding to Java #2471
Conversation
update for issue #1166 |
lib/grammars/java.js
Outdated
@@ -8,7 +8,7 @@ function JavaArgs(filepath, context) { | |||
const className = GrammarUtils.Java.getClassName(context) | |||
const classPackages = GrammarUtils.Java.getClassPackage(context) | |||
const tempFolder = GrammarUtils.createTempFolder("jar-") | |||
const cmd = `javac -encoding UTF-8 -sourcepath '${sourcePath}' -d '${tempFolder}' '${filepath}' && java -D'file.encoding'='UTF-8' -cp '${tempFolder}' ${classPackages}${className}` | |||
const cmd = `javac -J-Dfile.encoding=UTF-8 -Xlint -sourcepath '${sourcePath}' -d '${tempFolder}' '${filepath}' && java -Dfile.encoding=UTF-8 -cp '${tempFolder}' ${classPackages}${className}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the docs -encoding UTF-8
is a valid way to set the encoding.
https://docs.oracle.com/en/java/javase/13/docs/specs/man/javac.html
https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html
What version of Java are you trying this on? I added a test and tested this on Windows, and I had no issues.
This is running examples/helloworld.java on master branch:
const cmd = `javac -J-Dfile.encoding=UTF-8 -Xlint -sourcepath '${sourcePath}' -d '${tempFolder}' '${filepath}' && java -Dfile.encoding=UTF-8 -cp '${tempFolder}' ${classPackages}${className}` | |
const cmd = `javac -encoding UTF-8 -Xlint -sourcepath '${sourcePath}' -d '${tempFolder}' '${filepath}' && java -Dfile.encoding=UTF-8 -cp '${tempFolder}' ${classPackages}${className}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"-encoding UTF-8" will only set source file encoding to UTF-8. Since javac.exe will produce its warning/error messages in its default encoding (for exapmle, in Japanese Windows, it's CP932/ShiftJIS) and in local language (such as Japanese) so it will become garbled. -J-Dfile.encoding=UTF-8 will switch whole output messages of javac.exe to UTF-8.
For example, if you implement java.io.Serializable in your main class and won't set serialversionUID, javac will output warning message, and with '-J-Dfile.encoding=UTF-8', it looks like this:
while only with "-encoding UTF-8" option, this would show:
for -J option, please check "Standard Options" section in https://docs.oracle.com/en/java/javase/11/tools/javac.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we set both?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not necessarily I suppose. '-J-Dfile.encoding=UTF-8' will do the job, and I am sure in my environment '-encoding UTF-8' is not there and the package works fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added it just in case. Could you update your branch and try it now so I can merge this?
* Adding back '-J-Dfile.encoding=UTF-8' for javac so that its warning messages are printed in UTF-8 as well as its source files are treated as UTF-8 encoding. * Removing '-encoding UTF-8' for javac since that is covered by -J-Dfile.encoding=UTF-8 option * Adding back -Xlint for reaporting detailed warning messages for compilers * removing quotation marks around file.encoding and UTF-8 for java command, since java command does not understand quotation marks when it is not removed by shell (cmd.exe won't interpret that) Co-Authored-By: weather-tracker <[email protected]>
Moving detailed explanations to next comment to shorten pull-request message. |
That's because of the commit message, not the pull request. But thank you! I will fix it once I want to merge this. |
okay. |
🎉 This PR is included in version 3.32.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Adding back UTF-8 encoding related options and -Xlint. Tested on Windows 10 and Ubuntu 21.04.