Return to site

โ˜• JAVA TRICK: Executing Source Files as Scripts

ยท java

You know that you can use the'๐ฃ๐š๐ฏ๐š' command to launch a single source file without having to manually compile it first,

but did you know that you can use this capability to write full-on scripts in Java in just three simple steps?

First , add a shebang line to the source file that points at your '๐ฃ๐š๐ฏ๐š' executable, followed by '--๐ฌ๐จ๐ฎ๐ซ๐œ๐ž' and the Java version the code was written for:

#!/๐‘/๐‘ƒ๐‘Ÿ๐‘œ๐‘”๐‘Ÿ๐‘Ž~1/๐ฝ๐‘Ž๐‘ฃ๐‘Ž/๐‘—๐‘‘๐‘˜-17/๐‘๐‘–๐‘›/๐‘—๐‘Ž๐‘ฃ๐‘Ž --๐‘ ๐‘œ๐‘ข๐‘Ÿ๐‘๐‘’ 17

Second, rename the file so it doesn't end with '.๐ฃ๐š๐ฏ๐š' extension - this is a great opportunity to gite it a good command-liney name.

Third make it executable with "๐œ๐ก๐ฆ๐จ๐ +๐ฑ".

There you go, script in Java!