0

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?

  • I think you need to clean and build your project. This does a "mvn clean install" If you ask the question on the which choice to make, Ant and Maven, the question will be quickly closed as it's opinion based. 1)The basic fact is that if you want to share your NB Ant project with other developers that use Eclipse or IntelliJ,they will quickly give you the stink eye as they will need to jump through certain hoops to make it work. 2)Also if you want to use one of the millions of Maven dependencies out there, NB can help you with Maven, but in Ant based project it's up to you to download. – rjdkolb Jan 08 '16 at 06:06
  • FYI first time i hear of Maven and Ant :D I mean i've read Ant befor and Maven but never confronted myself with that topic. Seems like it's time to do so. Thanks for your help! – Squirrel in training Jan 08 '16 at 08:03

4 Answers4

1

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
enter image description here

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");
    }
}

enter image description here

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.

enter image description here

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();
    }

}
Duffydake
  • 907
  • 7
  • 18
  • Aswell in my library, do I have to declare my Methods "static"? Or is it possible to use "non-static" Methods in a "static" main Method? Thanks in advance and have a nice day! – Squirrel in training Jan 08 '16 at 07:50
  • @MilesA. Yes you can have "non-static" method and use it in your main or anywhere else : E.g : `MyClass myclass = new MyClass();` and then `myclass.myNonStaticFunction();` – Duffydake Jan 08 '16 at 11:11
0

Right click on libraries ( in your project) then add your library..

FuSsA
  • 3,735
  • 5
  • 35
  • 58
  • Already did that, but how can i use that in my code from that poitn onward? E.g: I have the library "MyLibrary" with the class "MyClass" with the Method "MyMethod". Now i've added the library to my project but how can i access "MyMethod"? – Squirrel in training Jan 07 '16 at 13:52
  • you can use it like you use any other class ( add the import ) – FuSsA Jan 07 '16 at 18:01
  • A that didnt work like that.. Anyway the trick was to create a package in the library and move your class in that package to acces it. Aswell it was required to set them to Static. – Squirrel in training Jan 08 '16 at 08:00
0

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

Community
  • 1
  • 1
Do Re
  • 1,060
  • 3
  • 11
  • 23
0

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)

enter image description here

rjdkolb
  • 8,217
  • 8
  • 57
  • 77
  • You know `ant`is a great tool too, so `maven` is not a requirement, it's a matter of taste. You can put your lib in "Libraries list" and just add it to your project when it's needed. – Duffydake Jan 07 '16 at 12:59
  • @Duffydake, true. I generally advice against it so the project can easily be moved between IDEs. I know Ant has some compatibility, but Maven is a first class citizen in NB. So yes, personal preference :) – rjdkolb Jan 07 '16 at 13:04
  • @Duffydake, I modified the answer to be a bit more neutral between Maven and Ant. – rjdkolb Jan 07 '16 at 13:23
  • 1
    I've eddited my question to fit your mavel client suggestion – Squirrel in training Jan 07 '16 at 13:45