简体   繁体   中英

How to Recreate Brew Aliasing, Without Brew?

Due to a security update in my organisation, I can no longer use brew to tap into my organisation's repo to install a package. I can, however, manually download the.jar files that brew was installing.

So previously I did:

brew tap <repo>
brew install <package>
<package> # Run the package from anywhere

And I could run the package from anywhere, just by typing in terminal. Easy peasy.

Normally Brew installs in usr/local/Cellar/<package>/some/internal/structure/<package.exe> . But somewhere along the way it does something with aliases and symlinks and $PATH [which I am confused by] so that I can run the given package from /usr/local/bin , which is in my $PATH, by just typing <package> anywhere in terminal.

I am trying to recreate this behaviour. I was able to manually download the jar files and put them in a folder /usr/local/bin/<package> . And if I run java -jar /usr/local/bin/<package>/<package.exe> then everything runs fine.

How do I get it so that I can run <package> from anywhere in terminal, like with Brew? Also, just to be 100% clear, I want to choose the alias; I want to be able to type "abc" to run the jar files.

/usr/local/bin/ is likely in your PATH variable already. If you want to check, print it to the terminal with echo "$PATH" . If it isn't, you can pick one of the other directories in there or add it to. If you want to add that directory to your PATH variable, you want to add this to the relevant dot file (likely ~/.bashrc ):

export PATH="/usr/local/bin:$PATH"

PATH is a colon separated list of directories where your system will look for executables.

Now, you can just write a short script to run java for you. For example, if we have a jar file called foo.jar, you could make a short script that runs java with the full path of foo.jar like this:

/usr/local/bin/foo :

#!/bin/bash

java -jar '/path/to/foo.jar' "$@"

sneaky edit : Make sure you give this file executable permissions:

chmod +x /usr/local/bin/foo

Now, on the terminal, if I run foo without a full path, it will run this script.

The "$@" is just going to pass any arguments you sent to this script along into the java program.

This isn't the only option to achieve this. You mentioned aliases as well. You could write an alias to your.bashrc that does the same thing:

alias foo='java -jar "path/to/foo.jar"'

A symlink wouldn't really be the best option here. This would be okay if your jar file was not in the PATH and you wanted it there. BUT , the PATH variable is really only for files that can be executed directly. As you already know, jar files cannot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM