To compile and run a Java file, use the command "javac .java" to compile the file and "java " to run the compiled program.