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

package problems

marilyn murphy
Ranch Hand

Joined: Aug 28, 2001
Posts: 84
Every time I think I understand this, I find again that I don't.

SuperclassX.java and SubclassY.java are both in C:\Java. No problem compiling SuperclassX.java, however SubclassY.java won't compile.
C:\Java\SubclassY.java:4: cannot resolve symbol
symbol : class SuperclassX
location: class packageY.SubclassY
public class SubclassY extends SuperclassX
^
I have directory C:\Java\packageY and my classpath includes it. I've tried C:\Java\javac SubclassY.java
I've tried C:\Java\packageY\javac SubclassY.java

Well, I won't tell you what all I tried, but nothing works. What am I doing wrong?
Kris Nelson
Ranch Hand

Joined: Nov 04, 2001
Posts: 35
My guess is that SubclassX (EDITED, I meant this to be SuperclassX, sorry) has default (or package) visibility and cannot be accessed by other classes outside the SAME default package.
------------------
WebNelly.com
Java/XML Web Development
Check it out! http://www.webnelly.com
[This message has been edited by Kris Nelson (edited November 09, 2001).]


WebNelly.com<br />Java/XML Web Development<br />Check it out!<br /><a href="http://www.webnelly.com" target="_blank" rel="nofollow">http://www.webnelly.com</a>
marilyn murphy
Ranch Hand

Joined: Aug 28, 2001
Posts: 84
There is no SubclassX. SuperclassX is public.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

The problem is that you declare SubclassY as belonging to packageY while SuperclassX does not have a package declaration making it belong to the DEFAULT package.
These are your options :

  • put the same package declaration in SuperclassX to make it belong in packageY as well
  • remove the package declaration in SubclassY to make it belong to the default package as well.

  • If you choose the first option, you may have to compile with the -sourcepath and -d options (see the help by typing just 'javac') if your source files are in C:\Java. To avoid the hassle of having to deal with the options, just put your source files in a directory with the same name as the package they belong to.
    A third option: you declare the classes to belong to different packages (say packageX for SuperclassX). You must then include the statement
    import packageX.*;
    in SubclassY.java so that it can "see" SuperclassX.
    HTH
    ------------------
    Junilu Lacar
    Sun Certified Programmer for the Java� 2 Platform
    [This message has been edited by JUNILU LACAR (edited November 09, 2001).]


Junilu - [How to Ask Questions] [How to Answer Questions]
marilyn murphy
Ranch Hand

Joined: Aug 28, 2001
Posts: 84
I thought if I put it in the default package I would not have to import it.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Originally posted by marilyn murphy:
I thought if I put it in the default package I would not have to import it.

If you put it in the same package, you won't have to import it.

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
marilyn murphy
Ranch Hand

Joined: Aug 28, 2001
Posts: 84
Is there any way to import the default package?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: package problems