Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I have a java tool that I use quite often from the command line but to use it I have to type java -jar dsim.jar. I want to make it so that I can call it just by typing dsim regardless of what directory I am in, but I can't quite work out how to accomplish this.

I have created a file at ~/.bash_profile and put into it:

alias dsim='java -jar dsim.jar'

And I have placed the dsim.jar file into /usr/local/bin as I read this is where user created command line tools should be kept. This location is in my $PATH.

I think the alias works well but location of the file doesn't seem to be accessible from anywhere. If I call dsim from within /usr/local/bin it works fine but if I am in any other directory I get:

Error: Unable to access jar file dsim.jar

How can I get this to work from any directory?

share|improve this question

2 Answers 2

This is not a direct answer, but you can consider the following solution as an useful one for often repeated commands:

Create ~/.inputrc and fill it with this:

"\e[A": history-search-backward
"\e[B": history-search-forward
set show-all-if-ambiguous on
set completion-ignore-case on

This allows you to search through your history using the up and down arrows … i.e. type "cd /" and press the up arrow and you'll search through everything in your history that starts with "cd /".

(via https://coderwall.com/p/oqtj8w)

share|improve this answer

Use absolute paths for the jar file:

alias dsim='java -jar /path/to/dsim.jar'
share|improve this answer
    
That will probably work but isn't the idea of putting something into a directory in your PATH that you don't have to address it like that? I thought that having it in the PATH meant that you could call just the filename from any directory and it would be found? –  Sam Apr 2 at 23:42
    
@Sam Not for files like this. java is located using the PATH, but files are not, otherwise if you had a file called java in a folder then running a command such as cp java java2 would copy the file from your PATH rather than the file in the current folder. –  George Garside Apr 3 at 20:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.