A .java file is source code which is meant for a programmer to write code in a format suitable to them, to define the behaviour of the program.
Source code is compiled into .class files which you can execute (run), although they are often packaged into JAR files (which are simply collections of .class files).
These can be executable and can be passed between people to share programs.
GUIs are irrelevant. You can either create a GUI or not, but the functionality will still exist in the code and can be executable. GUIs allow you to view and interact with a program, whereas without a GUI you have to use the command line if interaction is required.