jeremieca
jeremieca

Reputation: 1188

How to launch a cli Jar with double click?

Is it possible to launch a cli java project by double click on the jar ?

Inside the jar, we have the manifest file with the main class well defined, but when we try to double click on jar it can't launch it and displays a generic error : The java jar file could not be launched.

We supposed that it's because it is only able to run this jar from the cli.

Is it right ?

PS : Sorry for my bad english, I'm french :)

Thanks !

Upvotes: 1

Views: 345

Answers (1)

Kristin
Kristin

Reputation: 1391

Unless the jar-file includes some sort of implementation of Runtime that runs the system's terminal or command prompt, it won't open a terminal/prompt window when double clicking it (if some sort of GUI-implementation have been made with e.g. Swing, it will however launch the GUI). However, you can create a separate file, which will launch the jar-file.

As it seems you're on Mac, you can just create a .command-file. If you just need to execute your .jar-file, create a file with the following content:

#! /bin/bash
java -jar /path/to/file.jar

Name it something you remember, but don't forget to add .command at the end.

For Linux, use .sh extension, with the same content.

For Mac and Linux, you might have issues with executing the files because of lacking the permissions, see here for changing permissions on files.

For windows, use .bat extension. Exchange the slashes with backslashes when defining the path, and omit the #! /bin/bash-line. You'll also have to add Java to your environment variables, see here.

Upvotes: 2

Related Questions