• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is There No Such Thing as a Root Package?

 
Ranch Hand
Posts: 248
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your public class must have the same name of the file:



And not Rp.java
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 248
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 15630
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic