How to run a JAR file in Java?

0 votes
I have created a JAR file. But I am not able to run it. How can I execute it?
Jun 1, 2018 in Java by Sushmita
• 6,890 points
4,095 views

1 answer to this question.

0 votes

The command given below will help you to run the JAR file.

java -classpath Predit.jar your.package.name.MainClass

We should specify a main class in the JAR file Manifest.

For Example: If you have a Demo class.

public class Demo
{
    public static void main(String[] args)
    {
        System.out.println("Hello world. Happy Learning");
    }
}


manifest.mf will be:

Manifest-version: 1.0
Main-Class: Demo
Now, you can run the code.
javac Demo.java
jar cfm test.jar manifest.mf Demo.class
java -jar demo.jar

answered Jun 1, 2018 by Parth
• 4,640 points

Related Questions In Java

0 votes
1 answer

How to run a .java file from command line passing arguments in Maven?

Hello @kartik, You could run: mvn exec:exec -Dexec.args="arg1". This will ...READ MORE

answered Jun 3 in Java by Niroj
• 67,020 points
1,190 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 6, 2018 by Omkar 31,569 views
0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,890 points
1,301 views
+2 votes
10 answers

How to import a jar file in Eclipse?

To import jar file in your Eclipse ...READ MORE

answered May 15, 2018 in Java by sharth
• 3,370 points
172,742 views
0 votes
1 answer

Retrieving the path of a running jar file

Its quite simple. Try using the below ...READ MORE

answered May 25, 2018 in Java by geek.erkami
• 2,680 points
5,836 views
0 votes
1 answer

How to run the JAR files in windows?

Following are the steps to run the ...READ MORE

answered May 28, 2018 in Java by Parth
• 4,640 points
253 views
+2 votes
11 answers

Unable to execute jar- file: “no main manifest attribute”

add a jar/manifest/attributes setting like this: apply plugin: ...READ MORE

answered Nov 27, 2018 in Java by krishti
100,307 views
0 votes
2 answers

Include all the jars in a directory within the Java classpath

Just use lib/*   not lib/*.jar   That's the ...READ MORE

answered Apr 6, 2019 in Java by anonymous
7,391 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,200 points
365 views
0 votes
2 answers

How to read a text file in Java?

You can use Scanner class to read ...READ MORE

answered Aug 9, 2018 in Java by Parth
• 4,640 points
151 views