简体   繁体   中英

Illegal character error when compiling a program made using geany \u0000

I have been using Geany to create Java programs, where until now I was able to compile them successfully. The simple program created below in Java was made using Geany, however the illegal character error (\) occurred.

public class SumOfCubedDigits
{
    public static void main(String[] args)
    {
        for (int i=1; i<=9; i++)
        {
            for (int j=0; j<=9; j++)
            {
                for (int k=0; k<=9; k++)
                {
                    double iCubed=Math.pow(i,3);
                    double jCubed=Math.pow(j,3);
                    double kCubed=Math.pow(k,3);
                    double cubedDigits = iCubed + jCubed + kCubed;
                    int concatenatedDigits = (i*100 + j*10 + k);
                    if (cubedDigits==concatenatedDigits)
                    {
                        System.out.println(concatenatedDigits);
                    }
                }
            }
        }
    }
}

I recreated the program in nano and it was able to compile successfully. I then copied it across to Geany under a different name of SumTest.java, compiled it and got the same illegal character error. Clearly the error is with the Geany IDE for Raspberry Pi. I'd like to know how I could fix the editor to create and compile programs successfully as it not just this program, it is any program created in Java using Geany.

This might be a problem with encoding that Geany uses when saving the source file.

If you compile the file with javac without specifying the -encoding parameter the platform's default encoding is used. On a modern Linux this is likely to be UTF-8; on Windows it is one of the ANSI character sets or UTF-16, I think.

To find out what the default encoding is, you can compile and run a small java program:

public class DefaultCharsetPrinter {
    public static void main(String[] argv) {
        System.out.println(Charset.defaultCharset());
    }
}

This should print the name of the default encoding used by java programs.

In Geany you can set the file encoding in menu Document > Set Encoding . You need to set this to the same value used by javac . The Geany manual describes additional options for setting the encoding .

As you are seeing a lot errors complaining about the null character it is most likely that Geany stores the file in an encoding with multiple bytes per character (for instance UTF-16) while javac uses an encoding with a single byte per character. If I save your source file as UTF-16 and then try to compile it with javac using UTF-8 encoding, I get the same error messages that you see. After saving the file as UTF-8 in Geany, the file compiles without problems.

I had the same problem with a file i generated using the command echo echo "" > Main.java in Windows Powershell. I searched the problem and it seemed to have something to do with encoding. I checked the encoding of the file using file -i Main.java and the result was text/plain; charset=utf-16le .

Later i deleted the file and recreated it using git bash using touch Main.java and with this the file compiled successfully. I checked the file encoding using file -i command and this time the result was Main.java: text/xc; charset=us-ascii .

Next i searched the internet and found that to create an empty file using Powershell we can use the Cmdlet New-Item . I create the file using New-Item Main.java and checked it's encoding and this time the result was Main.java: text/xc; charset=us-ascii and this time it compiled successully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM