Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

packages - am i an idiot  RSS feed

 
Mario Skrlec
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everybody,

ive googled and youtubed, checked tutorials, used corejava but i cant get the packages to cooperate.

This is the deal.

My root class is called Test.java. The only package that she needs is the Automate package. Now, the Automate package...

The Automate package (that contains Automate class) needs two imports... the Resource class in its separate package and the Building class
in its separate package. Also, the Resource needs Building import.

Now... these are the directories. The Test folder is the root folder. It has the Test class with the main() method. In the Test folder is the
Automate folder with the Automate class. Since the Automate class need the Building and Resource packages, I've putted those two into separate
folders named Resources and Building. Also, since the Resource class needs the Building package, I've put that class in the Building folder who is
inside the Resource folder.

Now, these are the names of the packages:
package Resource; // he is in the Resource folder who is in the Automate folder
package Building; // he is in the Building folder inside the Resource folder

This is the problem. When I try to import the Building package from the Automate class, i use this statement 'import Resource.Building.*', but that
is not the name of the package and i get an error that the directory isnt found. If i change the name into 'package Resource.Building' then the Resource
class complains that it cannot find the specified directory. All of that happens when i call both packages from the Automate class.

So ive trie a different approach.

Ive renamed the Building package into 'package Resource.Building;'. In this scenario, the Automate class uses 'import Resource.Building.*;' and the Resource
class uses 'import Building.*' since the Building folder is inside the Resource folder. Now i get the same error but when i try to compile Resource class.

I tried to make it as much as clearer as possible. Also, if anyone knows a page where packages are explained in detail, i would greatly appriciate it as long as
it is not from the oracle tutorial beacuse it doesnt help.

Thank you in advance.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I could help you, as could many others, as your description suggests you've just made a few wrong assumptions about how package names correspond to directory structures. But, I have just come in on a hot day from mowing the lawn and I'm not up to sorting it out from your narrative, so... could you post some code fragments with your package statements, imports, and class definitions (you don't need the whole definition; just the line with the "class" keyword will do), and tell us the directory path to the file containing each fragment? I think it will be easy to get you straightened out if you do that.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, as to your second question: the answer is, "No, you are not an idiot." You are having a problem and asking for help. Idiots don't do that.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:"No, you are not an idiot." You are having a problem and asking for help. Idiots don't do that.

Indeed
 
Paul Clapham
Sheriff
Posts: 22482
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like your design includes three packages: Automate, Resource, and Building.

And the root directory for your project is called Test.

In that case your Test directory should have three subdirectories: Automate, Resource, and Building. (Don't start explaining which of those three packages "need" the other packages -- that has nothing to do with it.)

Now as far as I can see your Automate package only has one class so far, and it's also called Automate. So its source code should be called Automate.java and it should be put into that Automate directory. Since it needs a lot of classes from the Building and Resource package, it should import those two packages like this:



Likewise all of the classes in the Building package should be put into that Building directory, and all of the classes in the Resource package should be put into that Resource directory.

What ties all of these things together is the classpath. To compile (and run) your code you should set your classpath to contain the Test directory. That will make the compiler, and the runtime, look for classes in (say) the Resource package in a directory called Resource under that Test directory -- which is just where you put them.

Although I have to say, if you only have four classes so far, it's a bit extravagant to create three different packages. But perhaps you expect to have many more classes in those packages in future?

 
Mario Skrlec
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the deal.

I'm just practicing the use of packages, the program itself doesn't matter.

Now. I mentioned the Test class. The Test class only imports the Automate package that has the Automate class. The Automate package then works with the the Resource package and the Building package, respectivly.
Here is the code for the Automate class;



The Automate package and its class is in the Automate folder. In the Automate folder are the Resource folder with the Resource class and the Builder folder and the Builder class



The problem is that the Automate class doesnt see the Builder class if I name it 'package Builder' and if I name It 'package Resource.Builder;' then Automate doesnt see Resource class. I dont understand.

The are the folders that also reflect packages:



I hope i explained it good enough.





 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mario Skrlec wrote:The are the folders that also reflect packages:



I hope i explained it good enough.



Just a quick check first: your package is called "Builder," but your folder is listed above as being called "Building" (which is also how you spelled it in your comment next to the Resource folder). Is that a typo here, or is that actually how it is on your disk?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your three package definitions are this:


Package names are relative to the ClassPath, not relative to the classes that use them (thank the Gosling, since relative to use would be very hard to predict). In your case, your 'class path' is the Test folder, so your folder structure should be:

And in all of the classes that need to use each other, the imports should be:



Now, if you wanted the folder to be structured the way you currently have it, like this:


You would need to change your package and import definitions. The packages would be defined as:

Because Builder is in Resource, and Resource is in Automate. Then the imports would have to look like this:


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code is also easier to read when you follow established conventions. Package names should be in all lowercase to help distinguish them from class names which start with an uppercase character.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mario Skrlec wrote:This is the deal.

I'm just practicing the use of packages, the program itself doesn't matter.

Now. I mentioned the Test class. The Test class only imports the Automate package that has the Automate class. The Automate package then works with the the Resource package and the Building package, respectivly.



I hope i explained it good enough.



Listen to what Paul is saying (that classpath part)..

You need to set the classpath till the root directory where you are placing all the package folders and class file.
If you are with this, all the classes will compile successfully..

And I don't understand, why you are having such a hierarchy of directories.
Package A needs Package B, does not mean that you need to put folder B inside folder A.
Else imagine, you would have to put java/lang folder inside every java class you make.. (As java.lang is the implicit package import)

Just place them parallely in one root directory.
 
Mario Skrlec
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i got the point. I'll try what you all said and study the literature again from the beggining. Thanks for the answers
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!