简体   繁体   中英

How to build a jar library

I'm trying to build a library with netbeans, I'm building so: New project...Java...Java application:

package somma;

public class Somma {

   public static int somma(int a, int b) {
      int s = a + b;
      return s;

   }
}

With this main

package somma;

public class Main {

   public static void main(String[] args) {
      int a = 1;
      int b = 2;
      int s = Somma.somma(a, b);
      System.out.println(s);

   }

}

After that, right click on the project...properties...Buid...Packaging....click on the project again and clean and build. Now I created a Somma.jar, for to try the new library I build an exmple project:

package uselibrary;

import static somma.Somma.somma;

public class UseLibrary {
   public static void main(String[] args) {
      int a = 1;
      int b = 2;
      int s = somma(a, b);
      System.out.println(s);
   }
}

Run correctly, but there is a problem, when I import the library, I would like to avoid this name import static somma.Somma.somma; I would like to change with this name import somma; How can I do that ?

Okay try this edit

package somma;

public class Somma {

   public int somma(int a, int b) {
      int s = a + b;
      return s;

   }
}

And

package uselibrary;

import somma.Somma;

public class useLibrary {
   public static void main(String[] args) {
      Somma som = new Somma();
      int a = 1;
      int b = 2;
      int s = som.somma(a, b);
      System.out.println(s);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM