I have several classes that I use in almost every one of my projects. What, and How, do I create something so the I can easily access them?

I'm use NetBeans for my IDE.


You can use a package that includes all of your essential classes and just import that package into any class where you want to use those classes. So basically, if you wanted to import the popular File class, you would use import java.io.File; but if you wanted to import the whole package you could say import java.io.*;

Assuming these are your classes (not just ones in the Java API) put them all in a jar. Netbeans has a tool for creating one.

I've tried but failed to create a file of common classes that I can import! Here's what I did.
I'm using NetBeans 6.7.1 as my IDE.

1) I created standard Project:
File -> Java:Java Application -> Project Name: common.Code. I Left Create Main Class and Set as Main Project both checked. This created a package named "commoncode" with a main class of CommonCode.

2) I then added a file containing one of the classes I want use as common code.

3) I did a Clean and Build project.

4) In the output, the path for the jar was was listed. I copied the absolute path,including the jar file name, to my CLASSPATH.

5) I restarted NetBeans.

6) I then tried all these imports. None worked.
import common.Code.*;
import CommonCode.*;
import commoncode.*;

So, my friends, what am I doing wrong?

Thanks you very much for your help.

Add JAR/Folder

find folder and indicate your jar file

Thanks. Adding it to the project classpath worked. The thing I don't like is that it looks like I have to manually add it to the classpath of each project I build. How can I have it added automatically?

Also, can I remove it from my System Classpath?

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.