I am a student and I am new to Java. I have created a simple Java program. We have been given a package to import into the program we have developed. The classes of the package are stored in a folder called "pack" which is placed inside project folder called "tictactoe". The example program we were given imports the package as follows and it works fine.
import pack.*;
Is it that simple or do I need to do more.
It gives me the error "The import pack cannot be resolved"
Firstly, you have to include jar file that containing your pakage in your project. In Eclipse is setting build path.
Then is using import statement to import the needed class.
By the way, I recommened that you shouldn't import like that : import package_example.*
Should importing exactly what classes you need, e.g : import package_example.Class_A;
The package "pack" is just a folder containing files with .java and .class extensions. We are asked to create a program that contains four classes which should use the files inside the folder called "pack" by importing the "pack" i-e
import pack.*;
It does work for the example program given by the teacher but not for me. I am sure I am missing something. Could you please tell me the the way it is set in the build path?
If you having java source code files, just copy them into your package.
e.g : in package 'pack' already contains java source code like this : LibraryA.java, then just copy this file into your package. And using import statement like this in your code : import your_package.LibraryA;
You can set build path in Eclipse , if the classes you intend to 'import' have already packaged in jar file.
I have copied the .java classes into my project and it works fine. I do not need to import them any more. But I still cannot use the .class files and it gives me the same error. This is how I import them
I have copied the .java classes into my project and it works fine. I do not need to import them any more. But I still cannot use the .class files and it gives me the same error. This is how I import them
import pack.reader.class
Is this correct?
Hi gulHK,
You dont need to import java class for using, because that the class that be used in your program ( e.g Main.java ) is located the same package (folder) with Main.java. If it's belong to another package, you have to import in Main.java to use.
Bookmarks