简体   繁体   中英

cannot find symbol error javac command

I am using terminal to compile and build java programs. So I have 2 classes CP1 and CP2 belong to package name current that are implemented in 2 separate .java files - CP1.java and CP2.java Although classes are trivial, here is the code

CP1.java :

package current;
public class CP1{
                  void hello(){
                               System.out.println("CP1 class is used");
                  }
   }

CP2.java :

package current;
public class CP2{
                  public static void main(String [] args){
                               System.out.println("CP2 class is used");
                               CP1 c= new CP1();
                  }
   }

As CP1 and CP2 are defined in the same package i do not import the current package. Both .java files are in the same directory. CP1.java compiles and the result is CP1.class file. Now I am trying to compile CP2.java with with -cp option as:

javac -cp . CP2.java

However, this results in the error message:

symbol: class CP1    
location: class CP2
CP2.java.  error: cannot find symbol 
                  CP1=new CP1

I tried various paths to cp but still get the same error message. What am I doing wrong?

you are missing the instance variable name so it should be this

CP1  objname = new CP1();

not this

CP1 = new CP1();

Note : objname can be replaced with any valid variable name so you can search online for naming guidelines/how to name a variable or can try Official doc link

Update : follow the below steps to compile

1.) Seems like you are at the location where the file exists so simply do this

javac -d . CP1.java

javac -d . CP2.java

-d will generate the package for you if it's not there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM