aspose file tools*
The moose likes Beginning Java and the fly likes Accessing a class in another Package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing a class in another Package" Watch "Accessing a class in another Package" New topic
Author

Accessing a class in another Package

Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

The assignment is to access a class from another package, but I'm messing up on something. I'm given a package and told to create another package and access a class in the first one.

This is my code to access the package:


This is the class I am trying to access:



I would think I could just import the package I need, but it doesn't recognize the package I'm trying to import.



It says it doesn't exist at "at test.Test.<clinit>(Test.java:6)" , but I'm telling it to look in "myutil" with the command "import myutil.Person". Correct?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
Compile with
javac myutils/Person.java
java test/Test.java
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

You're not able to use:

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.
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Kemal Sokolovic wrote: . . . because that class is not visible from other packages (it has a default access modifier). . . .
Damn! I didn’t notice that.
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

Is there a way to allow my test.Test to see the myutil.Person? In a sense if I make the package "public" that would allow me to access it. Just adding:



wouldn't work, but is there a way to make it do what I am trying?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Either put both classes in the same package or make Person class public.
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

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.
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

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:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

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.

Thanks for the help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing a class in another Package