Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is There No Such Thing as a Root Package?

 
Kevin Simonson
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your public class must have the same name of the file:



And not Rp.java
 
Henry Wong
author
Marshal
Pie
Posts: 20999
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kevin Simonson
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephan van Hulst
Bartender
Pie
Posts: 5589
54
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Sheriff
Pie
Posts: 20962
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic