Running a file formatted in UTF-8 with javac

1

I tried to run a .java file that I did in Eclipse , and I missed a lot of errors. The file I programmed is encoded in UTF-8.

When executing this command in the CMD of Windows :

javac C:\ProgramasJava\Main.java

The output in console were the following errors:

C:\ProgramasJava\Main.java:1: error: illegal character: '\u00bb'
´╗┐package programa;
 ^
C:\ProgramasJava\Main.java:1: error: illegal character: '\u00bf'
´╗┐package programa;
  ^
C:\ProgramasJava\Main.java:154: error: illegal character: '\u00b3'
        private static void secci├│n(
                                  ^
C:\ProgramasJava\Main.java:154: error: invalid method declaration; return type required
        private static void secci├│n(
                                   ^
C:\ProgramasJava\Main.java:186: error: illegal character: '\u00b3'
                                        secci├│n(matriz);
                                              ^
C:\ProgramasJava\Main.java:186: error: not a statement
                                        secci├│n(matriz);
                                        ^
C:\ProgramasJava\Main.java:188: error: illegal character: '\u00b3'
                                        secci├│n(matriz);
                                              ^
C:\ProgramasJava\Main.java:188: error: not a statement
                                        secci├│n(matriz);
                                        ^
C:\ProgramasJava\Main.java:190: error: illegal character: '\u00b3'
                                        secci├│n(matriz);
                                              ^
C:\ProgramasJava\Main.java:190: error: not a statement
                                        secci├│n(matriz);
                                        ^
10 errors

Is it possible to run the .java file with that encoding?

Also, if I try to run the program with java instead of javac :

java C:\ProgramasJava\Main

I missed another error:

  

Error: Could not find or load main class C: \ ProgramsJava \ Main

Clarification: It's the first time I try to run a program from the console and not from Eclipse. If it's not annoying, I'd like to know how I can run what I programmed.

    
asked by ArtEze 09.10.2017 в 14:25
source

1 answer

3

By default, javac uses the encoding that the operating system has by default. In windows 10 it is already utf8, but in previous versions it uses its own system. You can declare the encoding to use with -encoding :

javac -encoding "UTF-8" C:\ProgramasJava\Main.java

If the compilation fails, the Main.class file will not be created, so it's normal for java Main to fail you

Note : if it still fails you, it is possible that the file is in UTF-8 format but in the "wrong" sense or use BOM , which in UTF-8 is unnecessary but some editors like Notepad usually add.

Edit after comment: / FEFF is the BOM UTF-16 Big Endian , check if you really are not using UTF-16.

    
answered by 09.10.2017 / 15:17
source