# A small batch file to compile and execute&#8230; Java

**Date:** 2017-04-19  
**Author:** Kees C. Bakker  
**Categories:** Automation, Windows  
**Original:** https://keestalkstech.com/small-batch-file-compile-execute-java/

![A small batch file to compile and execute&#8230; Java](https://keestalkstech.com/wp-content/uploads/2017/04/why-do-java-developers-wear-glasses-1.jpg)

---

My good friend Mark is doing Java. It brings back memories. Great to see that people are still using the command-line to compile and execute Java classes (command-line is back, y'all!).

Entering a `javac` (compile) and a `java` (execution) command seems tedious, so I wondered if those could be scripted into a good old `.bat` file. It has been a while since I've been playing around with batch files. I've seen my father do it... it still looks like a dark - and ancient - art to me.

## Batch me!

I've created a file called `yall.bat` which looks like:

```
@ECHO OFF

IF '%1'=='' GOTO error
IF NOT EXIST %1 GOTO notfound

ECHO.
ECHO Compiling '%1'...
javac "%~f1"
ECHO done!
ECHO.

ECHO Executing '%~n1':
ECHO.
java %~n1

GOTO end

:error
ECHO.
ECHO Invalid syntax. Please use:
ECHO.
ECHO yall {filename}
GOTO end

:notfound
ECHO.
ECHO File '%1' not found.
GOTO end

:end
ECHO ON
```

## Say what!?

There are some weird variable modifiers that should be explained:

Yep... dark arts indeed. Here is a list of arguments I've found on [StackOverflow](http://stackoverflow.com/a/3215539/201482):

| Variable with modifier | Description |
| --- | --- |
| %~I | Expands %I which removes any surrounding quotation marks (""). |
| %~fI | Expands %I to a fully qualified path name. |
| %~dI | Expands %I to a drive letter only. |
| %~pI | Expands %I to a path only. |
| %~nI | Expands %I to a file name only. |
| %~xI | Expands %I to a file extension only. |
| %~sI | Expands path to contain short names only. |
| %~aI | Expands %I to the file attributes of file. |
| %~tI | Expands %I to the date and time of file. |
| %~zI | Expands %I to the size of file. |
| %~$PATH | Searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, this modifier expands to the empty string. |

Might be handy as a reference for building future batch script (although one ought to move to PowerShell).

## Final thoughts

Batch. Still. Cool. Java. Still. Runs.

https://www.youtube.com/watch?v=2M4cd8EvFqg
