1

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 !

jeremieca
  • 1,156
  • 2
  • 13
  • 38
  • `java -jar jarNameGoesHere` in command line this is how you run a jar. Not by double clicking on it. – StackFlowed Jan 31 '18 at 15:46
  • I think it crashes for some "internal" reason... You could try to launch by command line with `java -jar yourjar.jar` to see possible errors. – Alessandro Jan 31 '18 at 15:48
  • ```java -jar yourjar.jar``` works. Any ideas ? @Alessandro – jeremieca Jan 31 '18 at 15:51
  • @StackFlowed I'm not in the cli when I double click but in the finder of course. So it's impossible without ```java -jar ...``` ? – jeremieca Jan 31 '18 at 15:52
  • 1
    You can convert your `.jar` file to `.exe` by using `Launch4j` program. And then it will work with double click. – Dumbo Jan 31 '18 at 15:56
  • Can you download a runnable jar from somewhere and see if that launches by double clicking? That'll tell you whether the issue is with your jar or your OS configuration. Although if it launches by `java -jar Foo.jar` it should work with double clicking. @StackFlowed have you never launched a jar file by double clicking? – Kayaman Jan 31 '18 at 16:32
  • @Kayaman No I haven't tried it yet ! ... – StackFlowed Jan 31 '18 at 16:33
  • 1
    @EJusius that would be useless, and not just because he's on Mac OS. – Kayaman Jan 31 '18 at 16:36
  • 1
    @Kayaman sorry, I didn't notice that... – Dumbo Feb 01 '18 at 07:01

1 Answers1

2

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.

Kristin
  • 1,336
  • 7
  • 23