Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javac "cannot find symbol" error with command line

Tags:

java

javac

I have two classes Owning and OwningAccessor. The files are in the same directory.

public class Owning {
    String _name = "";
    public void printBanner()
    {
    }
    public void printOwning(double amount)
    {
        printBanner();

        //print details
        System.out.println("name:" + _name);
        System.out.println("amount:" + amount);
    }
}


public class OwningAccessor {
    public void access()
    {
        Owning o = new Owning();
        o.printOwning(500);
    }
}

When I tried to compile OwningAccessor with javac -cp . OwningAccessor.java, I got compilation error.

symbol  : class Owning
location: class smcho.OwningAccessor
        Owning o = new Owning();
        ^
OwningAccessor.java:6: cannot find symbol
symbol  : class Owning
location: class smcho.OwningAccessor
        Owning o = new Owning();
                   ^

What's wrong with this? The code compiles fine under eclipse IDE.

like image 941
prosseek Avatar asked Nov 15 '12 23:11

prosseek


People also ask

Why is javac not working in CMD?

javac is not recognized is an error occurs while we compile the Java application. It is because the JVM is unable to find the javac.exe file. The javac.exe file is located in the bin folder of the JDK. The reason behind to occur the error is that the PATH is not added to the System's environment variable.

How do I get rid of Cannot find symbol error?

Output. In the above program, "Cannot find symbol" error will occur because “sum” is not declared. In order to solve the error, we need to define “int sum = n1+n2” before using the variable sum.

Why is Java saying Cannot find symbol?

Misspelled method name Misspelling an existing method, or any valid identifier, causes a cannot find symbol error. Java identifiers are case-sensitive, so any variation of an existing variable, method, class, interface, or package name will result in this error, as demonstrated in Fig. 3.

How do I fix javac file not found?

It means that javac.exe executable file, which exists in bin directory of JDK installation folder is not added to PATH environment variable. You need to add JAVA_HOME/bin folder in your machine's PATH to solve this error. You cannot compile and run Java program until your add Java into your system's PATH variable.


1 Answers

Make sure you compile both Owning.java and OwningAccessor.java, like so:

javac -cp . Owning.java OwningAccessor.java

Eclipse compiles all necessary files for you, which is why does work there.

like image 196
Kninnug Avatar answered Sep 22 '22 05:09

Kninnug