• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

packages

 
Neena Shah
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have two java files:
package p;
public class E1
{
int i=10;
int j=20;
int aSum(int a1, int a2)
{
int result = a1 + a2;
return result;
}
}

package p;
public class E2
{
int i1=10;
int j1=20;
public static void main(String args[])
{
E1 example1 = new E1();
int result = example1.aSum(10,20);
System.out.println(result);
}
}

when I try to compile E2 it says p.E1 not found....why is that?
How do we call a class that is in the same package?

Thanks
neena
 
steve viney
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried it and it worked ok for me. Did you remember to javac E1 before trying E2? Otherwise E2 wouldn't know about the exsistance of E1.
 
Neena Shah
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, I did. I compiled E1 first and then E2.
I created a directory called Stuff and then added directory p to it.....
C:\Stuff\p\E1.java
C:\Stuff\p\E2.java
This is the structure...do I need to make any changes to the classpath or something? I set the classpath to C:\Stuff
Thanks
Neena
 
steve viney
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think i had classpath set, i did a CD into the directory as the 2 class files were in and worked from there
sorry i couldn't help
 
rohit bhatt
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U have to compile the java file like
c:\stuff:\p> javac *.java.
i think this should work
Thanks
Rohit
 
Neena Shah
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
when compiling I tried this way:
c:Stuff > javac -d . E1.java
c:Stuff > javac -d . E2.java
c:Stuff > java p.E2
30
Now I got the answer. I followed a procedure written in one of the java sites.....Can anyone tell me why that works? Is that the way to compile packages?
Thanks
Neena
 
Neena Shah
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
when compiling I tried this way:
c:Stuff:p> javac -d . E1.java
c:Stuff:p> javac -d . E2.java
c:Stuff:p> java p.E2
30
Now I got the answer. I followed a procedure written in one of the java sites.....Can anyone tell me why that works? Is that the way to compile packages?
Thanks
Neena
 
rohit bhatt
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi .
u can also do like this
c:\stuff\p> javac -d . E1.java
will store the class file in the same directory.
Benifit of using package is if u r having same file E1.java in two different dir. then u can distingish using package name,
so while running E2 u have to use package name and class file.
I hope this will help u.
Thanks
Rohit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic