RunJava is a simple command-line tool that helps you execute Java files with command-line arguments using a single command.
Combining the compilation and execution steps into a single command can save you time and make your workflow more efficient.
Note: This script is intended for use with Git Bash on Windows, but it should work on other Unix-like systems as well.
The basic syntax for using RunJava is:
runjava [class_name] [args...]
For example, if you have a Java file named HelloWorld.java
with a main
method that takes a single argument, you can run it using the following command:
before:
$ javac helloWorld.java
$ java HelloWorld arg1
After:
$ runjava HelloWorld arg1
RunJava is a shell script and does not require any installation process. You can save the script below as runjava.sh
and make it executable using the following command:
-
Find your root directory of Git Bash installation and go to
/usr/bin
. For my case, full path is :C:\Program Files\Git\usr\bin
-
Copy the provided script there (you will need Administrator rights). runjava.sh
-
Open Git Bash as an Administrator and give your script executable right :
chmod +x /usr/bin/runjava.sh
-
Create a symbolic link to the script:
ln -s /usr/bin/runjava.sh /usr/bin/runjava
you could add the following line to your
.bashrc
or.bash_profile
file:alias runjava='runjava.sh'
Then, run the following command to apply the changes:
source ~/.bashrc
-
Now you can run the script from anywhere in Git Bash by typing
runjava class_name
.