Image of How to run a jar file from command line

Table of Contents

  • Introduction
  • i. Jars with predefined main form
  • 2. Jars without predefined main class
  • 3. Dependable jars
  • 4. Unsupported major.minor error
  • Summary
  • Side by side Steps

Introduction

So you got an executable jar and you are wondering how to run information technology! . In this tutorial nosotros define the unlike ways to run an executable jar through the windows command line. In case yous don't know what's an executable jar, refer to our previous tutorial about creating executable jars in java.

Throughout this tutorial, we consider that you've already installed java on your machine, and yous set up properly the java surround variables.

1. Jars with predefined main course

Every executable jar should agree a MANIFEST.MF file which holds important information almost the jar itself, if yous're lucky enough you get a jar with a predefined principal class in MANIFEST.MF

In order to check if the master grade is already divers:

extract your jar using whatsoever archive tool eastward.k. winrar, vii-zip => open MANIFEST.MF file under META-INF => so bank check whether in that location exists an attribute called Chief-Class and is set properly.

In lodge to run the jar, only type the post-obit command in the windows console:

          coffee -jar <name-of-jar-with-extension>                  

eg

          java -jar executable.jar                  

2. Jars without predefined main form

Some developers forget to define the main class inside the manifest file while creating the executable jar, when you lot get this kind of jar and try to run the to a higher place control, y'all become an mistake message similar : 'no master manifest attrbute'.

In society to run this kind of jar, y'all must know the full path of the main class and use the following command:

          java -cp <jar-file-proper noun-with-extension> <full-path-of-primary-class>                  

due east.thousand.

          java -cp executable.jar com.programmer.gate.HelloWorld                  

If yous don't want to write the path of the main class each fourth dimension you run the jar, you lot tin update the manifest file of the jar and add your main course there. Refer to this commodity on how to make an existing jar file executable.

3. Dependable jars

A jar is dependable if it tin can't run independently without the existence of other jars, in social club to run this kind of jar y'all must explicitly specify its classpath either through control line or via manifest file.

Suppose that you have a folder named lib which exists inside the project beside META-INF and holds third-political party libraries used by your application, your jar wouldn't work without including these dependencies.

Option 1: Specify the dependencies while running the jar in the control line:

          java -cp <proper noun-of-jar-with-extension>;<path-of-dependency-folder>/*;. <path-of-principal-course>                  

due east.g.

          java -cp executable.jar;lib/*;. com.developer.gate.HelloWorld                  

Choice 2: Specify the classpath of the jar in the manifest file:

Normally the developers who create the jar should specify its dependencies inside the manifest, just in case they forgot and you lot don't have other way to run the jar, just add the following attribute just earlier the primary class attribute in MANIFEST.MF:

          Course-Path: . lib/*                  

then utilise the typical command:

          java -jar executable.jar                  

4. Unsupported major.small error

In some cases, you lot got 'Unsupported major.modest version' error when running an executable jar. This error ways that the version of the JDK used for developing the jar is greater than the JRE version currently used to run it. Beware that java is backward compatible, then always make sure that the JRE version of running environment is greater than or equal to the JDK version used for development.

Summary

In this article we innovate the well-nigh mutual way for structuring a maintainable web service, our instance tin be used as a template for creating any web service using object oriented language. The of import gene when structuring an application is to divide it into several modules/components and to define how these components interact between each other.

Next Steps

If you're interested in learning more about the nuts of Coffee, coding, and software evolution, check out our Coding Essentials Guidebook for Developers, where we encompass the essential languages, concepts, and tools that yous'll need to become a professional programmer.

Thanks and happy coding! We hope you enjoyed this article. If you lot take any questions or comments, experience gratis to reach out to jacob@initialcommit.io.

Terminal Notes