0

I am getting a compiler error when compiling Order.java file even when it contains an import statement for the other packaged class. Im not entirely sure why this is happening but here is a directory tree with some files that I have:

  • com/my/domain/Order.java
    Inside this file are the following package and imports:

    package domain;
    import utils.MyDate;
    
  • com/my/utils/MyDate.java
    Inside this file are the following package and imports:

    package utils;
    

Compiler error I get when compiling Order.java :

 Order.java:2: error: package com.my.utils does not exist
 import com.my.utils.MyDate;
               ^
 Order.java:5: error: cannot find symbol
  public MyDate orderDate;
       ^
  symbol:   class MyDate
  location: class Order

 Order.java:16: error: cannot find symbol
  public Order(MyDate d, double amt, String c, String p, int q){
             ^
 symbol:   class MyDate
 location: class Order

 Order.java:24: error: cannot find symbol
  public Order (MyDate d, double amt, String c) {
              ^
 symbol:   class MyDate
 location: class Order
4 errors

I am still unsure how to solve this after trying form the comments. Here is some more detail.

Existing Statements in .bash_profile :

 export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
 export CLASSPATH=${CLASSPATH}:/Users/3aCaGa/Desktop/Java-SE-8-Programs/SimplifiedDateClass/com/my

How I am trying to compile? I am going to the java file location in the directory and running command for example :

 java Order.java

For more detail on the files that and their exact contents see: https://github.com/gosem01/Java-SE-8-Programs/tree/master/SimplifiedDateClass/com/my

5
  • 1
    What is your classpath? Commented Jun 30, 2017 at 6:55
  • based upon com/my/utils/MyDate.java maybe import com.my.MyDate; Commented Jun 30, 2017 at 6:55
  • Here is my class path: export PATH=$PATH/Users/3aCaGa/Desktop/Java-SE-8-Programs/SimplifiedDateClass/com also changing it to import com.my.MyDate; got rid of the error but now I have the cannot find symbol error when I compile Order.java: Order.java:2: error: cannot find symbol import com.my.MyDate; Commented Jun 30, 2017 at 7:25
  • 1- that is not the CLASSPATH it is just the PATH; 2- it is missing the separator ':' between $PATH and /Users/... (assuming non-Windows) Commented Jun 30, 2017 at 8:08
  • and if that were the classpath, it is missing the my folder Commented Jun 30, 2017 at 8:16

2 Answers 2

2

Your package and import statements do not match your directory structure.

Your Order.class should have:

package com.my.domain;
import com.my.utils.MyDate;

and the Utils.class:

package com.my.utils;
Sign up to request clarification or add additional context in comments.

Comments

1

To compile go to the directory where you can "see" the com folder and do:

*nix/MacOS

javac -cp . com/my/domain/*.java com/my/utils/*.java

Windows

javac -cp . com\my\domain\*.java com\my\utils\*.java

Hope it helps

3 Comments

more like javac -cp com/my/ com/my/domain/*.java com/my/utils/*.java ... or cd com/my and javac -cp . domain/*.java utils/*.java
@P.J.Meisch it is not moved anywhere just specifying the classpath using cp argument you can verify it using man javac
you're right; I mistook the cp as a shell command; sorry, just removed my previous coment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.