Reputation: 798
So eryler on I had the brilliant idea to write my own Library with a set of Classes for diffrent Methods that I might use Frequently.
With my new found motivation to be lazy i swiftly created a new Java-Project, to be percicse a Java Class Library.
Now that I have written myself my first Class in this Library and I just can't figure out how to implement(don't know if that is the right word here), the library into my Application.
package pkg4332423423;
import java.util.Scanner;
import StringOperators.*;
public class Main
{
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
}
}
Roughtly I Thought it would work like this.
I have a Package called StringOperators in that Package i have my Class MyStringOperators.
I've Imported the library already through rightclick and Properties.
And my Question is: -Just to sum it up
How the bleeding hell can I Import a Library that I created myself and use all Classes and Methods in that Library in a Application that I am Writing?
and as a Quick Bonus: Is it possible to set up the implementation of that Library per default? So that it is always included?
Thanks in Advance!
Edit:
First of all, Thank you all for your answers!
I've tried the Mavel Client suggestion and this is what it looks like here
Sadly, I can't click on the Open Projects tab. Do you have any other Idea?
And your answer raised another question, why only use the Mavel client?
Upvotes: 0
Views: 3965
Reputation: 927
You don't need to use Maven project, if you prefer default project (Ant), you can do it like this.
File -> New Project -> Java class Library
Right click "Source Packages" (or "default package") -> New -> Java Package : MyLib
Add your class (right click "MyLib" package -> New -> Java Class) : MyClass
MyClass.java :
package Mylib;
public class MyClass {
public static void myFunction() {
System.out.println("This is a function of my lib");
}
}
Clean and build "MyLib project"
Right click on "Libraries" -> Add Library -> Create : MyLib
and add your jar file in "classpath"
Now when you need to use "MyLib" in a project, Right click on "Librairies" -> Add Library and select "MyLib" in the list.
At this point you can use your lib in your project.
MyApp.java :
package myapp;
import MyLib.MyClass;
public class MyApp {
public static void main(String[] args) {
MyClass.myFunction();
}
}
Upvotes: 1
Reputation: 11898
My first rule of Netbeans, is only use Maven projects. So when you create a project select Maven and Java application and never "Java->"Java application". If you are using the second option, this is an Ant style application it's quite similar.
Clean and build your library you want to reuse.
On the project "myspecial-app" find Dependencies and click "Add Dependencies" as below. (myspecial-lib)
Upvotes: 0
Reputation: 896
If I get you right, you want to make your class into a library and use this library in other projects.
To do so, have a look at the answer in this topic
To include your own library, you need to include it in your build path
Upvotes: 0