Exporting own .jar file and importing it in another project

how to export jar file in eclipse with libraries
how to edit jar file in eclipse
how to export java file from eclipse
how to open source code of jar file in eclipse
how to import executable jar file in eclipse
we can move an already existing project in eclipse to another location by compressing it
create project from jar file in eclipse
how to export eclipse project to zip

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

After this step, I can see in the path I've chosen the file 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:

  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:

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.*;

Importing and Exporting Projects as JAR Files, In Package Explorer, left-click on the project you want to export. 3. When the Export dialog box pops up, expand Java and click on JAR file. Click Next. 5. Another Import dialog box opens and shows you what's in your JAR file. Ideally, you  All methods use an export settings file to define how the export is executed and which files, folders, projects, or system resources to include in the generated configuration JAR file. The export tool runs in two phases, load and export, and each phase is configured in an export settings file.

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

Use a JAR file in another project, Non-runnable JAR files can be imported into other projects so you libraries with other developers or recycle them in your own code. In this video, see how to export JAR files and how to import and use them in other projects. Select Import from the File menu. In the Import wizard, click to expand the Mule folder, select Anypoint Studio Project from File System, and click Next. In the Project Root field, click the ellipses (… ) button to explore your local drive, and select the root folder for the Mule project that you want to import.

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:

  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:

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!

Exporting Jar Files, Learn how to package your Java program so other people can run it. So far, we​'ve been writing, compiling, and running code on our own computer. To export your project as a .jar file, right-click the project name, and then click the Export Exporting and Importing Projects in Eclipse Here is how I usually move or copy a project to another machine: Find the project place on my hard disk. Export Project as Archive File.

How to export extension classes as a JAR file, To create a manifest file in the Eclipse IDE, follow these steps: Right-click the project, click New, and click Other as shown in the following illustration: The New​  In both Netbeans and Eclipse you can have packages in the same project. If so, the IDE will create a classpath that includes the two packages. If you have the Java files in different projects, then you need to tell the IDE that one project is included in the other. In Netbeans you do this in the Project Properties (see attached image).

Creating a jar File in Eclipse, Either from the context menu or from the menu bar's File menu, select Export. other files or resources you want to include they must be in a an open project. After you have confirmed the code works as expected, you need to export the Java project to a runnable JAR file so that it can be run by AlertSite. To export your project, right-click it and select Export. Select Java > Runnable JAR file as the export destination and click Next.

Importing and Exporting Resources and Configurations, You can also import and export from Fusion Middleware Control Console. How to Locate Services · Working with the Project and Folder Definition a configuration JAR file that you can then import into a different instance of You can use it as a basis for your own script, but be sure to check paths and file names against  How to Add or Import Jar in Eclipse Project. 1. First of all copy all the jars that you want to import. 2. Now paste them inside lib folder of your project. Web Project: For web project you can find the lib folder inside YourProjectFolder > WebContent > WEB-INF > lib. If it is not present there just create lib folder.

Comments
  • Hey, first: please edit your question to display your code more clearly. Second: avoid posting pictures, instead, write down errors and warnings you receive. Third: I would recommend using a build tool to manage dependencies: ant, maven, or gradle should work. As for what you’re doing, I believe the problem has to do with not using the full path (package name followed by class name)
  • Thanks @RoyShahaf! First, I don't know why at the top it doesn't shows as code.I did the same as the code below.. I tried several times but no success. Maybe a moderator can help me. About pics, I thought is more easy to understand my steps with them because here I am doing something wrong or with the code or with the steps =(.. About build tools, I've never used any, but Im going to read about them thanks! And about the problem I've tried 'package.classname', 'package.*', 'package.classname.*', the same without package, and also 'Operaciones.jar'.. but no success, I don't understand why!! =(
  • Hey, regarding the code I’ve found that in eclipse what works is ctrl a (select all), tab (to indent), ctrl c (copy) then in stackoverflow you press the code button and ctrl v (paste). Regarding the pictures your reasoning is good but pictures can be very difficult to view on some screens while text is normally easier. About your problem I would suggest learning to use maven or gradle and avoid using the manual method.
  • @RoyShahaf finally I've found what the problem was. Until the creation of the .jar file, everything is right. The next steps are the following: 1st, I create a new Java Project. 2nd, right click in the package and create a new Class. 3rd, right click on the package and Import, and select the Archive File option. Click Next and choose the .jar file created before. This creates a package inside the project with the name given to the project where the .class of the library was created. After, I have to write the import statement inside the package of the new project. I will update the question!
  • Thanks @HarshKumrawat! I tried using 'LibreriaCalculadora', 'LibreriaCalculadora.Operaciones', 'LibreriaCalculadora.Operaciones.*', 'Operaciones.*', 'Operaciones.java', 'Operaciones.class', 'Operaciones.jar'... But no success. Maybe could be another problem? About the importing and exporting you think is everything right? Thank you very much!