• Post Reply Bookmark Topic Watch Topic
  • New Topic

A public class in sub-folder is not getting found by another class while compilation  RSS feed

 
Ranch Hand
Posts: 88
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!!

I was doing coding exercise from a book ('OCP Java SE 6 - Practice Exams' by Kathy Sierra and Bert Bates). I came to a question that told to demonstrate the difference between 'default' and 'protected' access rules by creating/making a directory structure and putting a couple of classes in different packages.

For this, I made a total of four classes, out of which, three classes are-Car, TestingCars, CarDimensions. (The fourth is not yet used in testing code till now, so, I am giving only the other three classes.) Their coding is given below.

Out of these classes, the classes- TestingCars and Car - are in a directory (say, FolderName).
And, the class- CarDimensions is in FolderName's sub-folder.

The class 'CarDimensions' is public (and its components too are public). And, I am testing all the classes from the class- 'TestingCars'.
But, this class (TestingCars) is not able to find the public class- 'CarDimensions' which is in its sub-folder and gives two 'Cannot find symbol' errors citing the class-CarDimensions. Also, If all three classes are put in one single directory, the programs work, without any error.

Coding:
Class TestingCars:
Class Car:
The class below is from the sub-folder.
Class CarDimensions:
I could not find why the public class- CarDimensions- is not getting found by the TestingCars class. If you can tell me, it will be helpful for me.

Thank you!
 
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I suppose you need to set class path for the same as they are on different path that's why you getting error.
 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Ramteke wrote: and putting a couple of classes in different packages.

Do you have any package statements in your classes ? If you do, you need to show them for us to help you. If you don't, you need to add them.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… or put all the XXX.java files in the same directory.
 
Ashish Ramteke
Ranch Hand
Posts: 88
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Thanks you all for the replies- Tushar Goel, Stuart A. Burkett, and, Campbell Ritchie!!

Tushar Goel wrote:I suppose you need to set class path for the same as they are on different path ... .

Do you mean compiling the TestingCars class using the option '-classpath', like, >javac -classpath...? I tried this but I am not able to put the classpath locations correctly while compiling, and, because of that, the class in the same directory (Car.java) is not getting found. I think this is also one way in which these programs can be executed without getting any errors... I will see later about how to put the classpaths correctly.

I am using the directory structure as follows:
------------------------------
|
|
|->FolderName
-----L> NewFolder
-----L>CarDimensions.java
|
|-> Car.java
|-> TestingCars.java
------------------------------

Stuart A. Burkett wrote:
Do you have any package statements in your classes ? ... .

No, I did not put any package and import statements in the classes. After reading this, I put the appropriate package and import statements in the classes and there is no error now, and, the programs are working fine!

I thought that if a class is public and is in the sub-folder of the running class, then, there might not be any need of importing it by putting package and import statements in the classes- because the class is 'public'- but, maybe it is not like that, Right?

Thanks!!

Campbell Ritchie wrote:… or put all the XXX.java files in the same directory.

Yes, sir, if the classes are put in one single directory, the programs work fine without any errors. And, in that case, there is no need for putting package and import statements in the classes. The coding exercise that I was doing told to put the classes in different packages, so, I was trying to access the class from a different package.

Is it necessary to put import statements for accessing classes that are 'public' and in the sub-folder from where the current class is?

Thank you all!
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Ramteke wrote:Hi! Thanks you all for the replies- Tushar Goel, Stuart A. Burkett, and, Campbell Ritchie!! . . .
You're welcome
. . . Yes, sir, if the classes are put in one single directory, the programs work fine without any errors. . . . The coding exercise that I was doing told to put the classes in different packages, so, I was trying to access the class from a different package.

Is it necessary to put import statements for accessing classes that are 'public' and in the sub-folder from where the current class is? . . .
Yes.

If you are using the folder structure you showed earlier, I suggest:
  • 1: Run the application as you have it without package names.
  • 2: Empty the “L” folder.
  • 3: Delete the CarDimensions.class file from the outer folder.
  • 4: Move the CarDimensions.java file into the L folder and add the package name to the beginning of the file.
  • 5: Compile that file with javac L/CarDimensions.java
  • 6: Add the import declaration import L.CarDimensions; to the other classes.
  • 7: Compile the other two classes with javac XXX.java
  • I am assuming you are using the strange name L for your folder.
     
    Ashish Ramteke
    Ranch Hand
    Posts: 88
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry for the really late reply...
    Campbell Ritchie wrote:I am assuming you are using the strange name L for your folder.

    While showing the directory structure above, 'L' is not a name of a folder or anything..., I just used it to help represent the directory structure... I not at all mean 'L' there, they are just used with the '|', '-', and, '>' for representing the directory structure... It did not come to my mind while giving the directory structure previously that the 'L' I used can be thought of as a folder name there..

    Also, I found that I mistakenly gave a wrong directory structure above, sorry... The directory structure I wanted to represent is this:
    Directory-Structure.jpg
    [Thumbnail for Directory-Structure.jpg]
     
    Ashish Ramteke
    Ranch Hand
    Posts: 88
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi!!

    After just adding this 'package' statement:- in the class CarDimensions.java

    and this 'import' statement:-in the class TestingCars.java

    the program compiles and runs successfully.
    So, after just adding the two Java statements given above in the two appropriate classes, the program compiles and runs.

    Thanks for help!
     
    Campbell Ritchie
    Marshal
    Posts: 56610
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome

    The reason it compiles is that the folder structure and the package names now match.

    Please avoid coloured text, which some people find difficult to read; I have turned all your text back to black.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!