0

I have been trying to compile through CMD by creating packages manually. I tried the command javac -d . Nameinfo.java and javac -d . Returndata.java, where Nameinfo contains the calculations and Returndata contains main function, and this command worked absolutely fine. Now I made 2 different packages in my "java programmes" folder i.e packx and packy, where packx contains Nameinfo and packy contains Returndata. Now, when I compile these two, Nameinfo.java got compiled but Returndata could not read/recognize the imported Nameinfo in Returndata.java.

errors:

E:\java programmes>cd packx

E:\java programmes>cd packx

E:\java programmes\packx>javac Nameinfo.java

E:\java programmes\packx>cd..

E:\java programmes>cd packy

E:\java programmes\packy>javac Returndata.java
Returndata.java:2: error: package packx does not exist
import packx.Nameinfo;
            ^
Returndata.java:7: error: cannot find symbol
 Nameinfo data = new Nameinfo();
 ^
  symbol:   class Nameinfo
  location: class Returndata
Returndata.java:7: error: cannot find symbol
 Nameinfo data = new Nameinfo();
                     ^
  symbol:   class Nameinfo
  location: class Returndata
3 errors
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Please [edit] the post and show `Nameinfo.java` and `Returndata.java`. I have a suspicion that the `package ...;` statements are missing. – Turing85 Sep 18 '20 at 14:58

1 Answers1

0

if java or javac needs, say, packx.Returndata as a type to do something, it will scour a classpath and/or sourcepath for a directory named packx, and within that, Returndata.java or Returndata.class depending on the tool and path we're talking about.

So, you're in E:\java programmes\packy, and you're compiling some code that imports packx. Therefore, javac will look in the classpath for packx/Returndata.class and in the sourcepath for packx/Returndata.java and it doesn't find what you want because E:\java programmes isn't on the source path; generally they default to . (i.e. the current directory).

Stay in the E:\java programmes folder for all this work:

E:
cd "\Java programmes"
javac packx/Nameinfo.java
javac packy/Returndata.java

Or better yet, realize that manually compiling a multipackage java application is crazy. Use maven, gradle or some other build tool that takes care of all this for you :)

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72