This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Access modifier question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access modifier question" Watch "Access modifier question" New topic
Author

Access modifier question

Sumanth Shanbhag
Greenhorn

Joined: May 10, 2007
Posts: 13
I have a directory called test and it has these two files in them.

1) A.java and
2) B.java

The code for A.java


The code for B.java


The A.Class file is in the directory test. When i try compiling B.java[while being in the directory test] it is not able to resolve A(compile time error). Could somebody tell me what is happening?
Where am i going wrong?
Shafian Kisna
Ranch Hand

Joined: Oct 25, 2005
Posts: 70
For the instance, B is stored within the same package as A (Test). However since its in the same package, the error was called. From A.java, remove the package Test and run the program. It should not show any error.
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
The compiler is looking for a directory test with a file A.java in it, but as you are already in the directory test, it cannot be found. I tried compiling it with:

javac -cp .. B.java

telling the compiler where to start looking for the directory test and everything went fine (after adding one semicolon and a curly brace to your code)
Sumanth Shanbhag
Greenhorn

Joined: May 10, 2007
Posts: 13
Shafian,

I am not too sure i got that. I am defining the package in A.java. Why would i have to remove that? You mean to say that A and B are not supposed to be in the same package(test)?
Shafian Kisna
Ranch Hand

Joined: Oct 25, 2005
Posts: 70
Hi Sumanth,

The reason why you do not need to specify the import is that, you are already placing B within the same package.
Sumanth Shanbhag
Greenhorn

Joined: May 10, 2007
Posts: 13
Hi Sasha,

I was trying to figure out what "cp" meant in the compile statement?
Is it the CLASSPATH that you are mentioning? I tried using this to compile but it still gave that error.


>javac - classpath ## B.java

## = the pwd
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Note that you have the package structure and directory structure wrong.

The directory structure must match the package structure. You have a class A in package test, which is in directory 'test'; your class B is not in package test, yet you have it in the same directory as class A.

You need to move B.java up one directory (it must not be in the 'test' directory, but one level up), or you must put class B in package test also (add a statement "package test;" to B.java and remove the "import test.A;").

The Java Tutorial - Packages
[ May 18, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Anita Singh
Greenhorn

Joined: May 18, 2007
Posts: 17
Hi,
You are telling that B.java is also in test package,but You are not using package statement there.

use package test;
in B.java also.
& remove import statement from there.

Then compile both the java file using below comand:-
javac -d . A.java
javac -d . B.java
it crete the folder test & put your class file there &
for running B.java write:-
java test.B

Anita Singh


SCJP 5.0 100%
 
Consider Paul's rocket mass heater.
 
subject: Access modifier question
 
Similar Threads
Import
Package
Access modifiers
using package getting compiler error
Not able to COMPILE!!!