Create a java file in eclipse

How To Create JAR File In Java Using Eclipse

Hello coders, welcome to my new tutorial, How To Create JAR File In Java Using Eclipse. In this tutorial, you will learn to create normal jar files as well as executable jar files using eclipse IDE, and you will also learn the basics of jar files and why do we need to use jar files. So let’s get started

How To Create JAR File In Java Using Eclipse

Creating a jar file in Java using Eclipse IDE is very easy. It will take some simple steps to create a jar file. I will explain them in a detailed way so that you can understand it very well. But before going further to our main topic – How to create a Java JAR file? We have to know about jar files and why do we use them.

So let’s continue this tutorial .

What is jar file?

  • Jar stands for Java-archive, which is a compressed format of a compiled Java project.
  • It is a file format based on the popular ZIP file format and is used for aggregating many files into one.
  • In other words, a JAR file is a file that contains a compressed version of .class files, metadata and some other resources.
  • JAR files end with the .jar extension.

Types Of JAR Files

  • Library JAR (normal JAR) files : Normal JAR files are the collection of compiled Java classes.
  • Executable JAR files : Executable JAR files are also the collection of compiled Java classes but with the main class. To create an executable jar file, we must have to specify the Main Class in the manifest file.

We will discuss all the steps of creating normal and executable jar file in further section. So keep reading till the end.

Why do We Need to Use Jar Files?

  • If we are working on multiple projects and there is one feature which is common or used in all projects, then we can use a jar file.
  • It is used to distribute Java codes/libraries for use by other projects.
  • We can also use the jar file to execute the Java program directly without any IDEs.
  • The JAR format also supports compression, which reduces the size of the file and improves download time further.
  • We can use jar files for tasks such as lossless data compression, archiving, decompression, and archive unpacking.
Читайте также:  Java for collection remove

Now let’s start our main topic, how to create jar file from Java project in eclipse.

How To Create Library JAR (normal JAR) File In Java Using Eclipse

So here are the steps which are followed to create jar file in eclipse. First of all, open your eclipse workspace and then follow the following steps.

Step 1. Select and right-click on your project name in the Project Explorer view and then select the Export option.

How To Create JAR File In Java Using Eclipse

Step 2. Expand the Java folder, select the JAR file, and click on the Next button.

How To Create JAR File In Java Using Eclipse

Step 3. Now you are in JAR File Specification window.

  • Here Under the section, Select resources to export , select the source and resources you want to export into your JAR file.

How To Create JAR File In Java Using Eclipse

  • Now under section Select the export destination,specify the location where you want to export your jar file.
  • And under section Options, check the check box of compress the contents of the jar file.
  • Then click on the next button, it will take you to the JAR Packaging Option window.

How To Create JAR File In Java Using Eclipse

Step 4. Now you are in JAR Packaging Option window.

  • Here under section Select options for the JAR export, check both checkboxes. You can see it in below screenshot.
  • Now check the option Save the description of this JAR in the workspaceand click Browsebutton to specify the location where you want to save the description of this JAR file so that you can use this file to create JAR file whenever you want to create a new JAR file.
  • If you make JAR description file then you will not have to follow all the above steps to create a new JAR file every time.

  • Now click on the Finish button, and our jar file has been created. Let’s see in the below image.

  • You can see our HelloWorld jar file has been created.

How To Create Executable JAR File In Java Using Eclipse

To create executable jar file, we have to follow the same steps as we followed to create a normal jar file except for one more step.

  • In JAR Packaging Option window, click next button, it will open JAR Manifest Specification window. This will generate the manifest file for jar file.
  • Here, under section Specify the manifest, check Generate the manifest file option. Now under section Seal contents, check the Seal the JAR option.
  • Now under section Select the class of the application entry point, you have to specify your main class.

Specifying main class is important to make your JAR an executable JAR file because as we all know the main method is the entry point of any Java application.

You can see them in below screenshot. Then click on the finish button, it will create executable jar file in the specified location.

Читайте также:  Check php module version

So guys, I am wrapping up the How To Create JAR File In Java Using Eclipse tutorial here. I hope you have found this detailed guide with screenshots very helpful. But anyway, if you have confusion in any steps, feel free to ask your queries in the comment box. If you like, make sure to SHARE this tutorial with your Java fellows. THANKS 🙂

People are also Reading…..

  • How to Connect MySQL Database in Java Using Eclipse
  • Menu Driven Program in Java Using Switch Case
  • How to Create Calculator in Java Swing
  • How to Create Tic Tac Toe Game in Java
  • How to Create Login Form in Java Swing
  • Registration Form In Java with Database Connectivity
  • How to Create Splash Screen In Java
  • How to Create Mp3 Player in Java
  • How to Connect MySQL Database in Java Using NetBeans
  • 11 Best Site to Learn Java Online for Free

Источник

How to create Java Application with Eclipse

In this tutorial will create our first Java application using Eclipse. If you need help to properly configure your PC before start you can follow How to prepare a Windows Java development machine tutorial.

Note: This tutorial applies to Eclipse running on all operating systems: Windows, Linux, Mac Os.

Step1 — Create new Java Project

  • From Eclipse select the menu item File > New > Java Project
  • Choose the project name. e.g. JavaApp

How to create Java Application with Eclipse - /images/EclipseJavaApp.png

Step2 — Adding a dependency

To add a dependency I will use as example MySQL JDBC Driver. Download the zip archive and unzip it in our project folder (I will use a lib folder).

    One is to right click on the files from package explorer ->Build Path ->Add to Build Path.

How to create Java Application with Eclipse - /images/EclipseJavaAppDependency1.png

Right-click on the project name in Package Explorer->select Properties ->Java Build Path ->Libraries -> Add JARs -> Navigate to the library and click OK.

How to create Java Application with Eclipse - /images/EclipseJavaAppPropertiesDependencies.png

More advanced way (and the proper way) to add the dependency is first to create a variable and then to attach it to the project. Using this method the variable can be use to multiple projects, so you need to set up only once.

How to create Java Application with Eclipse - /images/EclipeJavaAppVariable.png

Step3 — Write a Java class

  • Inside Eclipse select the menu item File > New > Class.
  • Type the name and the package and click Finish.

How to create Java Application with Eclipse - /images/EclipseJavaAppNewClass.png

I will edit the class to look like this.

package com.admfactory.app; public class JavaApp < public static void main(String[] args) < System.out.println("Hello World! This is my first Java Application!"); >> 

It’s just a simple application to output a text.

Step4 — Run the program from Eclipse

  • select from menu Run ->Run.
  • from Run As window select Java Application and click Ok.

You can see in the console the text we are expecting.

Hello World! This is my first Java Application!
  • select JavaApp.java file and right-click on it;
  • select Run As ->Java Application.
  • from Run As window select Java Application and click Ok.

You can see the same result as first option. Note: the Run As windows will appear only once as the run configuration was is already created.

Step5 — Run the program from command line

To run the program from command line will use command prompt/terminal and navigate to the project root folder. if you need help on how to navigate you can check simple commands from Windows basics commands and tools tutorial. Cd commands applies to all operating systems.

Читайте также:  Path from url php

Because Eclipse already compiled the Java code navigate to the project bin folder and run the command

java com.admfactory.app.JavaApp

and you can see our «Hello World. » message.

Conclusions

As you can see is not complicated to create your first application. Any comments and suggestions are welcome.

References

Источник

Create a java file in eclipse

Creating a jar File in Eclipse

This page shows you how to create a jar file (java archive) using the Eclipse IDE. The instructions are for bundling the source code (.java files) into a jar. These instructions do not cover how to make an executable jar.

To create a new JAR file in the workbench:

    Either from the context menu or from the menu bar’s File menu, select Export.

IMPORTANT: In this example the project keeps the source code in a folder named src. Your project may have a different set up. Be sure you expand the tree to show the default package and that the .java files are checked on the right. Alternatively deselect the option that says Export generated class files and resources and select the option that says Export Java source files and resources. For CS307 we want the .java files. We do not want or need the .class files. If you do not turn in your source files, the .java files, your assignment grade will be 0.

If there are other files or resources you want to include they must be in a an open project. Browse to their location via the directory tree on the left and ensure the file or resource is checked in the window on the right. In the example below we are including a file named OtherFile.txt which is located in the A3 directory.

  • Click Finish to create the JAR file immediately.
  • Click Next to use the JAR Packaging Options page to set advanced options, create a JAR description, or change the default manifest.

One easy way of checking if the jar has the correct files is to rename it with a .zip extension. Then use whatever you zip program is to look at the files inside. (This may not work depending on how your system is set up.)

Open up with zip program / utility.

In the above image you can see that A3.jar (renamed to A3.zip) contains 2 files MathMatrix.java and MathMatrixTester.java. Realize you may have multiple copies of those files on your computer. You can unzip the file (in a location different from where the files were originally located!!) and open the .java files to ensure they are correct or compare the size of the files to the size of the originals. Alternatively you can unjar the jar file, again in a different location that the original .java files, and check the files.

Источник

Оцените статью