To compile a .java file on Linux (e.g., Ubuntu), first you need to install Java JDK on your computer. You can install it with the instructions at How do I install Java? , here and here.
Once you have java JDK installed, open your terminal and cd to the direcotry where your .java file located and type the following:
$ javac filename.java
To run the generated class file, use the following command
$ java filename