1

I'm using a .bat file for running a .jar program. The problem is that, even though the .bat file reads the input (aéí / lalélí / cácécí) it doesn't print them to the text file. ( Image: CMD console output ) And you can see here the text file with the missing accents: Image: Text file with missing accents

Why do I tell you this? Because the Eclipse console successfully PRINTS accents to the file, but the BAT file doesn't do it. And I want to share a program with some friends. You may ask: "What do you have on your bat file"? I only have this, for running the jar file:

java -jar crpg.jar

And, obviously, you would ask about my code, I'll post it right now, but hey! My code works on Eclipse, the accents are succesfully printed in a text file, like you can see here: Program runned in Eclipse console

package ficheros;
import java.io.*;
import java.nio.charset.*;
import java.util.Scanner;
public class QuestionCreator {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  OutputStreamWriter preguntas = null;
  PrintWriter pw = null;
  try(Scanner leer = new Scanner(System.in)){
   // Abrimos el fichero y creamos el BufferedReader para hacer una lectura cómoda
   preguntas = new OutputStreamWriter(new FileOutputStream("preguntas.txt"), Charset.forName("UTF-8"));
   pw = new PrintWriter(preguntas);
   // Preguntamos qué tema quiere seleccionar el usuario
   System.out.println("¿Qué tema quieres? ");
   String tema = leer.nextLine();
   String pregunta, respuesta, trivial;
   boolean salir = false;
   // Hacemos un bucle de creación de preguntas
   while(!salir){
    System.out.println("Si quieres cambiar tema escribe \"Cambiar tema\". Si quieres salir, escribe \"Salir\"");
    System.out.println("Escribe pregunta: ");
    pregunta = leer.nextLine();
    if(pregunta.equals("Cambiar tema") | pregunta.equals("cambiar tema")){
     // En el caso de que el usuario quiera cambiar de tema.
     System.out.println("Escribe tema: ");
     tema = leer.nextLine();
     System.out.println("Escribe pregunta: ");
     pregunta = leer.nextLine();
    } else if(pregunta.equals("Salir") | pregunta.equals("salir")){
     // En el caso de que el usuario quiera salir
     break;
    }
    System.out.println("Escribe respuesta: ");
    respuesta = leer.nextLine();
    // Se crea la pregunta y se introduce en una nueva línea en el documento.
    trivial = "{\"Category\":\"" + tema + "\",\"Question\":\"" + pregunta + "\",\"Answer\":\"" + respuesta + "\"},"; 
    pw.println(trivial);
   }
   } catch(Exception e){
    e.printStackTrace();
   } finally{
    // Cerramos el fichero
    try{
     // Aprovechamos el finally para
     // asegurarnos de que se cierra el fichero
     if(null != pw){
      pw.close();
      System.out.println("PROGRAMA FINALIZADO");
     }
    } catch(Exception e2){
     e2.printStackTrace();
    }
  }
 }
}

Sorry if my code is in Spanish. It's made for spanish friends. But I don't REALLY think my code is the problem, because, how can it work on Eclipse console and not on the .bat / .jar file? I would want to share my program with friends, but it's impossible if the .bat file doesn't print accents to the text file! What could be the problem? Why it doesn't work out of Eclipse? Thank you in advance. ^^

Glevion
  • 21
  • 3
  • How are you generating you jar file? Your problem is your character encoding. Your Eclipse is configured to your language but when you run it through you OS it probably get that configuration from the JVM, read this: http://tutorials.jenkov.com/java-io/outputstreamwriter.html and you will solve your problem. You probably want to use `ISO-Latin1` – Jorge Campos Nov 05 '17 at 13:02
  • 1
    Take a look on answer on [Batch file with non-ASCII characters](https://stackoverflow.com/a/36183982/3074564), and on the articles [Working with Unicode](https://www.ultraedit.com/support/tutorials-power-tips/ultraedit/unicode.html), [On the Goodness of Unicode](http://www.tbray.org/ongoing/When/200x/2003/04/06/Unicode) and [The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)](http://www.joelonsoftware.com/articles/Unicode.html) – Mofi Nov 05 '17 at 13:05
  • The created file is a UTF-8 file without BOM, but Notepad does not read it as UTF-8. I don't know, but maybe Notepad would read the file correctly with a BOM: https://stackoverflow.com/questions/4389005/how-to-add-a-utf-8-bom-in-java – howlger Nov 05 '17 at 13:16
  • @JorgeCampos I have in my code " preguntas = new OutputStreamWriter(new FileOutputStream("preguntas.txt"), Charset.forName("UTF-8")); " which is not ISO-Latin1, I tried that character encoding but it still doesn't work out of Eclipse u_u – Glevion Nov 05 '17 at 22:20
  • @Mofi Thank you so much about the information, I understood the concept. But I still don't know what else to do in the code. In my code, I have set the "OutputStreamWriter" working with the character encoding UTF-8 . What I have done wrong? I think it's fine, isn't it? I'm using the encoding UTF-8 which is supposed to recognize accents and print them in the text file. I'm blank right now :/ – Glevion Nov 05 '17 at 22:23
  • I have no knowledge about Java programming, just C and C++ and batch file programming. I can only suggest to find out which encoding is used for Java source file, how to output the strings in that encoding to console, and how to configure Eclipse to interpret the captured output with same encoding. I'm not using Eclipse, but I think the encoding for captured output can be configured somewhere in Eclipse for each application executed by Eclipse. It is impossible for Eclipse to find out automatically which encoding the application used on writing strings to handles __stdout__ and __stderr__. – Mofi Nov 06 '17 at 06:03
  • On the command line (`cmd.exe`) for the correct output, the correct codepage must be set. See the differences of `Chcp 850 && java -jar crpg.jar` vs. `Chcp 866 && java -jar crpg.jar`. But this is not related to the file encoding. – howlger Nov 06 '17 at 16:36

1 Answers1

1

I don't know if you're notified by this message, but I solved the problem.

All I had to do was adding "chcp 1252" in the .bat file , which is the European Latin codepage. I had on Windows, by default, the codepage "850", which is the Multilingual (Latin 1) (I don't really know why this last codepage prints accents on the CMD but not on a text file :S)

It's a shame that this small thing was the solution because I think it was something harder. But thank you all for your help! I appreciate it ^^

Greetings! :P

Glevion
  • 21
  • 3