Eduard
Eduard

Reputation: 25

import package statement not working

I am trying to import a package into another project. Just to be clear, I am using NetBeans and all my projects are located in the same folder. So, I have a package called createobjectdemo, and I am trying to import it into another project called valueOfDemo. The statement that I am using is: import createobjectdemo.*; This line invokes the error: package createobjectdemo does not exit. But... it does exist! It is located in the same folder hosting all my Java projects. I have it open right now in the NetBeans editor window.

Why can't I access the package? Do I have to provide a specific path? Sorry, this has got to be really simple, but I can't figure it out. Thanks a lot.

Upvotes: 0

Views: 1407

Answers (2)

user1134181
user1134181

Reputation:

You need to make .jar file of java files which you want to use in another project and add your project in menu Libraries to another project.

Upvotes: 1

Menachem
Menachem

Reputation: 941

Not knowing exactly how your projects are set up makes it difficult to answer your question. I imagine your setup is something like the following:

projects/
  project_1/
    src/        // Or maybe you have src/ and classes/ in the same directory
      createobjectdemo/
        CreateObject.java
    classes/
      createobjectdemo/
        CreateObject.class
  project_2/
    src
      valueOfDemo/
        ValueOf.java
    classes/
      valueOfDemo/
        ValueOf.class

So, the package exists but it's not in your class path. You can tell NetBeans (I have no experience with it, but I'm sure you can) that you want project1's classes directory added to your build path, or possibly just add the first project to the build path, and NetBeans can figure out where to find the compiled classes.

Remember, each file in a package must declare that's it's in that package; otherwise, it just happens to be in that directory but (if the directory is on the classpath), it's in the "default" package, and can't be imported by package name.

Upvotes: 0

Related Questions