Reputation: 21
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. ^^
Upvotes: 1
Views: 307
Reputation: 21
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
Upvotes: 1