How to Import Classes in Java NetBeans

  • Java
  • Thread starter elbeasto
  • Start date
  • Tags
    Java
In summary, the conversation discusses the use of import in Java to reference classes in a package. The individual is new to NetBeans and is unsure of how to use import to reference a package. It is suggested to read up on Java packages and to utilize the "Projects" window in NetBeans to manage libraries. The "Add Project" option is recommended for updating libraries in dependent projects. The "Add Jar" option can be used for a one-time copy of a library, and the "Add Library" option allows for searching through global packages. NetBeans also has time-saving features for coding.
  • #1
elbeasto
33
0
I have a rather large list of classes that are have created and rather than compiling the classes that are needed with the main code I would rather use import. The problem is I do not know how to make that reference.

My end goal is to have a package called someClass and a file named
c:\myClasses\someClass\someClass.java
And within my main code, reference that file by
import someClass;

I am new to netbeans so I am not sure if something like that needs to be set in netbeans or in some environment variable like classpath. I have already tried setting my classpath however I did not anticipate it working and it didn't.

I am willing to try anything including dropping netbeans and using another IDE if someone knows how to do this in another program like eclipse or something.

Thanks for any help!
 
Technology news on Phys.org
  • #2
I have some trouble understand what are asking about precisely, but it seems like you may want to read up on how Java packages works. If you don't have a good textbook on Java you should be able to get the basics on the normal Java language tutorial on packages, http://download.oracle.com/javase/tutorial/java/package/index.html
 
  • #3
ok I will rephrase... I was kind of in a hurry when I posted it.

Lets say I have a bunch of classes within a package

package someClass;

public class classOne() {
}

package someClass;
public class classTwo() {
}

etc..

Days after making this package I decide to write another program that needs to use this package. So I say

import someClass;

However this does not work because I have not told NetBeans where this package is located. So my question in a nutshell I guess is where do I put this package at so that netbeans will recognize it as a valid package when I try to import it.
 
  • #4
It still sounds like you could benefit a lot from reading the tutorial I linked to. Given that you struggle with this issue you probably also want to follow general Java and/or Netbeans introductions or tutorials, like for instance http://java.sun.com/developer/onlineTraining/tools/netbeans_part1/.

Regarding your direct question: if you have the Java class with the fully qualified name "my.package.MyClass" on your class path, you can import it in the Java source in another package by saying "import my.package.MyClass;". Note, that you can always refer to a class by its fully qualified name; importing is only to allow you to refer to it without a package name.

By the way, you should probably not name a package "someClass" as a package is not a class, and it is also strongly advised not to use capital letters in package names.
 
  • #5
Thanks Filip. You are right I would never name a package "someClass" as a package is not a class as you say. I was more or less sticking with my rushed original post names where I had named the package "someClass".

Thanks again for your help Filip.
 
  • #6
I've been playing with Netbeans a lot lately. It has some nice features for managing libraries. Look in the "Projects" window. If you don't see the "Projects" window then click on the "Window" menu at the top and select the "Projects" button to make the "Projects" window reappear. You'll see some names for the projects that you have created with NetBeans. Click the plus arrow next to the one you wish to add a library to. Depending on what type of project it is, you should see some folders like "Source packages", "Test Units", and "Libraries". Right click on the libraries folder. You should see three options.

1) "Add Project" You can select another project to be a library for the current project. This option is good because if you update the library then dependent projects will be updated too.

2) "Add Jar" This option will copy any jar on your computer and make it available as a library to your project. It's a one shot deal. If the original library is updated then Netbeans won't know that the copy in the current project is out of date (you'll have to delete the lib and re-add it).3) "Add Library" This let's you search through Netbeans' global packages for an official NetBeans library (and some other stuff) that you may want to use. It's useful for making applications that use the NetBeans tech. You can add your own libraries to the global list if you want. That way you can use the quick search that can find packages by locating a specific class name inside any globally registered packages.

When Netbeans builds a project into a zip distribution, it will pack up all your project jars and also place all the needed libraries in a properly linked /lib folder so you have everything that you need to run the app anywhere.

edit: You still have to use "import SomePackage.SomeClass;" to use the library in a source file. NetBeans can automatically generate imports (and remove unused imports) by pressing CTRL+SHIFT+I. Eclipse can do it too but I can't remember the shortcut.

Also, go through NetBeans basic coding tutorials. It has plenty of time saving features that you would likely never notice on your own.
 
Last edited:
  • #7
Thanks zomgineer! Thats what I have been looking for! I had actually just stopped using netbeans and went back to command line but I am going to give netbeans another try now.

Thanks again!
 

Related to How to Import Classes in Java NetBeans

1. How do I import classes in Java NetBeans?

To import classes in Java NetBeans, you can use the "import" keyword followed by the name of the class you want to import. For example, if you want to import the "Scanner" class, you would use the following code:
import java.util.Scanner;

2. Can I import multiple classes at once in Java NetBeans?

Yes, you can import multiple classes at once in Java NetBeans by separating each class with a comma. For example:
import java.util.Scanner, java.util.ArrayList;

3. How do I import classes from a different package in Java NetBeans?

To import classes from a different package in Java NetBeans, you need to use the fully qualified name of the class. This includes the package name followed by a dot and then the class name. For example:
import java.util.ArrayList;
import com.example.MyClass;

4. Can I import all classes from a package in Java NetBeans?

Yes, you can use the "*" wildcard to import all classes from a package in Java NetBeans. For example:
import java.util.*;

5. What should I do if my imported class is not recognized in Java NetBeans?

If your imported class is not recognized in Java NetBeans, it could be because you have not added the necessary JAR files to your project. Make sure to check for any missing dependencies and add them to your project's build path. You can also try cleaning and rebuilding your project to see if that resolves the issue.

Similar threads

  • Programming and Computer Science
Replies
3
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
2
Views
764
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
2
Views
513
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top