The Artima Developer Community
Sponsored Link

Java Answers Forum
cannot access: package--->default

5 replies on 1 page. Most recent reply: Nov 22, 2002 5:44 AM by Anil Philip

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Anil Philip

Posts: 19
Nickname: anilphilip
Registered: Nov, 2002

cannot access: package--->default Posted: Nov 21, 2002 7:09 AM
Reply to this message Reply
Advertisement
I am surprised to find this:
It seems that a class in a package cannot access (or inherit) from a class with default access(?).
For example, TaskException is a public class not in any package and is in the classpath. LogServer.java cannot access TaskException even though I do an import. Leaving out the import does not help either.


LogServer.java [9:1] '.' expected
import TaskException;
^
LogServer.java [24:1] cannot resolve symbol
symbol : class TaskException
location: interface LogReader.LogServer
public void openSession(java.lang.String serverAddress, int port) throws TaskException;

thanks,
Anil ^


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: cannot access: package--->default Posted: Nov 21, 2002 11:09 AM
Reply to this message Reply
If it isn't in an explicit package, it is in the default package, so no import is needed and in fact is an error, as shown in your output.

If the TaskException class file is in your classpath, it should be working. Have you compiled it? Is it really an Exception (does it extend Exception, or Throwable or the like)?

The following example works fine -- the files are in different directories, but the directory containing TaskException.class is in the classpath:
// --- LogServer.java ---
class LogServer
{
   public void log( String msg ) throws TaskException
   {
   }
}
 
// --- TaskException.java ---
class TaskException extends Exception
{
}

Anil Philip

Posts: 19
Nickname: anilphilip
Registered: Nov, 2002

Re: cannot access: package--->default Posted: Nov 21, 2002 12:49 PM
Reply to this message Reply
Perhaps I was not clear in my post.
If you declare a class in the default package and then try to access it from another class which is in a package, you will get a compiler error. See below for compiler output and the classes which you ran, modified as just described:
-----------------------------------
/home/aphili01/Learn>javac LogServer.java
LogServer.java:3: cannot resolve symbol
symbol : class TaskException
location: class mylog.LogServer
{ public void log( String msg ) throws TaskException { }
^
1 error

-----------------------------------
// --- LogServer.java in package myLog ---
package mylog;
class LogServer
{ public void log( String msg ) throws TaskException { }
}
-----------------------------------
// --- TaskException.java in default package ---
class TaskException extends Exception{}
-----------------------------------

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: cannot access: package--->default Posted: Nov 21, 2002 5:20 PM
Reply to this message Reply
Ah ha. I didn't realize your log class was in a package.

I have never encountered this problem. I think that is because wouldn't ever design a package that wanted to access something int the default package.

I don't know for sure that it's impossible, but it seems like there are several good reason why it might not be supported.

Suppose you (or someone else) later added a class to your package with the same name as the class in the default package -- then how do you specify which one you want to use (normally for name conflicts you use the fully-qualified package name, like java.util.List vs. java.awt.List). Or suppose the reverse happens; you are using some library package and define your own class which conflicts, then the library starts going haywire. There might even be opportunities for malicious programs to exploit this...

It just seems like a bad idea for a package to have dependencies on the default package.

So, the answer is, move the TaskException class into your package.

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: cannot access: package--->default Posted: Nov 21, 2002 6:10 PM
Reply to this message Reply
You discovered a feature of Java. You can't import anything from the unnamed package into any other package. So if you want to use a type in other packages, you must put that type in a named package.

I use the unnamed package only on rare occasions. It is just kind of convenient to use it for simple apps. For example, a couple of days ago I wrote a simple JDBC app that clears out watches in this forum's database if they threads have had no activity for 30 days. It may have 25 lines of code in the main method, and isn't used as an API, so I just left it in the unnamed package. Any package you want to use as an API, to offer types to other packages, you'll need to put into a named package.

Anil Philip

Posts: 19
Nickname: anilphilip
Registered: Nov, 2002

Re: cannot access: package--->default Posted: Nov 22, 2002 5:44 AM
Reply to this message Reply
If it is a feature, then it is a little-known feature, because I did not find it in the book!

Yes, this was temporary. I developed the server side classes in the default package (for now, while still developing).

I was trying to use this in some client side classes which I had earlier put into a package.

Anyway, I have put TaskException and the server side stuff into its package.

thanks for the replies!
Anil


> You discovered a feature of Java. You can't import
> anything from the unnamed package into any other package.
> So if you want to use a type in other packages, you must
> put that type in a named package.
>
> I use the unnamed package only on rare occasions. It is
> just kind of convenient to use it for simple apps. For
> example, a couple of days ago I wrote a simple JDBC app
> that clears out watches in this forum's database if they
> threads have had no activity for 30 days. It may have 25
> lines of code in the main method, and isn't used as an
> API, so I just left it in the unnamed package. Any package
> you want to use as an API, to offer types to other
> packages, you'll need to put into a named package.

Flat View: This topic has 5 replies on 1 page
Topic: removing duplicates from arrays Previous Topic   Next Topic Topic: hey..java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use