How do we import our own package to a program in Java?

I have created the following package including the add class to add program-

package add;
public class Add1{

int a=3;
int b=2;
int c=a+b;
}

Open in new window


now when I make another  program  to print this add and import my add package by using import keyword that is
import add.*;

Open in new window

it shows an error that is
package add does not exist, incorrect package.

Plz tell me what is the problem if it is wrong tell me the right way how to import my own package  to anothrer prgram with an example
Sourodip KunduAsked:
Who is Participating?
 
rrzCommented:
Here is what I did with your code.
In my working directory,  I created a directory named src
Inside the src directory, I created two files: Add1.java
package add;
public class Add1{
	public int a = 3;
	public int b = 2;
	public int c = a + b;
}

Open in new window

 and  Test.java  
import add.*;
public class Test {
    public static void main(String arg[]){
        Add1 aObject = new Add1();
        System.out.println(aObject.a);
	    System.out.println(aObject.b);
		System.out.println(aObject.c);
    }        
}

Open in new window

 on my command line I typed three commands
javac ./src/Add1.java -d .

javac ./src/Test.java -d .

java Test

Open in new window

and the output was
3
2
5
0
 
raj3060Commented:
It looks like that is a reserved keyword.
Try using an different package name, e.g.
package com.testcode.addnumbers;

and then import in another file using import.

On the other hand, if you are using eclipse, don't add the import and let eclipse find it for you ...
You can do cntrl + shft + o to let eclipse resolve packages.
0
 
CEHJCommented:
Sounds like your package structure is wrong.

Please post a recursive file listing of your project directory
0
 
Sourodip KunduAuthor Commented:
What do you mean by recursive file listing plz explain
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.