简体   繁体   中英

Exporting own .jar file and importing it in another project

I have been looking for in a los of threads but I am not able to achieve this with the advices people give. I come from .NET and Visual Studio, so its my first time working with Eclipse and Java. I show you the steps I am following to export and import my own library, so you can tell me what I am doing wrong because I don't know what is the problem.

It is an example of a calculator, the library just have operations +, -, * and /, and I am trying to make a .jar file (.dll as i know it in .NET) to use it in other project where I just put two numbers. Steps:

  1. I have created a new Java Project.
  2. Inside of this Java Project I have created a package.
  3. Inside of this package I have created a Class.
  4. This is the code of the Class

    package LibreriaCalculadora; public class Operaciones {

    public int sumar(int x, int y) { return x + y; }

    public int restar(int x, int y) { return x - y; }

    public int multiplicar(int x, int y) { return x * y; }

    public int dividir(int x, int y) { if (y > 0) return x / y; else { System.out.println("El divisor no puede ser 0"); return 0; } } }

Ok, now I proceed to export this Class as a .jar file following this steps:

  1. Right click on project - Export.

  2. Export wizard window: I choose Java - Jar File.

  3. I choose the Java Project and mark the files that are in it to export it. Also I check the options below as many tutorials say. These are the pictures

文件检查.classpath和.project

类文件也被检查

After this step, I can see in the path I've chosen the file Operaciones.jar: Operaciones.jar文件

Ok now the Import:

  1. I create a Java Project.

  2. I create a package inside this Java Project.

  3. I create a Class inside of this package.

  4. Right click on the Java Project and choose Import.

  5. In the Import wizard I choose General - Archive File.

  6. In the Next step, I choose the .jar file from where it was saved before. It seems that all files I need are included. Pics below: 导入.jar 1

导入.jar 2

  1. Click Finish.

Now this is what I see in the Package Explorer, inside the new Java Project where I want to use my library: 文件夹LibreriaCalculadora和META-INF已创建

Now the code of this Java Project where I want to use the .jar library:

package LibreriaCalculadoraDemo;

import java.util.Scanner;

import Operaciones.*;

public class LibreriaCalculadoraDemo {

public static void main(String[] args) {
    int x;
    int y;
    Scanner scanner = new Scanner(System.in);

    System.out.println("Inserta el primer operando:");
    x = scanner.nextInt();
    System.out.println("Inserta el segundo operando:");
    y = scanner.nextInt();


}

}

After all of this steps, I am not able to see what is wrong when I write import Operaciones.*; . I have tried also import Operaciones.jar , import Operaciones.java and import Operaciones.class . But nothing.. I show you a picture of what I see: 代码错误

Can you please tell me if I am missing some steps or something like that? If needed, I have checked in JRE System Library folder of the project and the imported library neither is there..

Thanks!

what I noticed is the class file Operaciones has a package in your file so you should try giving the name of the package with the import statement. The syntax is import packagename.classname; or import packagename.*;

I would rather suggest you to create maven project and use its feature to create the jar file . You can follow below this tutorial.

Finally I've found what the problem was.

First, I have to create the .jar file as I mentioned at the beginning of the question.

The thing is about the Import steps. Doing the following I succeed importing the .jar:

  1. I create a new Java Project called DemoCalc4. This creates in the Package Explorer the project and two elements inside of it: the JRE System Library, and the src folder. Image below: Java项目创建

  2. Right click on the src folder, and create a new Package. And on the Package, right click and create a new class, where the main code is going to be written. Here is where the functions of the imported .jar will be used. In my example, I just want to write two numbers and make some arithmetical operations with them. Image below: 主班创建

  3. Right click on the src folder and click Import. Select Archive File from the wizard and select the .jar file. This will create a package inside the Java Project folder called as the .jar Java Project package. At this moment it is possible to import the class method inside the .jar file by calling the Import statement inside the package of the main program, create an object of the imported .jar type and use its methods. Image below: 主类的代码,使用从.jar文件导入的方法

Here is the final code of the main class:

package DemoCalc4;
import java.util.Scanner;
import LibreriaCalculadora.*;
public class DemoCalc4 {
    public static void main(String[] args) {
        int x;
        int y;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Inserta el primer operando:");
        x = scanner.nextInt();
        System.out.println("Inserta el segundo operando:");
        y = scanner.nextInt();
        Operaciones op = new Operaciones();
        System.out.println("Suma: " + op.sumar(x, y));
        System.out.println("Resta: " + op.restar(x, y));
        System.out.println("Division: " + op.dividir(x, y));
        System.out.println("Multiplicacion: " + op.multiplicar(x, y));
    }
}

And this is the result after execute the main program:

Inserta el primer operando:
5
Inserta el segundo operando:
5
Suma: 10
Resta: 0
Division: 1
Multiplicacion: 25

I hope this is useful for someone. Sorry if my question was not very clear and thanks for all the comments and suggestions made, was really useful for me!

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