0

I wrote a program for calling Unicode from outside the program. I am using Windows XP and Eclipse. When I run that program in the IDE, it shows the Unicode, but when I exported it as a .jar file I am unable read the Unicode and it shows as boxes.

I followed these instructions to install Unicode in my computer. I followed links to install the Telugu fonts to my system.

Can any one please tell me how can I get Unicode in Jar files?

dda
  • 6,030
  • 2
  • 25
  • 34
user18573
  • 1
  • 2
  • 3
    You've provided a lot of code, most of which is almost certainly irrelevant. Please provide a short but *complete* program which demonstrates the problem. Oh, and you almost certainly shouldn't use FileReader - use an InputStreamReader wrapped round a FileInputStream, as then you can specify the encoding. – Jon Skeet Jul 04 '13 at 10:04
  • I think that I have to add JRE Library to Jar. Can any one please send me how can I add JRE Libraries to Jar file. – user18573 Jul 04 '13 at 10:41
  • 4
    I very much doubt it. You seem to be confused between "Unicode" and "one specific font". – Jon Skeet Jul 04 '13 at 10:43
  • I can add strings it into my mother tongue. I can able to see those in IDE but not in Jar. I called those strings are from out side of the program i.e through file (.txt file) – user18573 Jul 04 '13 at 12:24
  • మర్డర్,రేప్,తెఫ్ట్,క్రిమినల్ బ్రీచ్ అఫ్ ట్రస్ట్ These are the strings which I read from text file – user18573 Jul 05 '13 at 05:01
  • 1
    Please include relevant code - but *only* relevant code - for how you're reading and displaying the text. Ideally, write a short but complete program which does *nothing* but read the text from the file, and display it. Then we can try to reproduce the problem. – Jon Skeet Jul 05 '13 at 05:57
  • @user18573 - If you don't do what Jon is suggesting, it is unlikely we'll be able to understand what your problem is. If we can't understand what your problem is, we won't be able to help you. (And don't just ask essentially the same Question again. People will notice and your Question it will get closed.) – Stephen C Jul 05 '13 at 06:47
  • I don't have any problem with Code. Because I can get Unicode in IDE. But I am unable to get Unicode in Jar file. Hope all can understand my problem. – user18573 Jul 05 '13 at 07:34

2 Answers2

2

While not answering the question directly, here is a small howto on how to read/write correctly from text files, in an OS dependent way.

First thing to know is that the JVM has a file.encoding property. It defines the default encoding used for all file read/write operation, all readers used when not specifying an encoding.

As such, you don't want to use the default constructors, but define the encoding each time. In Java, the class which "embodies" an encoding is Charset. If you want UTF-8, you will use:

  • StandardCharsets.UTF_8 (Java 7+),
  • Charset.forName("UTF-8") (Java 6-),
  • Charsets.UTF_8 (if you use Guava).

In order to read a file correctly, open an InputStream to that file, then an InputStreamReader over that InputStream (in the code samples below, UTF8 is the UTF-8 charset obtained from one of the methods above):

final InputStream in = new FileInputStream(...);
final Reader reader = new InputStreamReader(in, UTF8);

In order to write a file correctly, open an OutputStream to it, then an OutputStreamWriter over that OutputStream:

final OutputStream out = new FileOutputStream(...);
final Writer writer = new OutputStreamWriter(out, UTF8);

And, of course, do not forget to .close() both of the streams/readers/writers in a finally block. Hint: if you don't use Java 7, use Guava 14.0+, use Closer. It is the most secure way to deal with multiple I/O resources and ensuring they are dealt with correctly.

fge
  • 119,121
  • 33
  • 254
  • 329
  • I'm receiving this error message: "incompatible types: InputStreamReader cannot be converted to Reader" How do I fix this? Thanks in advance – WVrock Jun 08 '15 at 06:17
  • I've fixed it. I'm sharing, in-case someone needs it: Instead of using writer and reader I've used BufferedReader and BufferedWriter: `BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, UTF8));` do the same for the reader. Also I think doing exactly what this post says is impossible :http://stackoverflow.com/questions/30702904/might-not-have-been-initialized-error-at-null-check. Also this is how you declare UTF8: `Charset UTF8 = StandardCharsets.UTF_8;` – WVrock Jun 08 '15 at 16:33
0

You did the code already (I didn't follow the links), but you may compare the code with How to import a font - registerFont is crucial.

Also in a jar file all paths are case-sensitive. You may inspect the jar with 7zip or WinZip.

Community
  • 1
  • 1
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138