3.3. Compiling and Running Programs


Compiling a Java program

Use javac to compile a Java program. Command

  javac prog.java
converts the Java program in file prog.java into another file, prog.class, that the Java interpreter can run.


Running a Java program

The Java interpreter is called java. It runs a compiled Java program. Command

  java prog
Do not write .java after the program name.


Stopping a rogue program

If your program goes into an infinite loop, you will need to stop it. Type control-C. That will usually stop it.

If for some reason control-C does not work, there are more involved ways to stop it. You might need to start another terminal. Command

  ps u
shows all processes that you are currently running. Find the one that is your program. It has a process number. If n is the process number, then command
  kill n
will usually stop it. If it doesn't, then
  kill -9 n
will always stop it.