aspose file tools*
The moose likes Java in General and the fly likes Is There No Such Thing as a Root Package? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is There No Such Thing as a Root Package?" Watch "Is There No Such Thing as a Root Package?" New topic
Author

Is There No Such Thing as a Root Package?

Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 114
If I write a simple Java class called "HeloWorld.java" in my current directory like so:

it compilies with command "javac HelloWorld.java" and when executed with "java HelloWorld" prints out the phrase that we all expect. Note that there's no line at the start of the program that declares this Java class' package. I thought that it compiles and runs because in the absence of any specified package, the compiler puts it in the package corresponding to the directory where the "javac" command is executed.

So let's say I have files like so:

<root>
|--- Rp.java
|--- RpSub
| `--- UseRp.java
`--- RunIt.java

where the relevant pieces of source code are:



When I change directory to <root> and type in "javac Rp.java", it compiles class <Rp> just fine. But then when I type in "javac RpSub\UseRp.java" I get error messages:

RpSub\UseRp.java:3: '.' expected
import Rp;
^
RpSub\UseRp.java:3: ';' expected
import Rp;
^
2 errors

Why doesn't this work? Why can't I import class <Rp> to my class <UseRp> in package <RpSub>? Is it perhaps the case that one can only compile classes in the root directory if it doesn't use, or isn't used by, classes in other packages?

Kevin Simonson
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Your public class must have the same name of the file:



And not Rp.java


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40

Kevin Simonson wrote:When I change directory to <root> and type in "javac Rp.java", it compiles class <Rp> just fine. But then when I type in "javac RpSub\UseRp.java" I get error messages:

RpSub\UseRp.java:3: '.' expected
import Rp;
^
RpSub\UseRp.java:3: ';' expected
import Rp;
^
2 errors

Why doesn't this work? Why can't I import class <Rp> to my class <UseRp> in package <RpSub>? Is it perhaps the case that one can only compile classes in the root directory if it doesn't use, or isn't used by, classes in other packages?


Java doesn't support mixing classes that are in packages and classes that are not in packages. I believe that it use to support it, but not anymore.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 114
Hebert Coelho wrote:Your public class must have the same name of the file:



And not Rp.java


Hebert, "Rp.java" was the file I listed below where I mentioned it, namely:

I have three files in the directory I referred to as <root>, "HelloWorld.java", "Rp.java", and "RunIt.java". In addition to those three, directory "RpSub" also resides in that directory, and in "RpSub" resides file "UseRp.java".

Kevin Simonson
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

As already pointed out, you either use all classes in the default package (not "root") or all classes in named packages. No mixing as of Java 1.4.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

<root> implies packages have a hierarchical structure. They don't, really. They are only stored that way on the file system, but as an example, java.util is completely unrelated to java.util.concurrent.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18988
    
    8

Bear Bibeault wrote:As already pointed out, you either use all classes in the default package (not "root") or all classes in named packages. No mixing as of Java 1.4.


That's not quite right -- classes in the default package can import classes in named packages, such as java.util.LinkedList and so on. I have dozens of little test programs which do that. It's just that classes in named packages can't import classes in the default package.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Kevin,

As Stephan pointed out, Java does not recognize any sort of package hierarchy. Conceptually, they are frequently used that way in the real world, but Java itself doesn't care.

John.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is There No Such Thing as a Root Package?