Pages

How to create a executable Jar file

How to Create a executable Jar file in intellij IDEA?
  • Before creating a jar file first know about JAR.
  • While developing a web application we have multiple java files.
  • Now we will create a jar file contains .class files.
Steps:
  • Create a sample java file in your project with class name as JarDemo like below.
  • Below program we are printing a statement.
package com.company;

public class JarDemo {
    public static void main(String args[]) {
        System.out.println("This class will put in jar file");
    }
}
  • After creating a java file make sure program should be run successfully like below.
Sample Java file
Jar File
  • Once program run successfully,lets create a Jar file.
  • For Jar File creation, follow given steps:
   1.Go to File > Project Structure > Artifacts > Click '+' > select Jar>select 'From modules with dependencies' > Create JAR from Modules popup will display.
2. Select Main class > Select any java file > Click OK > Again Click OK
3. After selecting class file will get information like below:
4.By default jar will create with project name and if you want rename you
 can do it (Simply right click Session.jar and rename it).I rename it
 to Demojar like below:
5. After rename your jar file name, click OK.Then your Jar file created in project with group of class files,see below:
Check in artifacts folder where jar file created.
6.Once successfully created the jar,Go to Build > Build Artifacts > Select your required jar and select Build
7. After build success,now run your JAR file.
8. For run Jar file, just right click on Jar file > select Run like below 

9. We can see output.So a Jar file created with group of class files.

Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.