12

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

Max
  • 2,036
  • 2
  • 18
  • 27
Evan Sevy
  • 659
  • 1
  • 13
  • 25
  • in what directory the classes are placed and how and from where you run java – jmj Mar 08 '13 at 21:45
  • 3
    Most likely you are calling `javac` from the directory in which these two files are located. They both need to be in a directory called "ABC" (the package name), and you'll need to call `javac ABC\TheImport.java` from the directory containing "ABC". That's my guess, anyway. – Reinstate Monica -- notmaynard Mar 08 '13 at 21:50
  • since both the files are in the same package and declared public , then you dont need to worry about calling , simply creating the object is enough , but the question is , as jigar has said , how are you compiling it – Hussain Akhtar Wahid 'Ghouri' Mar 08 '13 at 22:00

2 Answers2

11

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
Max
  • 2,036
  • 2
  • 18
  • 27
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Perfect answer. You could add external jars while compiling in the `-cp` itself and they should `:` separated. You'll also have to pass the jars while running the class file using `java` command. – backslashN Sep 02 '17 at 07:40
3

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.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • The compiler doesn't have to compile both classes at the same time. It can compile one and then compile the other one that depends on it using the `javac` parameter `-cp`, as in the answer to this question by JB Nizet. – Max Nov 10 '16 at 04:28