dev.bandito
dev.bandito

Reputation: 33

Read encoded characters from socket not working when export to JAR

If i run as a java application in eclipse there is no problem but if i export as a runnable jar file the project i get encoding problem ?

if it runs from eclipse i can read the char "ş" that is coming from the server via socket. If it runs as a runnable jar i can't read the char "ş"

How can i fix this ?

EDIT

public class Start {

     public static void main(String[] args) {


               byte[] buff = new byte[1024];
               Socket socket = null;
               InputStream in = null;

               try {
                    socket = new Socket("11.11.11.11");
                    in = socket.getInputStream();

               } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
               }

               try {
                    in.read(buff);

               } catch (IOException e) {
                     e.printStackTrace();
               } 

               System.out.println(new String(buff));




    }

}

Upvotes: 2

Views: 97

Answers (1)

greg-449
greg-449

Reputation: 111217

new String(buff)

creates the string using the default encoding for the current platform. This may be different in Eclipse and outside of Eclipse.

Instead use

new String(buff, StandardCharsets.UTF_8)

to specify that the byte array should always be treated as UTF-8 (assuming it is UTF-8 that you are receiving)

Also don't forget to close your streams.

Upvotes: 2

Related Questions