Home>

There are files called Calc.java and CalcLogic.java.
Each code is written below.

package calcapp.main;
public class Calc {
    public static void main (String [] args) {
        int x = 10;int y = 2;
        int total = calcapp.logics.CalcLogic.tasu (x, y);
        int delta = calcapp.logics.CalcLogic.hiku (x, y);
        System.out.println ("add" + total + "subtract" + delta);
    }
}
package calcapp.logics;
public class CalcLogic {
    public static int tasu (int x, int y) {
        return x + y;
    }
    public static int hiku (int x, int y) {
        return x --y;
    }
}

Write javac Calc.java in this code? When trying to compile

Error: Package calcapp.logics does not exist
int total = calcapp.logics.CalcLogic.tasu (x, y);

Will come out.

I am doing this while reading the reference book, but it is stated that in this state an error will occur at runtime, but compilation is possible.
I don't understand why I can't compile.
By the way, these two source files are put in the same file called main.
So I open GitBash and compile it.

I think there are many strange points in the usage of words and strange points in the code, but please lend me your wisdom.

  • Answer # 1

    I am doing this while reading the reference book, but it is stated that in this state an error will occur at runtime, but compilation is possible.
    I don't understand why I can't compile.

    Did you specify the location of the * .java file in the reference book you are reading?

    At the beginning of each * .java filepackageIf specified, in the code in Calc.javacalcapp.logics.CalcLogic.tasu (x, y);From the place where there is a description like, it seems that it is assumed that the directory structure (file arrangement) is as follows.

    calcapp/logics/CalcLogic.java
    calcapp/main/Calc.java

    So, create a directory appropriately for each, place the * .java file in that location, andcalcappCompiling is completed by compiling from the directory one level above the directory. The following is not Git Bash, but an example of compiling from the command prompt. The compilation is successful and each * .class file is generated.

    C: \ projects>dir
     The volume label for drive C is HOGEFUGA
     Volume serial number is AAAA-FFFF
     C: \ projects directory
    2020/11/18 00:04<DIR>.
    2020/11/18 00:04<DIR>..
    2020/11/17 23:58<DIR>calcapp
                   0 files 0 bytes
                   3 directories 5,316,739,072 bytes of free space
    C: \ projects>javac calcapp \ main \ Calc.java
    C: \ projects>dir calcapp \ logics
     The volume label for drive C is HOGEFUGA
     Volume serial number is AAAA-FFFF
     Directory of C: \ projects \ calcapp \ logics
    2020/11/18 00:04<DIR>.
    2020/11/18 00:04<DIR>..
    2020/11/18 00:04 313 CalcLogic.class
    2020/11/17 23:55 202 CalcLogic.java
                   2 files 515 bytes
                   Two directories 5,314,043,904 bytes of free space
    C: \ projects>dir calcapp \ main
     The volume label for drive C is HOGEFUGA
     Volume serial number is AAAA-FFFF
     C: \ projects \ calcapp \ main directory
    2020/11/18 00:04<DIR>.
    2020/11/18 00:04<DIR>..
    2020/11/18 00:04 967 Calc.class
    2020/11/17 23:57 314 Calc.java
                   2 files 1,281 bytes
                   Two directories 5,314,043,904 bytes of free space
    C: \ projects>