1

I have created a desktop application in JAVA using NetBeans IDE. Application can write data to a file. I use Arabic characters in my application and When i Run the Application directly from NetBeans, I can write The Arabic characters to a file without any issue. But when i run the .jar file in dist folder outside NetBeans, it shows '????' instead of Arabic font. I will be grateful if some one can help me to find a solution.The screen shot is given below

This is my Custom Logger Class:

public class MLogger {

   private static final Logger log = Logger.getLogger( MLogger.class.getName() );
   private static   FileHandler fh; 
   private static SimpleFormatter sf;

   public static synchronized  void writeLogToFile(String message){
        try {
             fh   = new FileHandler("D://wblogs/wblog.txt", true);
              // true forces append mode
            sf= new SimpleFormatter();
            fh.setFormatter(sf);
            log.addHandler(fh);



            log.log( Level.INFO,message);


        } catch (IOException ex) {
            Logger.getLogger(MLogger.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(MLogger.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

and I use the method :

MLogger.writeLogToFile("محمد اقبال");
MUHAMMED IQBAL PA
  • 3,152
  • 2
  • 15
  • 23

1 Answers1

2

You have different encodings in use between running in NetBeans, and from commandline. A quick fix for your problem is to add

fh.setEncoding("UTF-8"); // and catch UnsupportedEncodingException

to the code you showed.

A better fix for your problem is:

  1. Don't use java.util.logging it's horrible, and nobody uses it
  2. Don't create "custom code" for logging. You don't need it. This is especially true if you switch to a modern logger like SLF4J/Logback.
Kayaman
  • 72,141
  • 5
  • 83
  • 121