wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "packages" Watch "packages" New topic
Author

packages

Neena Shah
Greenhorn

Joined: Nov 18, 2000
Posts: 9
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

Joined: Nov 18, 2000
Posts: 5
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

Joined: Nov 18, 2000
Posts: 9
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

Joined: Nov 18, 2000
Posts: 5
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

Joined: Nov 19, 2000
Posts: 2
U have to compile the java file like
c:\stuff:\p> javac *.java.
i think this should work
Thanks
Rohit
Neena Shah
Greenhorn

Joined: Nov 18, 2000
Posts: 9
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

Joined: Nov 18, 2000
Posts: 9
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

Joined: Nov 19, 2000
Posts: 2
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
 
wood burning stoves
 
subject: packages
 
Similar Threads
How to Compile source code from a different Java project using Maven
problem with wait()- notify()
Interfaces doubt
exam lab gc question
IllegalMonitorStateException from notify()