i have make one package(c:\program files\java\jdk1.6.0\bin\MyPack)
and save file as AccountBalance.java this file has two classes.see below.

package MyPack;
class Balance
{
String name;
double bal;
Balance(String n,double b)
{
name=n;
bal=b;
}
void show()
{
if(bal<0)
System.out.print("-->");
System.out.print(name+":$"+bal);
}
}
class AccountBalance
{
public static void main(String args[])
{
Balance current[]=new Balance[3];

current[0]=new Balance("ajay",123.23);
current[1]=new Balance("rinku",145.25);
current[2]=new Balance("don",-152.12);

for(int i=0;i<3;i++)
current.show();
}
}


now i m import this package in file Hello.java(c:\my java\Hello.java)
as given below.

import MyPack.AccountBalance;

class Hello extends AccountBallance
{
public static void main(String arg[])
{ System.out.print("my name is ajay jaiswal");
AccountBalance b=new AccountBalance();
}
}

on compling this i m gating error
packge MyPack does not exist

can u tellme where i m wrong.
i have also try to import
import MyPack.*;
but it is also not working
i try this also
import MyPack.Balance;
import MyPack.AccountBalance;
but result is same error

peter_budo commented: Ask for help and ignore the answer. What is the purpose to help him??? -1

Recommended Answers

All 13 Replies

You should not make any changes to instalation of your Java! No adding new folders or files. So place your folder MyPack somewhere else on the disk.
My only experience with package are that the package have to be in same folder us program with main method and you can go down the line.
For example in project folder I will have MyProgram(with main method), then folder Tools. In this folder I can have some Java files. This is classic example of creating package. So in each Java file of this folder you enter as first line package Tools; and in MyProgram you will say import Tools.*; .
However Tools folder can have some other folder/-s in like Substring. Any java file inside should start with package Tools.Substring; and to call any of them in MyProgram you say import Tools.Substring.*; . What I described above so far worked for me as my package and rest of coding always been in same folder or package in subfolders, but I do not know how to do it if your program and package are in different folders. I think it has to be done by declaring CLASSPATH.
I maybe wrong so if anybody spot some error please corect it.

You should not make any changes to instalation of your Java! No adding new folders or files. So place your folder MyPack somewhere else on the disk.
My only experience with package are that the package have to be in same folder us program with main method and you can go down the line.
For example in project folder I will have MyProgram(with main method), then folder Tools. In this folder I can have some Java files. This is classic example of creating package. So in each Java file of this folder you enter as first line package Tools; and in MyProgram you will say import Tools.*; .
However Tools folder can have some other folder/-s in like Substring. Any java file inside should start with package Tools.Substring; and to call any of them in MyProgram you say import Tools.Substring.*; . What I described above so far worked for me as my package and rest of coding always been in same folder or package in subfolders, but I do not know how to do it if your program and package are in different folders. I think it has to be done by declaring CLASSPATH.
I maybe wrong so if anybody spot some error please corect it.

thanks sir for helping me.but i want too tell u what i do after posting this problem.i reffer the book "The Complete Reference by HERBERT Schildt"in that book i see some example n try that n that aslo i can't understand disirabale ans.In that book they give an example in that there was two package p1 & p2.Where as in p1 they have three files and in p2 they give two files and they aslo give two Demo.java file in each package p1 & p2 which i am pasting below.

file name =Protectection.java
i save it in "C:\program files\java\jdk1.6.0\bin\p1"

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);
}
}


second file is Derived.java
i save it in "C:\program files\java\jdk1.6.0\bin\p1"

package p1;
public class Derived extends Protection
{
public Derived()
{
System.out.println("derived constructor");
System.out.println("n= "+n);
// class only
// System.out.println("n_pri= "+n_pri);
System.out.println("n_pro= "+n_pro);
System.out.println("n_pub= "+n_pub);
}
}

third file is SamePackage.java
i save it in "C:\program files\java\jdk1.6.0\bin\p1"


package p1;
public class SamePackage
{
public SamePackage()
{
Protection p=new Protection();
System.out.println("same package");
System.out.println("n= "+p.n);
//class only
//System.out.println("n_pri= "+p.n_pri);
System.out.println("n_pro= "+p.n_pro);
System.out.println("n_pub= "+p.n_pub);
}
}

now in p2 i have following two files
first files is Potection2.java
i save it in "C:\program files\java\jdk1.6.0\bin\p2"

package p2;
public class Protection2 extends p1.Protection
{
public Protection2()
{
System.out.println("derived other package constuctor");

//class or package only
//System.out.println("n= "+n);
System.out.println("n_pro= "+n_pro);
System.out.println("n_pub= "+n_pub);
}
}

secont file is OtherePackage.java
i save it in "C:\program files\java\jdk1.6.0\bin\p2"

package p2;
public class OtherPackage
{
public OtherPackage()
{
p1.Protection p=new p1.Protection();
System.out.println("other package constructor");
System.out.println("n_pub= "+p.n_pub);
}
}

now after that i make two Demo.java file in p1 n p2 are as follow.

in p1

package p1;
public class Demo
{
public static void main(String args[])
{
Portection ob1=new Protection();
Derived ob2=new Derived();
SamePackage ob3=new SamePackage();
}
}

But this is also not working

now in p2 second Demo.java is

package p2;
public class Demo
{
public static void main(String arg[])
{
Protection2 ob1=new Protection2();
OtherPackage ob2=new OtherPackage();
}
}

this is also not working

now after that i do which work is that,I make My.java in C:\program files\java\jdk.6.0\bin is

import p1.*;
import p2.*;
//import p1.Protection;
//import p1.Derived;
//import p1.SamePackage;

class My
{
public static void main(String a[])
{
Protection p=new Protection();
Derived d=new Derived();
SamePackage sp=new SamePackage();
OtherPackage op=new OtherPackage();
Protection2 pr2=new Protection2();
}
}


and i also try to run this package by storing at different places in drive but it works only in above condition and with help of My.java

can u tell me what is the reson behind this.
i m looking for u r help.
thanking u.

commented: use code tags -2

don't use that book. It's riddled with errors.
There is some valuable information in it, but unless you know Java very well you'll never find it.

I seriously doubt though he'd tell you to do what you're trying.
Placing anything in the JVM installation is BAD, so is naming packages with capitals.
I'm pretty sure he explains about classpath settings too.

tahks sir for helping me.can u tell me which book i should reffer for larning basic of java.

don't use that book. It's riddled with errors.
There is some valuable information in it, but unless you know Java very well you'll never find it.

I seriously doubt though he'd tell you to do what you're trying.
Placing anything in the JVM installation is BAD, so is naming packages with capitals.
I'm pretty sure he explains about classpath settings too.

Something which is not older then 2004, for example I use Java How to program from deitel&deitel, plus have Swing book from Osborne just pick something which suite your reading style...

...and do a favour to the members of this forum by posting code in code tags. :D

commented: thanks for wishing.but i m thankfull to u who help me. +1

Head First Java 2nd edition (Kathy Sierra and Bert Bates) is generally recognised as one of the best books for beginners.
Agile Java (Jeff Langr) is also good.

sure sir next time i will post my code in code tag.


Head First Java 2nd edition (Kathy Sierra and Bert Bates) is generally recognised as one of the best books for beginners.
Agile Java (Jeff Langr) is also good.

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);
  }
 }
 
 
second file is Derived.java
i save it in "C:\program files\java\jdk1.6.0\bin\p1"
 
package p1;
public class Derived extends Protection
{
public Derived()
 {
  System.out.println("derived constructor");
  System.out.println("n= "+n);
//  class only
//  System.out.println("n_pri= "+n_pri);
  System.out.println("n_pro= "+n_pro);
  System.out.println("n_pub= "+n_pub);
  }
 }
 
third file is SamePackage.java
i save it in "C:\program files\java\jdk1.6.0\bin\p1"
 
 
package p1;
public class SamePackage
{
 public SamePackage()
 {
  Protection p=new Protection();
  System.out.println("same package");
  System.out.println("n= "+p.n);
//class only
//System.out.println("n_pri= "+p.n_pri);
  System.out.println("n_pro= "+p.n_pro);
  System.out.println("n_pub= "+p.n_pub);
  } 
 }
 
now in p2 i have following two files
first files is Potection2.java 
i save it in "C:\program files\java\jdk1.6.0\bin\p2"
 
package p2;
public class Protection2 extends p1.Protection
{
public Protection2()
 {
  System.out.println("derived other package constuctor");
  
//class or package only
//System.out.println("n= "+n);
  System.out.println("n_pro= "+n_pro);
  System.out.println("n_pub= "+n_pub);
  }
 }
 
secont file is OtherePackage.java
i save it in "C:\program files\java\jdk1.6.0\bin\p2"
 
 package p2;
public class OtherPackage
{
 public  OtherPackage()
 {
  p1.Protection p=new p1.Protection();
  System.out.println("other package constructor");
  System.out.println("n_pub= "+p.n_pub);
  } 
 }
 
now after that i make two Demo.java file in p1 n p2 are as follow.
 
in p1
 
package p1;
public class Demo
{
 public static void main(String args[])
 {
  Portection ob1=new Protection();
  Derived ob2=new Derived();
  SamePackage ob3=new SamePackage();
  }
 }
 
But this is also not working
 
now in p2 second Demo.java is
 
package p2;
public class Demo
{
 public static void main(String arg[])
 {
  Protection2 ob1=new Protection2();
  OtherPackage ob2=new OtherPackage();
  }
 }
 
this is also not working
 
now after that i do which work is that,I make My.java in C:\program files\java\jdk.6.0\bin is 
 
import p1.*;
import p2.*;
//import p1.Protection;
//import p1.Derived;
//import p1.SamePackage;

class My 
{
 public static void main(String a[])
 {
  Protection p=new Protection();
  Derived d=new Derived();
  SamePackage sp=new SamePackage();
  OtherPackage op=new OtherPackage();
  Protection2 pr2=new Protection2();
  }
 }

Why do you ask for advice if you ignore what we says, NO CHANGES TO INSTALATION OF JAVA!

i m sorry sir
but i always go through u r advice.i don't know y u fell like that.may be i miss u r advice but i want to tell u that i fill that u r advices always help me that the reason that i always ask for help from u and other.but i m human so may be i do mistake but i never did that intetionly.so again i want to say sorry publicly if u feel like that.

Why do you ask for advice if you ignore what we says, NO CHANGES TO INSTALATION OF JAVA!

So how can you explain this?

second file is Derived.java
i save it in "C:\program files\java\jdk1.6.0\bin\p1"

third file is SamePackage.java
i save it in "C:\program files\java\jdk1.6.0\bin\p1"

now in p2 i have following two files
first files is Potection2.java
i save it in "C:\program files\java\jdk1.6.0\bin\p2"

secont file is OtherePackage.java
i save it in "C:\program files\java\jdk1.6.0\bin\p2"

Placing anything in the JVM installation is BAD, so is naming packages with capitals.

You should not make any changes to instalation of your Java! No adding new folders or files. So place your folder MyPack somewhere else on the disk.

ajay_tabbu you been told by two of us DO NOT make any changes/ DO NOT place anything into original instalation. What did you do? You still get on placing all your programs into C:\program files\java\jdk1.6.0\bin directory :'(

sir i follow u r instruction so i got the solution os my probleam at that time.but as some one ask me to post my code in code tag thats y i post my code again in code tag that was the only reasion.but i has alrady sloved my problem.i realise that fact that u advise help me in that.so thanks for that and next time i will inform u all when i get solution or what result or error i got after applying u r instrution.
and i want to say one more thing that really i m very thankfull for u and all other as they help me lot.by reading u r another post i got realy pratical knowleg and aproch.this thing help me in my campus selection also.and i placed finaly in company.so from the core of my heart i have grat respect for you all people.
:)

So how can you explain this?


ajay_tabbu you been told by two of us DO NOT make any changes/ DO NOT place anything into original instalation. What did you do? You still get on placing all your programs into C:\program files\java\jdk1.6.0\bin directory :'(

commented: Congratulations on getting a job ;-) ~s.o.s~ +17
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.