Java run jar main method

How to Run a JAR file from Command Prompt — Windows and UNIX [Example]

So you got a JAR file that contains your favorite Java game and you are wondering how to run a Java program from a JAR file? Or you may be simply trying your hand after running the Java program from the command prompt and Eclipse IDE. Anyway, it’s good to know How to execute Java programs from the JAR file, as the JAR file is the most popular way of shipping Java programs. JAR file usually contains all sources and resources in the form of class files, images, sound files, etc.

In order to run a Java program from the JAR file, it must be an executable JAR file i.e. it manifest file inside the META-INF folder must contain a Main-class entry like Main-Class: Hello , which specifies the name of Java class which contains standard main method in Java.

Without Main-class entry, you can not run a JAR file in Java and if you try to run such jar file using jar command you will get the error «Failed to load Main-Class manifest attribute from HelloWorld.jar» .

If you are not very much familiar with JAR files and don’t know How to create an executable JAR, See this step-by-step tutorial on How to create executable JAR in Java. In this tutorial, we will only focus on running a Java program from an executable JAR file.

Running Java program from JAR file in Java

Suppose you have a hello.jar that contains Hello.java class which has the main method as an entry point. When we run this JAR file using the JAR command, JVM will call this main method which is specified in its manifest file as shown below:

By the way, don’t forget to set PATH and Classpath in Java before running the Java program. If classpath is specified in the manifest file then that will be used by JVM and it will ignore the CLASSPATH environment variable, If you are not familiar with these details then see How classpath works Java, a must-read for any beginner in Java.

Once you are sure that Java is installed in your machine, path and classpath are proper, just execute the following command and it will run your Java program from the JAR file itself:

Читайте также:  Background size css in jquery

If you see the error «no main manifest attribute, in Test.jar» it means your JAR file is not a runnable JAR file i.e. it’s the manifest file doesn’t contain the Main-Class attribute.

$ java -jar Test.jar
no main manifest attribute, in Test.jar

In my case MANIFEST.MF file just contains one line:

Once I added the Main-Class attribute it runs just fine as shown below:

$ java -jar Test.jar
before swapping x: 3
before swapping y: 3
after swapping x: 3
after swapping y: 3

Here are nice slides with all steps required to run a JAR file from the command prompt in Windows 8 and UNIX:

How to run a JAR file from command prompt in Windows 8

That’s all about how to run a JAR file in windows and UNIX. Running a JAR file is the simplest way to ship and run Java Program. You can also create a batch file to wrap the actual command, so that next time you can just run the batch file to start your Java program.

If you face any issues related to Java, classpath, and path while running your Java program from the JAR file, please post here. If you don’t know how to create an executable JAR file then see this tutorial. That tutorial gives you step by step guide to making an executable JAR using Eclipse IDE.

Источник

How to Create an Executable JAR file and Use it to Run the Main Method?

To cover basic concept of GRADLE to run the main method of Java program by:

  • Creating an executable jar file of the project
  • Run the main method using executable jar file

Why should we prefer executable Java Archive (JAR) file?

Some of the benefits of using Java Archive (JAR) file are given below.

  1. It can be used as library reference to include the classes.
  2. It is a compiled version, so the process of compiling is not required for executable JAR files.
  3. It is easy to run an executable JAR file.
  4. It is in compressed form and requires less space.

Prerequisites

Before running the Java program, you must have to configure GRADLE and JAVA on your system. Please refer the following link to configure GRADLE: http://webdemo.saksoft.com/360logica/blog/2015/11/how-to-configure-gradle-on-windows-machine.html

Write a simple JAVA program by following the given steps

1. Create a folder named as “GradleWorkspace”.

2. Open Notepad++ and write the Java code as shown in the below image.

open Notepad++ and write the Java code as shown in the below image.

3. Save the Java program file as “GradleDemo.java”.

Create and configure build.gradle

2. Go to File >> New and write the code given below.

This plugin will enable the basic Java build functionality.

apply plugin: ‘java’

3. Now we need to specify the name of the class that contains the main method. For this, add the code given below.

Now we need to specify the name of the class that contains the main method. For this, add the code given below.

Note: If we don’t declare the name of the main method in build.gradle file, then we will get the following error while executing the jar file.

image004

Create execuatble JAR file

Читайте также:  Php самоучитель для чайник

1. Press “Windows + R” button and type ‘cmd’ as shown below.

Press “Windows + R” button and type ‘cmd’ as shown below.

2. Go to the Java file location by typing: cd E:\GradleWorkspace.

Go to the Java file location by typing: cd E:\GradleWorkspace.

3. Enter the command “gradle build”. This command will create the executable JAR file under build/libs folder with the name of root folder (for e.g.: GradleWorkspace.jar).

Enter the command “gradle build”.

This command will create the executable JAR file under build/libs folder with the name of root folder

Run executable JAR file

1. Go to the command prompt and reach root folder/build/libs.

Go to the command prompt and reach root folder/build/libs.

2. Enter the command: java –jar .jar

Источник

Java how to run main method in java

If your entry point is the class, only the main method of that class will be executed (unless you manually execute main methods of other classes). Put them in a package, make sure the directory tree matches the package tree, and use the fully qualified name of the class: Solution 1: You can run static methods as needed Solution 2: Based off your question I assume you want the main function in Exe to essentially run the main function in PackageDemoDriver: I think that’ll provide the functionality you’re after, if your PackageDemo and PackageDemoDriver are in different classes Solution 3: Is this what you’re looking for?

Why doesn’t the main method run?

Your application has one entry point, and that entry point is the single main method that gets executed. If your entry point is the theGame class, only the main method of that class will be executed (unless you manually execute main methods of other classes).

Creating an instance of lineTest class doesn’t cause its main method to be executed.

I’ve made a start below. It looks like you might want to invest some time in following a more basic java tutorial or course to get your basic java knowledge up to speed.

What happens in the code below is that the class theGame has a main entry for the program. The JVM will invoke the main method at the start of your program. From there, it will execute the instructions you give. So most of the times, two main methods do not make sense in a single project. Exception to this rule is if you want to have two separate application entry points two the same program (for instance a command-line application and a GUI application that use the same logic but are controlled differently).

So with the code below, you will have to specify the TheGame class as a main entry point when starting your JVM for this application.

public class TheGame < private final LineTest theBoard; public TheGame() < theBoard = new LineTest(); >public void run() < JFrame frame = new JFrame("Points"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(theBoard); frame.setSize(250, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); >/** * Main entry for the program. Called by JRE. */ public static void main(String[] args) < TheGame instance = new TheGame(); instance.run(); >> 
public class LineTest extends JPanel < public void paintComponent(Graphics g) < super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.red); g2d.drawLine(100, 100, 100, 200); >> 

When a java application is executed, it is executed by invoking the main method on one particular class. This main method will be whichever main method is on the class that was selected to executed.

Читайте также:  Read xml from php file

In your case, you are selecting to execute the main method on the class theGame .

When another class is constructed in the application, the constructor of that class is automatically executed, but the main method on that class is not automatically executed.

Learn Java Programming — Main Method Parameter and Arguments, The main method is the entry point for a class invoked from the java command line tool. In Duration: 6:00

Java Main Method Tutorial — Everything You Need to Know

Full Java Course: https://course.alexlorenlee.com/courses/learn-java-fastIf you want to be a Duration: 9:50

JAVA MAIN METHOD Explained | public static void main(String[] args)

6.15 Different Ways of writing main method in java

When we start JVM by running java command we also provide name of class which contains
Duration: 2:57

How to run the main method of a Java class in a jar? [duplicate]

you can do something like this

java -cp jarName.jar packageName.ClassName argumentsIfAny 
java -cp the\path\to\jarFile.jar SecondLevelJsonCreator 3 "C:/path/filename.xls" 

You should generally avoid putting classes in the default package. Put them in a package, make sure the directory tree matches the package tree, and use the fully qualified name of the class:

java -cp the\path\to\jarFile.jar com.mycompany.myproject.SecondLevelJsonCreator 3 "C:/path/filename.xls" 

How to run a main method method without building the WHOLE, But now instead of running it, it started to build the maven project as a whole. enter image description here. Is there a way to «fix» this? java intellij-idea

How do I run the main method in a different file in Java?

You can run static methods as needed

public static void main(String[] args)

Based off your question I assume you want the main function in Exe to essentially run the main function in PackageDemoDriver:

I think that’ll provide the functionality you’re after, if your PackageDemo and PackageDemoDriver are in different classes

Is this what you’re looking for?

package exercise; import jav.PackageDemoDriver; class Exe < public static void main(String[] args) < PackageDemoDrive.main(args); >> 

What is the difference for run or main function in java class, main method is entry point of a program but run method of Runnable interface is entry

Java 11 doesn’t run main method if the public class is not declared first

The class to be executed is the first top-level class found in the source file. It must contain a declaration of the standard public static void main(String[]) method.

i.e. you need to make public class Chimpanzee as the top-level class as follows:

public class Chimpanzee extends Ape < public Chimpanzee() < super(2); System.out.print("Chimpanzee-"); >public static void main(String[] args) < new Chimpanzee(); >> class Primate < public Primate() < System.out.print("Primate-"); >> class Ape extends Primate < public Ape(int fur) < System.out.print("Ape1-"); >public Ape() < System.out.print("Ape2-"); >> 

Run a Java Main Method in Maven, And we want to execute its main method from the command line via Maven. In order to do this, we can use the exec-maven-plugin. To be more

Источник

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