This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Your exception in initialiser has probably got nothing to do with packages.
Put both packages as folders inside your current folderPut your .java files inside the appropriate folders.
Because Person is a dependency for Test, you have to compile Person first (well, not later; you can pass an instruction to compile both together).
Once you start getting more files in those packages, you should consider using an IDE or learning how the build tools (e.g. Ant) work. From the error message it would appear you are using NetBeans. You ought to have a red mark on your code, telling you there is a compiler error, and you should not even try running the code until you have got rid of all those red marks. You would have to tell NetBeans to create the package and the new class inside it.
Joined: Oct 13, 2005
A few minutes ago, I wrote:Your exception in initialiser has probably got nothing to do with packages.
. . . .
I think I may have been mistaken about that. I think the rest of the post is all right.
because that class is not visible from other packages (it has a default access modifier). I believe that's why you're getting the error you posted.
Edit: Nope, the error message is not about what I posted, it's about the packages. But even when you follow the guidelines given by Mr Ritchie you'll still not going to be able to compile the code and that's when what I wrote comes.
The quieter you are, the more you are able to hear.
Thanks for the replies! Yes I am using netbeans and there are red "squiggly" lines under some of the code. I tried to compile it to see if the error message would give me a better idea of what was wrong. Both packages are saved under the same folder which is "NetBeansProjects".
I'm wondering though if it could be the IDE is goofing up and maybe if I compile it outside of the IDE it may work? It's worth a try I guess. My teacher says he'll go over packages hopefully in my next class so I may find out from him what I'm doing wrong here, but from what I can gather online it seems importing the package is the correct thing to do.
Joined: Oct 13, 2005
Kemal Sokolovic wrote: . . . because that class is not visible from other packages (it has a default access modifier). . . .
Also I tried to compile outside of the IDE and it gave me the same error, but one thing I noticed that was interesting is that another class I had created, (this one being in the myutil package) the class Myutil, wouldn't compile outside of the compiler, but it would inside the compiler. This may just be a lack of knowledge on my part, but I was wondering why this would be?
Compile using netbeans and run:
Attempted compile outside of netbeans:
At first I thought it was because I had already compiled the class Myutil in netbeans, but the Person class I am trying to access outside of it's package will compile inside of netbeans or outside.
Thanks for the reply! I tried making the Person class public, but it still doesn't compile.
I'm still getting this error when I try to run it and the "squiggly" red lines are still there:
Joined: Oct 13, 2005
You can’t make a package public; you can only add it to the classpath. If it is inside the current directory, then you do not need an explicit classpath. Did you compile the Person class first? Which folder is it in? If you open this old post, it should tell you more about compiling in packages.
I think I understand now why it isn't working now. The reason why Test can't see Person is that while they are both under the "C:\Users\Alex\Documents\NetBeansProjects" They differ from there in that Test is in "...\NetBeansProjects\test\src\test" and Person is in "...\NetBeansProjects\myutil\src\myutil".
I think that in the example I would need to use somthing similar to this to compile Test "C:\Users\Alex\Documents\NetBeansProjects\> javac test\src\test Test.java"? Where I am telling it the compile command "javac" the class path from where I am in the directory "test\src\test" and the file I am wanting to compile "Test.java" and the same goes for the Person class? I'll give this a try when I get home, but for now am I understanding this correctly? I'm not sure if it will work because I'm not quite understanding what the class path represents unless my above example is correct.