Problem :
I want to create an alias for running the java projects on command line, like instead of putting “java classname” every time ; I want to create an alias for that ‘java’ command. For instance, “run classname”. Is there any way to substitute standard java project execution command with our own? Thank You.
Solution :
If you want an alias for executing a specific class (with specific parameters) add the following to your bash profile file (something like .bash or .profile in your home directory):
alias aliasname="java classname param1 param2"
This would run the class indicated by classname with the parameters specified when you type aliasname in your command line.
However, if you want an alias that can take any class (and any parameter) add the following the your bash profile:
function functionname() { java $@ }
Which is like renaming the java command to functionname without any added benefits.
Note that you would have to execute the following command:
source "filepath_to_your_profile_path"
or restart your terminal for this to take effect.
If you just want to add parameters to the java
command, that’s easy enough:
alias java="java -Xmx2g"
That would let you run java <classname>
to launch the class in a JRE instance that is allowed to use up to 2GB of RAM. If you want to make an alternate version of the java
command that runs with different parameters, that’s also easy:
alias runjar="java -jar"
That would let you run runjar <jarfilename.jar>
to run a .JAR file. Since it calls java
internally, it will also expand the alias above (if both of them are defined), so the full resulting command would be java -Xmx2g -jar <jarfilename.jar>
.
In both cases, you would want to add these lines to your .profile
and/or .bashrc
(or equivalent for your preferred shell) scripts, so they are loaded automatically when you open a shell.