About Javac commands

About the javac command, which is a Java compiler command

This command that I use casually. Sometimes, there are times when you can't compile simply when specifying the file encoding or when specifying a package, so I'll summarize it as a reminder.

About the execution environment

The contents described below are executed in the following environment. OS: macOS Mojave (version 10.14.4) javac command: version 1.8.0_151

How to use the javac command Execution example 1

First of all, about the straightforward usage, when compiling the sample source written in Java as follows.

Sample.java


public class Sample {
    public static void main(String[] args) {
        System.out.println("Hello, world!!");
    }
}

Just specify the above file as an argument of the javac command and execute it as shown below. If there are no errors and the compilation is successful, a prompt will be displayed.

Execution example 1


$ javac Sample.java

By the way, if the Java syntax is incorrect, the following error will occur (when the void spelling of the return value of the main function is misspelled). The message is surprisingly kind, and if you read it properly, you will understand why the error is occurring.

Execution example 1(Compile error)


$ javac Sample.java
Sample.java:2:error:Can't find symbol
    public static viod main(String[] args) {
                  ^
symbol:Class void
place:Class Sample
1 error

How to use the javac command Execution example 2

Next is the case where the character encoding of the source file in which the Java code is written is different from the character encoding of the system of the client environment. For example, if it is Windows, it becomes an error, so basically SJIS, for example, save the file that describes or Hello to the source in the Japanese in UTF-8 to compile.

Pre-check to show an example of the error. First of all, the default environment? Get the file encoding. Create the following source and execute it after compilation. When executed, the file encoding will be output to standard output. This time, it was output as ʻUTF-8`.

EncodingCheck.java


public class EncodingCheck {
    public static void main(String[] args) {
        System.out.println(System.getProperty("file.encoding"));
    }
}

Next, save the source file with the following contents in the character encoding SJIS and compile it. This is set to SJIS in order to make it different from the file encoding examined in advance above.

Sample2.java


public class Sample2 {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

The execution result is as follows. A compilation error occurred. Looking at the content of the error, it was an error because a character code that could not be displayed normally in UTF8 was used. In this case, you can specify the character encoding with the option of the javac command (in this case, of course, you can compile normally by saving the file again in UTF8). (I don't know why 7 errors are displayed. I want to check it even when I feel like it)

Execution example 2 (compile error)


$ javac Sample2.java
Sample2.java:3:error:This character cannot be mapped to encoding UTF8
        System.out.println("����ɂ���");
                            ^
Sample2.java:3:error:This character cannot be mapped to encoding UTF8
        System.out.println("����ɂ���");
                             ^
Sample2.java:3:error:This character cannot be mapped to encoding UTF8
        System.out.println("����ɂ���");
                              ^
Sample2.java:3:error:This character cannot be mapped to encoding UTF8
        System.out.println("����ɂ���");
                               ^
Sample2.java:3:error:This character cannot be mapped to encoding UTF8
        System.out.println("����ɂ���");
                                 ^
Sample2.java:3:error:This character cannot be mapped to encoding UTF8
        System.out.println("����ɂ���");
                                  ^
Sample2.java:3:error:This character cannot be mapped to encoding UTF8
        System.out.println("����ɂ���");
                                   ^
7 errors

As shown below, if you specify the file character encoding SJIS in the argument of the ʻencoding option and execute it, a compile error will not occur (By the way, the ʻencoding option is described after the file name. No problem).

Execution example 2


$ javac -encoding sjis Sample2.java

Afterword

There are still other things I would like to confirm, but even if I write more, it will be too much, so it's over. Later, I'd like to check the classpath and source path as well.

Recommended Posts

About Javac commands
About qiita_org commands
About Java commands
About method.invoke
About Kotlin
About attr_accessor
About Hinemos
About inheritance
About params
About Docker
About form_for
About Spring ③
About polymorphism
About Optional
About hashes
About JitPack
About Dockerfile
About this ()
About devise
About encapsulation
About Docker
About JAVA_HOME
About static
About exceptions
About scope
[Maven] About Maven