aspose file tools*
The moose likes Beginning Java and the fly likes Help with Package understanding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Package understanding" Watch "Help with Package understanding" New topic
Author

Help with Package understanding

Divas Pandey
Greenhorn

Joined: Dec 27, 2011
Posts: 3
Hello,

I am relatively new to Java, may be the question that I am asking is very simple but please help me out with this.

I created a folder under the bin directory named "MyPack" here i am putting my Sample Java files.

Now I created a file under this named variousareas.java and the file contains:

package MyPack;

public class variousareas
{
int radius;
int height;
int width;
int area;

public void circlearea(int radius)
{
area=(int)3.14*radius*radius;
System.out.println("area of the circle is"+area);
}

public void rectangle(int width,int height)
{
area=width*height;
System.out.println("area of the circle is "+area);
}
}

then I created one more file areamain.java and the file contains:

package MyPack;
public class areamain
{
public static void main(String []args)
{
variousareas v1=new variousareas();
v1.circlearea(2);
v1.rectangle(2,3);
System.out.println("Keep searching, this search is on, on and on");
}
}

then I compiled variousareas.java and it got compiled successfully, now when I am trying to compile areamain.java I am getting the error:

C:\Program Files\Java\jdk1.7.0_02\bin\source files\MyPack\areamain.java:8: error
: cannot find symbol
variousareas v1=new variousareas();
^
symbol: class variousareas
location: class areamain
2 errors

Can anyone please point out my mistake....
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1558
Divas Java wrote:... I compiled variousareas.java and it got compiled successfully


Do you see the variousareas.class file?


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
If suppose I have two classes in two sources files A.java and B.java inside a package mypack like below -



Then while compiling B I use javac mypack/B.java where mypack is the package name.
If I compile moving into the directory mypack in the cmd prompt and just give javac B.java I get the error you posted.

Divas Pandey
Greenhorn

Joined: Dec 27, 2011
Posts: 3
Then while compiling B I use javac mypack/B.java where mypack is the package name.
If I compile moving into the directory mypack in the cmd prompt and just give javac B.java I get the error you posted.

view plaincopy to clipboardprint?
C:\test>cd mypack

C:\test\mypack>javac B.java
B.java:5: cannot find symbol
symbol : class A
location: class mypack.B
A a = new A();
^
B.java:5: cannot find symbol
symbol : class A
location: class mypack.B
A a = new A();
^
2 errors

C:\test\mypack>cd ..

C:\test>javac mypack\B.java

I tried compiling the same by coming one directory above but was not able to do so, anyways one doubt, how does it matter at the time of compilation to jump to one directory above will solve the problem.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Divas Java wrote:I tried compiling the same by coming one directory above but was not able to do so,....

Always TellTheDetails... how did you compile? Did you put package/sourcefile in the javac command?
Jagannath Duraisamy
Greenhorn

Joined: Sep 30, 2011
Posts: 7

while compiling A.java its accessing itself inside the "MyPack" package. but while trying to compile B.java file its trying to access A.class file which is inside a "MyPack" folder that is inside another "MyPack" folder i.e(MyPack/MyPack). which doesn't exist. so we have to come out of that package and include the package name also while compiling the B.java file. so i create another folder "MyPack" inside "MyPack" folder i.e(MyPack/MyPack) and place the compiled A.class file inside "MyPack/MyPack" folder and compile B.java file then its get compiled fine. this is what happening now. somebody can explain and make us clear and correct me if im wrong

C:\Jagan\mypack>javac A.java

C:\Jagan\mypack>javac B.java
B.java:5: cannot find symbol
symbol : class A
location: class mypack.B
A a = new A();
^
B.java:5: cannot find symbol
symbol : class A
location: class mypack.B
A a = new A();
^
2 errors

C:\Jagan\mypack\mypack>javac A.java
C:\Jagan\mypack>javac B.java
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Package understanding