• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Package understanding

 
Divas Pandey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divas Java wrote:... I compiled variousareas.java and it got compiled successfully


Do you see the variousareas.class file?
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic