How to associate a file to execute with a certain program

izzykiefer

New Member
Joined
Dec 8, 2020
Messages
2
Reaction score
1
Credits
20
Hello Everyone,

I was just wondering if there is a way in linux command-line to associate a file extension to be executed by a previously configured program .

For Example,
Associate all *.jar files to run by Java program.

So instead of typing java -jar /home/izyy/myprogram.jar
Can we just associate that file in commandline somewhere in the config so bash recognizes that extension and chooses java to execute that file.

so just typing /home/izzy/myprogram.jar would execute on its own..
 
Last edited:


With any script file, you can use a shebang at the start which specifies the interpreter the shell should load.

But with .jar files that wouldn’t work.
There might be something you can do with xdgopen? Or was it xcbopen?? Something like that, I think?! Unfortunately, I don’t have my laptop handy atm.

But even with that, you’d still be having to type xdgopen and the path to the jar.

Maybe you could set up an alias in your .bashrc:
Bash:
alias jar='java -jar'

Then you can run your jar files like this:
Bash:
jar /path/to/jarfile.jar
It’ll at least save you from typing a few extra characters.

Or alternatively, if it’s an application you use a lot, set an alias that will run your application:
Bash:
alias myapp='java -jar /path/to/jarfile.jar'

In the above example, name the alias whatever you want. Also, the path to the jarfile must be the absolute path from / and NOT a relative path.

And then you can simply use:
Bash:
myapp
Or whatever you called the alias.
 
THank you sooo much for the reply. SOrry a little late on replying.. I thought so too that only those options are available ...

With any script file, you can use a shebang at the start which specifies the interpreter the shell should load.

But with .jar files that wouldn’t work.
There might be something you can do with xdgopen? Or was it xcbopen?? Something like that, I think?! Unfortunately, I don’t have my laptop handy atm.

But even with that, you’d still be having to type xdgopen and the path to the jar.

Maybe you could set up an alias in your .bashrc:
Bash:
alias jar='java -jar'

Then you can run your jar files like this:
Bash:
jar /path/to/jarfile.jar
It’ll at least save you from typing a few extra characters.

Or alternatively, if it’s an application you use a lot, set an alias that will run your application:
Bash:
alias myapp='java -jar /path/to/jarfile.jar'

In the above example, name the alias whatever you want. Also, the path to the jarfile must be the absolute path from / and NOT a relative path.

And then you can simply use:
Bash:
myapp
Or whatever you called the alias.
 

Members online


Top