Can you compile and execute Java files in Geany with one command?

Curious picture Curious · Feb 18, 2013 · Viewed 8.2k times · Source

Is there a way to compile and execute java files using one command, say F1, in Geany?

Its much more comfortable than having to click f5 to wait for the class to compile first, then F1 to execute that compiled file.

Answer

user6574979 picture user6574979 · Jul 11, 2016

Yes you can!

While editing a Java file, go to Build > Set Build Commands. Under Java commands click the first empty label and give it a name, e.g. Compile & Execute. Then in the Command field enter the command line you would use in a terminal to compile and execute your files, substituting file and class names with variables (see the Geany manual).

For example, on Linux one would use javac "%f" && java "%e". (Using && ensures the resulting class file is only executed when compilation is succesful.)

Click OK and open the Build menu again to see the Compile & Execute command is now there. In my case Geany automatically assigned it the shortcut key F9. If there's no shortcut key you can assign one in Preferences > Keybindings.

Pressing your shortcut key will now run the command line you entered and display its output in the Compiler tab of the Message Window. Alternatively you can create your build command under Execute commands instead of Java commands, and it will be run in a terminal window instead.