Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

define a package for a class  RSS feed

 
Luca Olivieri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

i have a problem. I defined a package for my class:

The C class is in a directory called c. In the same directory there is the D class, but this
class has no package

file C.java


file D.java


When I try to compile the class C I have this error:

MacBook-Pro-di-Luca:Test Package Luca$ javac -cp .:c/ c/C.java
c/C.java:10: error: cannot access D
D d = new D();
^
  bad source file: ./c/D.java
    file does not contain class c.D
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.
1 error

this is mi tree directory (e is not important):

.
├── C.java
├── D.java
└── e
    └── E.java

1 directory, 3 files

When I define a package for a class I can't access to other class that aren't in package?

I hope I was clear.
Thank you all

Luca
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Luca!
first of all change the name of the directory from c to anything else.it's putting a confusion b/w the main c directory and your package named c,another thing use the oracle convention for naming classes dont use single litterals,use noun's for naming it.

----------------

1.Dont put the class and source file in a same directory,Rather create the folders named "source" and "classes" for the respective purpose and set the classpath to classes directory,if you don't know how to set the class path,no worrie's learn it here--->Wiki/set classpath.

2.In your case you have set the class path to---> .c/D.java-->where actually the compiler will search for the required class files so set it to ---> ".c" ,thats a bad source file because you have mentioned compiler for doing a search for required class file in a file of type ".java" which is annoying.

i would advice you to strictly follow what i have mentioned first
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The root cause is that your class D does not declare a package. Therefore, Java will put this in the default package. For small, single-class programs, having your class in the default package doesn't matter much. But when you have multiple compilation units (*.java files), then it becomes less confusing if you have an explicit package declaration in each one of them. In this case, if C.java and D.java are in the directory c, then D should have a package declaration: package c;
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, if you have c\e\E.java, then the package declaration in E.java should be: package c.e; if you're going to compile and run the classes in C.java, D.java, E.java as part of a larger program.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!