将编码开关传递给Gradle的Java虚拟机“JavaExec”任务 [英] Passing an encoding switch to the JVM for a Gradle "JavaExec" task
问题描述
我在这里有几个编码问题。
这是在W10操作系统上,但我使用DOS控制台和Cygwin shell直接运行任务。在Eclipse中,当我使用Buildship插件运行时,对于我的生产输出,我制作了一个胖jar(即包含所有依赖jar)。
>为了让fat jar在DOS控制台中运行, chcp 65001
> java -jar -Dfile.encoding = UTF-8 myFatJar.jar
让fat jar运行在Cygwin控制台中,我省略了chcp命令
...并且这些都运行良好,没有编码probs。
我有一个 run
任务,如下所示:
任务myRun(类型:JavaExec,dependsOn:assemble){
standardInput = System.in
classpath sourceSets.main.runtimeClasspath
main =core.ConsoleHandler
}
当我在Eclipse(Buildship插件)中运行它时,没有编码probs。
在gradle目录中,我这样做:
> gradle myRun
无论我使用DOS还是Cygwin控制台,都会遇到编码问题:Unicode字符问题去 System.out
。
在我的gradle.build(我正在使用 $ b $ pre $
applicationDefaultJvmArgs = [-Dfile.encoding = UTF- 8]
...但这似乎没有任何效果。
顺便说一下,在测试期间,我有一些输出到 System.out
...,当我检查JUnit / Mockito测试结果。
在我追求我所有的鸭子连续的过程中,我也尝试过使用Java代码本身:而不是使用 System.out
直接试过:
PrintStream out = new PrintStream(System。 out,true,UTF-8);
out.print(message);
...但是这导致编码问题输出到 System.out
在运行测试时!
gradle命令 applicationDefaultJvmArgs
让我想知道是否有在运行 JavaExec
类型任务时将非默认开关传递给JVM ...
稍后有点
哈...... ..这就是这样的事情:
我将以下行添加到我的 myRun
任务中:
jvmArgs-Dfile.encoding = UTF-8
...和DOS和Cygwin控制台运行的编码问题已解决!但是,当我在Eclipse Buildship中运行 myRun
任务时,我现在遇到了编码问题!:
法官坐在钱伯斯处理紧急
问题
?tat juge de mise en〜:问题的合并法官
'Ã''应该是'é'(并且在添加这个 jvmArgs
参数)
总体而言,这是一个进步......但从专家那里知道这里发生了什么很好......
p>有时候 做 是有意义的,尽管我仍然发现编码一个令人痛心的,令人生畏的,破坏灵魂的主题。
我的Eclipse针对myRun的运行配置缺少这种JVM参数。添加它很简单:运行 - >运行配置 - >选择myRun - >参数选项卡 - > JVM参数。
我的所有鸭子看起来像在一个UTF-8行。 Hurray。
I am having a few encoding problems here.
This is on a W10 OS but I'm using both the DOS console and the Cygwin shell to run tasks directly. When in Eclipse I'm running using the Buildship plugin, and for my "production" output I'm making a "fat" jar (i.e. containing all the dependency jars).
To get the fat jar to run in a DOS console I go like this:
> chcp 65001
> java -jar -Dfile.encoding=UTF-8 myFatJar.jar
To get the fat jar to run in a Cygwin console I omit the "chcp" command
... and both of these run fine, no encoding probs.
I have a run
task that looks like this:
task myRun(type: JavaExec, dependsOn: assemble ) {
standardInput = System.in
classpath sourceSets.main.runtimeClasspath
main = "core.ConsoleHandler"
}
When I run this in Eclipse (Buildship plugin) there are no encoding probs.
When, in the gradle dir, I go like this:
> gradle myRun
I get encoding problems whether I'm using a DOS or Cygwin console: problems with Unicode chars going to System.out
.
In my gradle.build (I'm using the application
plugin) I tried this:
applicationDefaultJvmArgs = ["-Dfile.encoding=UTF-8"]
... but this doesn't seem to have any effect.
Incidentally, during testing, I have some output going to System.out
... and this comes out fine when I examine the JUnit/Mockito test results.
In my quest to get "all my ducks in a row" I also tried messing with the Java code itself: instead of using System.out
directly I tried this:
PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.print( message );
... but this then caused encoding problems with output to System.out
when running tests!
The gradle command applicationDefaultJvmArgs
makes me wonder if there is a way of passing "non-default" switches to the JVM when runing a JavaExec
-type task...
ever so slightly later
Hah.. such is the way with these things:
I added the following line to my myRun
task:
jvmArgs "-Dfile.encoding=UTF-8"
... and the encoding problem with the DOS and Cygwin console runs was... solved! However, when I run the myRun
task in Eclipse Buildship I now get encoding issues!:
référé juge des ~s: "Judge sitting in Chambers to deal with urgent matters"
état juge de mise en ~: joinder of issue judge
'é' should be 'é' (and was before adding this jvmArgs
argument)
On the whole of course this is an improvement... but it'd be nice to know from an expert what's going on here...
Sometimes things do make sense... though I still find encoding a harrowing, intimidating and soul-destroying subject.
My Eclipse "run configuration" for myRun was lacking this JVM argument. Adding it was simple enough: Run --> Run configurations --> choose "myRun" --> Arguments tab --> JVM Arguments.
All my ducks appear to be in a UTF-8 row. Hurrah.
这篇关于将编码开关传递给Gradle的Java虚拟机“JavaExec”任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!