Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Java Cloud function can't display non-ascii character

Have a encoding problem for java cloud function in GCP, non ascii character can not be displayed, e.g.

public void accept(PubSubMessage message, Context context) throws Exception {
String text = "最近一篇来自 Blogto 的文章指出“;
logger.info(text) ;
}
Will give me ??????Blogto?????
 
I don't have this problem in local. Options tried but neither works
1.  Add below to maven properties
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 
--set-build-env-vars=JAVA_TOOL_OPTIONS=-Dfile.encoding="UTF-8"
 
3. Add system variable to mvn build before gcloud deploy.
mvn -Dproject.build.sourceEncoding=UTF-8  ....
 
Please advise. Thank you!
0 1 528
1 REPLY 1

Hi @ZinY,

Welcome to Google Cloud Community!

The issue is that the Java cloud function is not displaying non-ASCII characters properly. The solution is to set the output encoding of the JVM explicitly to UTF-8 using the System.setProperty() method, or set the encoding explicitly when creating the logger. It's also recommended to check if the PubSubMessage object is correctly encoded.

Thanks