Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two java files, in same directory, one accessing the others class/s?

I am trying to grant one .java file access to the class in another .java file. I would like to do this on the command line. For example how would I do this using the two files below?

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 get no errors but when I type javac TheImport.java I get the following error,

Command Prompt Error Message

like image 242
Evan Sevy Avatar asked Mar 08 '13 21:03

Evan Sevy


People also ask

Can two Java classes be in the same file?

No, while defining multiple classes in a single Java file you need to make sure that only one class among them is public. If you have more than one public classes a single file a compile-time error will be generated.

How can we use two classes in Java?

You can use at most one public class per one java file (COMPILATION UNIT) and unlimited number of separate package-private classes. Compilation unit must named as public class is. You also can have in your public class the unlimited number of inner classes and static nested classes .

Why we Cannot declare multiple public classes in single .Java file?

So the reason behind keeping one public class per source file is to actually make the compilation process faster because it enables a more efficient lookup of source and compiled files during linking (import statements).


2 Answers

From the package containing the .java files run:

javac *.java

or

javac TheImport.java ToImport.java

The compiler needs to compile both classes at the same time, it cannot individually compile a single class with dependencies on another.

like image 22
Kevin Bowersox Avatar answered Nov 03 '22 11:11

Kevin Bowersox


TheImport depends on the class ToImport. So, when you compile TheImport the compiler must either also compile ToImport or have access to the already compiled ToImport class.

Let's say you have a directory that looks like the following,

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

In addition let's say you're in the directory src 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 both .java files depended on each other then you'd have to compile them both at once as in the first command.

Also note that packages should be all lowercase to respect the Java naming conventions.

To run the main program you could type,

cd ../classes
java ABC.TheImport
like image 132
JB Nizet Avatar answered Nov 03 '22 13:11

JB Nizet