Two java files, in the same directory, one accesses other classes / S?

I tried to grant one Java file to access another Classes in java files I want to do this on the command line For example, how do I do this using the following two files?

File: "toimport. Java"

package ABC;
public class ToImport {
    private String aName;
    public ToImport(String Name)  {
        aName = Name;
    }
    public String toString() {
        return("Text: " + aName);
    }
}

File: "theimport. Java"

package ABC;
public class TheImport {
        public static void main(String[] args) {
        ToImport abc = new ToImport("a");
        System.out.println("TEST: " + abc);
    }
}

When I type javac toimport Java, I have no errors, but when I enter javac theimport Java, I received the following error,

Solution

Theimport depends on the toimport class Therefore, when you compile theimport, the compiler must also compile toimport or access the compiled toimport class

Suppose you have a directory as shown below,

src
└── ABC
    ├── TheImport.java
    └── ToImport.java

Also, suppose you are in the SRC directory and want to compile to/ classes. You must use one of the following commands:

javac -d ../classes ABC/ToImport.java ABC/TheImport.java

or

javac -d ../classes ABC/ToImport.java
javac -cp ../classes -d ../classes ABC/TheImport.java

If two Java files are interdependent, so you must compile them at the same time as the first command

Also note that packages should be all lowercase to comply with Java Naming Conventions

To run the main program you can enter,

cd ../classes
java ABC.TheImport
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>