0

I know the default way of calling a jar file from terminal

java -jar myapp.jar

I want to call the app from terminal by the name myapp. I know its possible to call a shell script from terminal by this method if the script is kept in /usr/bin/ (And of course the .sh extension is removed)

Can this be achieved with jar files? I don't wish to set aliases. I wanted to do this specifically by calling from bin directory.

Edit: If its possible to call by something like java appname, that shall also be helpful.

Edit: I have checked the usage of linux gcj as Michael Aaron Safyan redirected me in comments. But gcj is said to be obsolete for years and is no longer an industry standard.

Can this be achieved by creating a jad file and calling something like jad myapp from terminal?

Anonymous Platypus
  • 1,242
  • 4
  • 18
  • 47
  • 1
    possible duplicate of [Executing a jar file in linux](http://stackoverflow.com/questions/6724953/executing-a-jar-file-in-linux) – Sam Estep Jul 02 '15 at 12:29
  • 1
    What about putting a script in /usr/bin/ that does java -jar PathToMyApp.jar ? – LBes Jul 02 '15 at 12:31
  • @RedRoboHood This is not my problem. Please read my question completely. I know how to execute a script and run as `./appname` – Anonymous Platypus Jul 02 '15 at 12:32
  • 1
    possible duplicate of [How to convert a .java or a .jar file into a Linux executable file ( without a .jar extension, which means it's not a .jar file )](http://stackoverflow.com/questions/29429976/how-to-convert-a-java-or-a-jar-file-into-a-linux-executable-file-without-a) – Michael Aaron Safyan Jul 02 '15 at 12:33
  • It doesn't have to be in "/usr/bin", specifically. Any location that is in the "PATH" environment variable would work. You just need a program that will invoke the "java -jar" command (could be BASH script, but it could be a Python script or any other wrapper, for that matter). – Michael Aaron Safyan Jul 02 '15 at 12:34
  • Can the jar file be separate? You could write that shell script you mentioned. Or are you looking for a one file solution? – maxwellb Jul 02 '15 at 12:37
  • @maxwellb Sorry, I didn't understand what you have asked. May be my lack of knowledge. – Anonymous Platypus Jul 02 '15 at 12:45

2 Answers2

4

Unix shells can't run jar files. The only solution to this are a) aliases, or b) writing a minimal wrapper around this, in a file called myapp

#!/bin/sh

java -jar myapp.jar

and putting it in your bin directory. Don't forget to make it executable (chmod 755 myapp). You can then call it by myapp.

Notice that you can be much cleverer about finding the right version etc. You might simply take such a script from your favourite MIT/BSD licensed Java application, probably, if you include its license with your software.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • Nice answer! That's what I suggested in the comments but was unsure whether that would solve his problem but I couldn't think of any other way to do that – LBes Jul 02 '15 at 12:35
  • @Marcus Müller ꕺꕺ This can be helpful. I will go for this if nothing better pops up. Could you please tell me the standard I should follow in unix to place the jar file? Should I put the jar also in `usr/bin/` or in `usr/share/` – Anonymous Platypus Jul 02 '15 at 12:42
  • 1
    /usr/local/lib or /usr/local/myapp/lib might be semantic and simple enough. Beyond that, you might look into learning pkgconfig. – maxwellb Jul 02 '15 at 12:48
  • @maxwellb Thank you. Will look for this :) – Anonymous Platypus Jul 02 '15 at 13:11
0

It's possible to configure Linux to recognize arbitrary file type as executable. I've never done it, but here is the description of binfmt_misc kernel capability.

That being said, it's a rare java program that does not require setting up a classpath. And that is best accomplished via a shell script wrapper.