• Post Reply Bookmark Topic Watch Topic
  • New Topic

package Parent does not exist  RSS feed

 
Aakash Nigam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a package Parent. Within this Parent package I have a package Child. Within this Child package I have a class file C.class.

I have another package Root. Within Root I have a file D.java that contains class D which imports Parent.*;

On compiling D.java, I get an error, Package Parent does not exist.

Note that the package Parent does not have any class file within it. The class file C.class is present within Child package (which itself is present witin Parent package)

Please find the attached source code files.

Please let me know the reason behind this issue. Is the package Parent not identified since it does not have any class files within it ?

Find the code below :
-----------------------------------------
C.java  // compiled successfully as javac -d . C.java


-------------------------------------
D.java  // Compilation failed



Thanks,
Akash
package-not-found.PNG
[Thumbnail for package-not-found.PNG]
Compilation error
 
Henry Wong
author
Sheriff
Posts: 23028
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aakash Nigam wrote:
Please let me know the reason behind this issue. Is the package Parent not identified since it does not have any class files within it ?


Interestingly, I looked into something similar only a few days ago. And basically, from that investigation ... First, there is nothing in the Java Language Specification that states that it is a compile error to do an import on demand of a package that doesn't contain any classes. And Second, when I tested it with Eclipse, all I got was a warning stating that the import is never used.

Now, I didn't test it with javac, but... Since it is silly to import on demand of an empty package, I am not surprised if the implementation is a bit inconsistent, between compilers. After all, there is no reason to ever do this, so hence, it rarely ever happens.

Henry
 
Dave Tolls
Ranch Hand
Posts: 2560
27
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a CLASSPATH defined?
If so then that's your likely problem.

I notice your source (.java) files are not in the correct folder structure, though the .class ones will be (./Parent for example), which leaves the classpath as the likely culprit.
 
Henry Wong
author
Sheriff
Posts: 23028
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
I notice your source (.java) files are not in the correct folder structure, though the .class ones will be (./Parent for example), which leaves the classpath as the likely culprit.


Good catch. In my case, my directory structure was obviously correct (as it was managed by Eclipse), which may explain why it worked for me.

Henry
 
Aakash Nigam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry and Dave for replying to this thread. I still did not understand what the issue is.

On my system, both C.java and D.java are present at the path : C:\Users\Akash\Desktop\OCAJP\Programs\Chapter1.

I am compiling these files from the command line. At first when I open command line, I run the command : set "PATH=%PATH%;C:\Program Files\Java\jdk1.8.0_101\bin;C:\Windows\System32" after which I compile my files as :

javac -d . C.java

javac -d . D.java


Thanks,
Akash
 
Henry Wong
author
Sheriff
Posts: 23028
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aakash Nigam wrote:On my system, both C.java and D.java are present at the path : C:\Users\Akash\Desktop\OCAJP\Programs\Chapter1.

You can't just place source/class files anywhere. There is a directory structure (that is related to packages) that should be followed. Anyway, here is a link to the Oracle Tutorial related to this...

http://docs.oracle.com/javase/tutorial/java/package/managingfiles.html

Aakash Nigam wrote:
I am compiling these files from the command line. At first when I open command line, I run the command : set "PATH=%PATH%;C:\Program Files\Java\jdk1.8.0_101\bin;C:\Windows\System32" after which I compile my files as :

We are talking about classpath. This is not the same as the OS path variable. You need to understand that too. Here is a link to the Oracle Tutorial on classpaths (and how it is different from paths)...

https://docs.oracle.com/javase/tutorial/essential/environment/paths.html

Henry
 
Paul Anilprem
Enthuware Software Support
Ranch Foreman
Posts: 3991
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aakash, All you need to do is include the -classpath option while you are compiling your code. Like so:

javac -classpath . -d . *.java


Since you are using -d . that means, the compiler will create appropriate directory structure in . (dot i.e. current directory) and then place the class file inside those directories as per their package names.

The -classpath . option will tell the compiler where to look for those packages that are being used in other java files.


There is no restriction on where you have to keep the .java files. You can keep them anywhere. The issue is only about the location of the class files. You can tell the location of your packages to the compiler or the jvm either explicitly using the -classpath option or implicitly by setting the CLASSPATH environment variable.
 
Paul Anilprem
Enthuware Software Support
Ranch Foreman
Posts: 3991
32
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, the env variable name classpath is actually a misnomer.  It is not used to specify the path of the class files.  It is used to specify the path of packages. It should have been called packagepath, imho. Once you look at it that way, you will never have a confusion about where to put your class files or how to find them.
 
Roel De Nijs
Sheriff
Posts: 11095
173
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aakash Nigam wrote:Please let me know the reason behind this issue. Is the package Parent not identified since it does not have any class files within it ?

In this topic and this one provide an excellent explanation about how to compile Java source code files in different folders using a similar example. I'm pretty sure after carefully reading this topic all your doubts (concerning this topic) will be cleared. If you still have doubts/questions after reading these topics, simply click on the "Post reply" button and let us know

Hope it helps!
Kind regards,
Roel
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!