Anonymous Platypus
Anonymous Platypus

Reputation: 1302

How to run java application from terminal by simply calling its name?

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?

Upvotes: 0

Views: 279

Answers (2)

user3458
user3458

Reputation:

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.

Upvotes: 0

Marcus Müller
Marcus Müller

Reputation: 36337

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.

Upvotes: 4

Related Questions