Is there way to set up an alias in such a way so that I could enter the command, followed by the argument in a single line?
For example, instead of
javac Program.java && java Program
I would be able to go
newcommand Program.java //or "newcommand Program", whichever is easier
which would do the same commands as the line above.
Since Java 11 you can use a single command
java example.java
https://openjdk.java.net/jeps/330
Linked: How to compile and run in single command in java
An alias
is not made to accept parameters, define a function like this:
jcar() { javac $1.java && java $1 ; }
Then use it:
jcar Program
(jcar
was intended as an acronym for java-compile-and-run)
Adding on to enrico.bacis' answer, i personally don't like to have Program.class files cluttering up my workspace if i'm just testing a program, so i'd do
jcar() { javac $1.java && java $1 && rm $1.class}
in addition, i found it helpful to trap ctrl-c
so that even if i end the program halfway, it still removes the .class
jcar() {
trap "rm $1.class" SIGINT SIGTERM
javac $1.java
java $1
rm $1.class
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With