How to create an alias for standard "java" execution command?

2

1

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.

noob

Posted 2015-09-25T04:46:56.900

Reputation: 23

Cross-posted on StackOverflow: http://stackoverflow.com/questions/32775019/how-to-create-an-alias-for-standard-java-execution-command

– John1024 – 2015-09-25T05:16:05.753

Are you trying to be able to just run a Java class without specifying "java" at all, as in load myclass.class into the Java runtime just by typing ./myclass? I don't know a way to do that. If you simply want to be able to add parameters to (or make an alternate name for) the java command, you can simply use alias like for any other command. – CBHacking – 2015-09-25T05:41:21.567

Answers

2

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.

deeeeeeekun

Posted 2015-09-25T04:46:56.900

Reputation: 117

Java makes this difficult because the JRE is too stupid to strip the .class off the filename, and will fail if you just pass it a file name. You would need to write a function or simple script that strips the extension. – CBHacking – 2015-09-25T05:30:26.830

From what I get from the question though, the user passes the classname and not the file name. Or am I getting the question wrong? – deeeeeeekun – 2015-09-25T05:33:22.187

3

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.

CBHacking

Posted 2015-09-25T04:46:56.900

Reputation: 5 045