Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Help WIth Packages

  Asked By: Joel    Date: Dec 02    Category: Java    Views: 659
  

I was writing a program with Packages.
Created a directory called P1 and wrote these programs

1)Protection.java
package p1;
public class Protection
{
int n=1;
private int n_pri =2;
protected int n_pro =3;
public int n_pub =4;
public Protection()
{
System.out.println("Base Constructor");
System.out.println("n"+n);
System.out.println("n_pri"+n_pri);
System.out.println("n_pro"+n_pro);
System.out.println("n_pub"+n_pub);
}
}
2)Derived.java
package p1;
class Derived extends Protection
{
Derived()
{
System.out.println("Derived Constructor");
System.out.println("n"+n);
//System.out.println("n_pri"+n_pri);
System.out.println("n_pro"+n_pro);
System.out.println("n_pub"+n_pub);
}
}
3)SamePackage.java
package p1;
class SamePackage
{
samePackage()
{
Protection p = new Protection();
System.out.println("Same Package
Constructor");
System.out.println("n"+p.n);
//System.out.println("n_pri"+p.n_pri);
System.out.println("n_pro"+p.n_pro);
System.out.println("n_pub"+p.n_pub);
}
}
4)Demo.java
package p1;
public class Demo
{
public static void main(String args[])
{
Protection ob1 = new Protection();
Derived ob2 = new Derived();
SamePackage ob3 = new SamePackage();
}
}







But iam not able to compile it . These are the error messages on
compiling

C:\j2sdk1.4.1_03\bin\p1>javac Demo.java

Demo.java:7: cannot resolve symbol
symbol : class Protection
location: class p1.Demo
Protection ob1 = new Protection();
^
Demo.java:7: cannot resolve symbol
symbol : class Protection
location: class p1.Demo
Protection ob1 = new Protection();
^
Demo.java:8: cannot resolve symbol
symbol : class Derived
location: class p1.Demo
Derived ob2 = new Derived();
^
Demo.java:8: cannot resolve symbol
symbol : class Derived
location: class p1.Demo
Derived ob2 = new Derived();
^
Demo.java:9: cannot resolve symbol
symbol : class SamePackage
location: class p1.Demo
SamePackage ob3 = new SamePackage();
^
Demo.java:9: cannot resolve symbol
symbol : class SamePackage
location: class p1.Demo
SamePackage ob3 = new SamePackage();
^
6 errors



Share: 

 

3 Answers Found

 
Answer #1    Answered By: Holly Brown     Answered On: Dec 02

To compile  and run your program  you could work in the directory  which
is parent of the package directory, in your case is:
C:\j2sdk1.4.1_03\bin

To compile:
javac p1\Demo.java

To execute:
java p1.Demo

 
Answer #2    Answered By: Maliha Malik     Answered On: Dec 02

Have you tried with importing all classes of the p1 package.

import p1.* ;

try including above line in the main file of yours.

Hope this will help....

 
Answer #3    Answered By: Edward Jones     Answered On: Dec 02

compile all classes at the same time

javac *.java

 
Didn't find what you were looking for? Find more on Help WIth Packages Or get search suggestion and latest updates.




Tagged: