2

Is it possible to compile a jar file so that it already "remembers" to be run with UTF-8 encoding?

My JAR works correctly when it is run from CMD with specifying the encoding:

java -Dfile.encoding="UTF-8" -jar "C:\(...)\ZBook 14\Desktop\Server.jar"

I would rather have the file remembered the setting. I do not want to modyfy my default environmental JAVA_TOOL_OPTIONS.

Annabelle
  • 734
  • 9
  • 23
  • 3
    I would just explicitly specify the character set everywhere in your code. Anything that uses the system default is brittle IMO. – Jon Skeet Jan 07 '19 at 18:54
  • I already tried that but that is not a solution. I need a separate code for UTF-16 etc. – Annabelle Jan 07 '19 at 18:55
  • Consider editing your question and adding an example of code which is affected by this. – VGR Jan 07 '19 at 18:56
  • I am having troubles to set encoding to HermiT reasoner. But I also need it to my other jars to set more complex settings than only utf-8. All of them work fine from CMD, but I would prefere them to remember the prefered settings – Annabelle Jan 07 '19 at 19:00
  • If your file encoding needs to be passed as a parameter always then it may be better to add a script to execute java command or an `ini` file to accept encoding as a parameter to run your jar. – Acewin Jan 07 '19 at 19:10
  • Could you please write me full example of how to do it (how the JAR knows about ini file to start with)? This is problably the solution. – Annabelle Jan 07 '19 at 19:14
  • this may be OS dependent. you may need to write a .bat or .sh file to run the java command. Please check https://stackoverflow.com/questions/32804020/create-a-shell-script-to-run-a-java-program-on-linux and https://stackoverflow.com/questions/8938944/how-to-run-java-application-by-bat-file – Acewin Jan 07 '19 at 19:17
  • I see... :(, .bat file is not much better than CMD for me – Annabelle Jan 07 '19 at 19:17
  • basically you will have to combine 2 solution. Read this thread how to params from `.ini` https://stackoverflow.com/questions/6318809/how-do-i-grab-an-ini-value-within-a-shell-script – Acewin Jan 07 '19 at 19:21
  • 1
    if you are uncomfortable in writing a shell script then do this [run java command from a java program](https://stackoverflow.com/questions/12402547/how-to-execute-javac-and-java-commands-from-java-program) – Acewin Jan 07 '19 at 19:23
  • The last one is clever. In general it will be useful for me. – Annabelle Jan 07 '19 at 19:25
  • In manifest it is impossible to set encoding? – Annabelle Jan 07 '19 at 19:26
  • Well that is because there is no straight forward solution to passing params to java. We always have to tweek shell scripts to pass `JAVA_OPTS` and other params to java command. – Acewin Jan 07 '19 at 19:27
  • passing a manifest file is used by `maven` and `ant` – Acewin Jan 07 '19 at 19:29
  • Thank you very much for the discussion. I am still not sure how to solve my problem but you gave me a bunch of new ideas. – Annabelle Jan 07 '19 at 19:31
  • 1
    Note that you can also just call `System.setProperty` as the first thing you do in your `main` method, too... – Jon Skeet Jan 07 '19 at 21:00

0 Answers0