How to execute jar linux

How to Create and Execute a .Jar File in Linux Terminal

A JAR (Java ARchive) is platform-independent file format used to aggregate many Java class files and associated metadata and resources such as text, images, etc, into a single file for distribution.

It allows Java runtimes to efficiently deploy an entire application in one archive file, and provides many benefits such as security, its elements may be compressed, shortening download times, allows for package sealing and versioning, supports portability. It also supports packaging for extensions.

In this article, we will show how to create a simple Java application and bundle it into a JAR file, and demonstrate how to execute a .jar file from the Linux terminal.

To do this, you must have java command line tool installed to launche a Java application, and the -jar flag to execute a program encapsulated in a JAR file. When this flag is used, the specified JAR file is the source of all user classes, and other class path settings are ignored.

How to Create a JAR File in Linux

1. First start by writing a simple Java class with a main method for an application called TecmintApp, for demonstration purpose.

Copy and paste the following code to TecmintApp.java file.

Save the file and close it.

2. Next, we need to compile and pack the class into a JAR file using the javac and jar utilities as shown.

3. Once tecmintapp.jar created, now you can excute the file using java command as shown.

From the output of the above command, we encountered an error. The JVM (Java Virtual Machine) couldn’t find our main manifest attribute, thus it could not locate the main class containing the main method (public static void main (String[] args)).

The JAR file should have a manifest that contains a line in the form Main-Class:classname that defines the class with the main method that serves as our application’s starting point.

4. To fix the above error, we will need to update the JAR file to include a manifest attribute together with our code. Let’s create a MANIFEST.MF file.

Copy and paste the following line to MANIFEST.MF file.

Save the file and let’s add the file MANIFEST.MF to our tecmintapp.jar using following command.

5. Finally, when we executed the JAR file again, it should produce the expected result as shown in the output.

For more information, see the java, javac and jar command man pages.

That’s all! In this short article, we have explained how to create a simple Java application and bundle it into a JAR file, and demonstrated how to execute a .jar file from the terminal. If you have any questions or supplementary ideas to share, use the feedback form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

Читайте также:  Microsoft windows phone dev account

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

How to Run Jar File in Ubuntu Linux

Last updated October 29, 2019 By Abhishek Prakash 19 Comments

Got a Jar file but struggling to execute it? Learn how to run a Jar file in Ubuntu and other Linux distributions.

How to execute Jar file in Ubuntu and other distributions

A Jar file is basically a Java executable file. So you must have Java installed on your system. If you have Java installed in your Ubuntu, you should be able to run it either by double clicking or by selecting from right click options.

The problem here is that most Linux distributions don’t come with Java pre-installed. So please make sure to install Java on Ubuntu. or whichever distribution you are using.

You only need the Java Runtime Environment and you can install it using the command below in Ubuntu-based distributions:

Even with Java installed, sometimes running a Java application is not as straightforward as double-clicking the icon. You might have to go through a few steps.

When I tried to open the .jar file by double clicking, I was presented with the following error:

Fixing this error is very trivial. Right click the .jar file, and open properties.

Now in the properties menu, open the ‘Permissions’ tab. Enable the ‘Allow executing this file as program’ checkbox.

Now, you can simply double click the .jar file to launch the application, AndroMouse in my case. You can see that it works without any problems.

You have to keep in mind that you have to enable the ‘Allow executing as program’ permission for every .jar application you download. It is disabled by default as a security measure. You should enable it only for the applications that you trust and know are safe.

Further troubleshooting Jar file not running on Linux

Even if you have Java Runtime Environment installed and execution permission set, in some older versions of Ubuntu, the Jar file won’t run. Here’s a fix for that.

Right click on the .jar file and choose the option “Open With Other Application“:

Now on the next screen, select the option Show other applications:

In other applications, if you do not find option for Java, install the Java Runtime Environment first. Normally, you should be able to see the option Open With OpenJDK Java x Runtime. Select it and successfully run it.

Enjoy running Jar file on Ubuntu!

I hope this short guide was useful to all of you. Which Java based applications do you use? Do you have any other problems with them? Let me know in the comments below!

Like what you read? Please share it with others.

Источник

Creating and Executing a .jar File in Linux Terminal

JAR – Java Archive. It is like a zip file but for java classes. It combines all the .class files in Java into a single .jar file. It is used to download all java classes on HTTP in a single operation. These can be created using the “jar” CLI tool. It also has an optional META-INF which can include files like –

  • MANIFEST.MF – manifest file is used to define the extension and package-related data.
  • INDEX.LIST – It contains location information for packages defined in an application or extension.
  • x.SF – This is the signature file where ‘x’ is the base file name.
  • x.DSA – This file stores the digital signature of the corresponding signature file.
  • services/ – This directory stores all the service provider configuration files.
Читайте также:  Linux mac os дистрибутивы похожие

The most common and majorly used file is MANIFEST.MF

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

Requirements

Java (JDK + JRE) must be installed. Check by using command –

Create Jar files

Let us consider 4 class files – Class1, Class2, Class3, Class4

Источник

How can I execute a .jar file from the terminal

I know that to execute a file, I use the . command, then the file name with a space between them. But I’m trying to execute a .jar file using the . and it does not work. I went into the properties and marked it as executable and made it run with Java.

Is there a way to execute a file with Java in the Bash Terminal?

I am trying to execute the Minecraft.jar file.

8 Answers 8

The . syntax can only be used to run (by «sourcing») shell scripts.

You’ll need to use the java command to run a .jar file:

If you don’t have java installed, you can fix that by installing the default-jre ¹ package. You can see if you already have java installed by running in a terminal:

[1]: This will install the default openjdk Java runtime. You can use openjdk-8-jre , or openjdk-7-jre , or openjdk-6-jre instead, if you prefer — whichever is available on your version of Ubuntu.

Linux is perfectly capable of running a foreign binary, like a JAR file. This is how Wine works, for example. To run JAR files as executable do the following in a console

Cd to your JAR file and change it to executable (you can also do this through file properties in Nautilus)

Run your jar file just as if it was any other binary executable or shell script

Note: Be sure you have binfmt_misc linux kernel module loaded. If you use your custom compiled kernel without this module, binfmt-support won’t work.

If it is an executable jar, then

Not all jar-Archives contain an executable class, declared to be started in the Manifest file, but if there is, this will work.

Btw.: You don’t start most programs from the shell with the dot. The dot is a shortcut for source , and it only works in the bash and some other shells, to include a script in the scope of the current session.

A compiled binary xybin is simply started with its name if it is in the path:

or, with its absolute path:

or with its relative path:

or if you happen to be in the directory of the file, with this relative path:

The file has to be marked executable for you (see: chmod). All of the above is true for shellscripts too, but they often have an extension .sh, and you can start a shellscript by invoking the interpreter, and then it needn’t be marked executable:

If you don’t want to start a new bash, you can use source, and you do so, to consume function definitions, aliases and variable settings.

Источник

How can I make a .jar file executable?

I’m trying to run a jar application under Ubuntu, so I installed OpenJDK Java 7 Runtime, but when I open this application I got this message :

The file ‘/home/aimad/Programms/jMerise/JMerise.jar’ is not marked as executable. If this was downloaded or copied from an untrusted source, it may be dangerous to run. For more details, read about the executable bit.

5 Answers 5

You can always run a jar file by doing java -jar JMerise.jar .

Читайте также:  Realtek bluetooth driver windows 10 для чего нужен

However, to make the jar file itself executable, you need to set the executable bit, as the message hints. chmod +x /home/aimad/Programms/jMerise/JMerise.jar will accomplish this.

After that you can do ./JMerise.jar to run it.

man chmod will provide you with information about how chmod works.

Right click on the file, click on properties, then go to the Permissions tab, and check the box that says «Allow executing this file as a program».

Since you run your jar application with java -jar application.jar then that means java is on your path. You need two simple things: 1) add an interpreter (which apparently is #!java -jar ) in the first line of your jar file jut like you do that with your shell scripts: echo ‘#!java -jar’ > app.jar cat application.jar >> app.jar mv app.jar application.jar

If you cat the contents of your jar file you’ll see it starts with ex.: #!java -jar PK ^lN BOOT-INF/PK . . 2) add execute attribute by chmod +x application.jar Now you are able to «self-run» it via ./application.jar .

First you’ll need to make sure you have a suitable Java runtime environment on your system. Ubuntu has openjdk in the official repo which is 99.99% combatible with Oracle Java, to install it type:

sudo apt-get install openjdk-7-jre

Next create a file called java-jar-launcher.desktop in

/.local/share/applications and put the following contents in it:

Next add the following line in

Now you should be able to just double click jar files to launch them, if nothing happens then right click on a jar file, select properties then go to the «Open With» tab and there you should see «Java Application Launcher», select that.

This method is prefferable (IMHO) because this way you are not giving execute permissions to jar files which can be potentially dangerous. This method will only work in a graphical environment and needs the user to manually double click on the file.

If running a .jar file from the command line works ( java -jar myFile.jar ), but double-clicking it in the GUI does not, and if sudo chmod +x myFile.jar appears to succeed but you still can’t open with double-click, and if right-clicking the .jar file > Properties > Permissions > «Allow executing file as program» does not work (i.e., the checkbox switches back just after you click it), then probably the .jar file is on an NTFS file system, which does not allow execute permissions on a per-file basis. (You may have this problem if you dual-boot, for example, and have a shared NTFS partition between Ubuntu and Windows).

Creating a launcher (as @tusharkant15 describes) will work because behind the scenes you’re executing /usr/bin/java , not the .jar file itself. Moving the .jar file to some other file system that is not NTFS will also work.

Not the answer you’re looking for? Browse other questions tagged openjdk or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

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