Irfan Ansari
Irfan Ansari

Reputation: 83

'cannot find symbol' compilation error when compiling a class of a package

I created a directory named mypack and added a Java file inside that directory named as A.java . The code for A.java is as follows:

package mypack;

public class A{
    public A(){
        System.out.println("Inside A");
    }
}

And I added another file in the same directory named Demo.java . The code for this file is as follows:

package mypack;

public class Demo {
    public static void main(String args[]) {
        A a = new A();
    }
}

The problem is when I compile the second file Demo.java I get error as such : cannot find symbol A

Unknown symbol A

the directory mypack looks like this :

Directory structure

I don't know why Demo.java the file of the same package (mypack) as that of A.java can't access class A even when I declared it as public in A.java. Please somebody help!!

Upvotes: 0

Views: 1112

Answers (3)

James
James

Reputation: 16

Maybe you can use

javac *.java

to complie two java source files at once.

Upvotes: 0

N00b Pr0grammer
N00b Pr0grammer

Reputation: 4647

You could do something like this to compile the files that are needed, just as the case you have mentioned in your question:

javac Demo.java A.java

This compiles your class along with the dependency A.java in this case.

Or you could do something like this, to cover all the .java files that you have under your directory.

javac *.java

Please refer to this for further reference!

Upvotes: 0

nimour pristou
nimour pristou

Reputation: 153

To compile the file, open your terminal and type

javac filename.java

To run the generated class file, use

java filename

Upvotes: 0

Related Questions