Compile and build with single command line Java (Linux)
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
}
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