200

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 Java .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?

jonathan@ubuntu:~/Desktop$ . Minecraft.jar
bash: .: Minecraft.jar: cannot execute binary file

I am trying to execute the Minecraft.jar file.

Pablo Bianchi
  • 17,552
jaorizabal
  • 3,788
  • 5
    Hello and welcome, jaorizabal. I slightly corrected your question. Maybe you can install a spell checker for your browser, to improve the next question yourself. Another hint: For text issues, you may mark text with your mouse in the terminal, and then paste it into the edit field of your browser (and elsewhere) with a middle click on the scroll wheel. Then you can layout your code with the buttons at the edit field. This is much faster than taking a screen shot, and uploading it. – user unknown Feb 06 '12 at 01:55

9 Answers9

313

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:

java -jar Minecraft.jar

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:

java -version 

[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.

Jeremy Kerr
  • 27,879
45

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 first make sure you have Java installed, then do the following in a console:

  1. Install binfmt-support Support for extra binary formats:

    The binfmt_misc kernel module, contained in versions 2.1.43 and later of the Linux kernel, allows system administrators to register interpreters for various binary formats based on a magic number or their file extension, and cause the appropriate interpreter to be invoked whenever a matching file is executed. Think of it as a more flexible version of the #! executable interpreter mechanism.

    sudo apt-get install binfmt-support
    
  2. cd to your JAR file and change it to executable (you can also do this through file properties in Nautilus)

    chmod a+rx myjar.jar
    
  3. Run your jar file just as if it was any other binary executable or shell script

    ./myjar.jar
    

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.

David
  • 731
  • 1
    Thanks! This is exactly what I was looking for. It's better than using java -jar because it doesn't require the host program to know that it is a java program, and it's better than using a script because you don't have to worry about passing through STDIN and OUT. – srlm Dec 06 '13 at 05:34
  • 4
    Doesn't work on Debian 9: "invalid file (bad magic number): Exec format error" – Paul Aug 29 '17 at 21:52
  • be careful this conflicts with cosmopolitancc – Rainb Apr 14 '24 at 17:57
  • For you, it is obvious that you have to have Java installed to run a jar file. It wasn't obvious for me... As you said "Linux is perfectly capable of running a foreign binary, like a JAR file", I thought that I didn't have to have Java installed. Now, I understand it. Maybe it doesn't hurt to say it. Anyway, thanks for the tip. – Mario Mey Oct 07 '24 at 18:50
20

If it is an executable jar, then

java -jar Minecraft.jar 

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:

xybin 

or, with its absolute path:

/home/foo/proj/test/xybin

or with its relative path:

proj/test/xybin

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

./xybin

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:

 bash xy.sh

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.

user unknown
  • 6,902
9

You might as well want to make a nice entry for the application in Unity. execute the following commands:

gedit ~/.local/share/applications/minecraft.desktop

In the window that pops up, copy and paste the following:

[Desktop Entry]
Type=Application
Name=Minecraft
Comment=Click here to play Minecraft
Exec=java -jar /path/to/minecraft.jar
Icon=/path/to/minecraft/icon.png
Terminal=false
Categories=Game;

You might need to log out and back in to see the effects. :) Also you need to search the internet for a nice lookin Minecraft icon since they don't provide one with the download..

Gladen
  • 2,726
6

Install jarwrapper. After that (and by adding the executable bit) you can start the jar file just by entering the jarfile name.

sudo apt-get install jarwrapper

This works by using binfmt to add support for a new binary format to the kernel.

muru
  • 207,970
5
  1. Open a command prompt with CTRL+ALT+T
  2. Go to your ".jar" file directory. If your Ubuntu version / flavour supports it, you should be able to right click on your ".jar" file's directory and click "Open in Terminal"
  3. Type the following command:

    java -jar jarfilename.jar
    

This way your ".jar" file will execute.

kos
  • 41,378
viper
  • 163
3
 java -jar /home/username/.minecraft/launcher.jar
1

New answer to an old question

Executing a jar file using the regular syntax ./app.jar (instead of java -jar), is easy to achieve, as explained here: how to create executable jars.

Basically, JAR is a variant of ZIP, which allows random bytes to be pre/appended to the JAR without corrupting it. This means it is possible to prepend a launcher script at the beginning of the jar to make it "executable".

Here is a simple example of turning a "normal" jar into an executable one:

# Append a basic launcher script to the jar
cat \
  <(echo '#!/bin/sh')\
  <(echo 'exec java -jar $0 "$@"')\
  <(echo 'exit 0')\
  original.jar > executable.jar

Make the new jar executable

chmod +x executable.jar

With this, you can now run ./executable.jar instead of java -jar original.jar. This works on all unix like systems including Linux, MacOS, Cygwin, and Windows Linux subsystem.

Derlin
  • 303
-2

if u want to install your jar with specific java version Specify the java directory also

/scratch/app/product/Software/jdk1.8.0_112/bin/java -jar /path-to-jar/Minecraft.jar