Compile multiple Java classes in Command Prompt

I. Introduction.
In this article it will be shown how to compile multiple Java classes in console (Command prompt) or Terminal.

As a prerequisite you should have Java downloaded, installed and set the JAVA_HOME environment variable.

II. Compiling multiple source files in console.

The example project, which will be used contains two files:

  • MessageDeliver.java – Example file, which contains one private field and method
  • AppMain.java -> the main class, which calls method from MessageDeliver

MessageDeliver.java

public class MessageDeliver {
	private String msg = "Hello!";

	public String getMsg() {
		return this.msg;
	}
}

AppMain.java

public class AppMain {
	public static void main(String[] args) {
		MessageDeliver mDeliver = new MessageDeliver();

		System.out.println(mDeliver.getMsg());
	}
}

In order to build both files, open command prompt or terminal and type:

javac MessageDeliver.java AppMain.java

javac command calls java compiler. If you have configured correctly JAVA_HOME variable then in the folder, where are located MessageDeliver.java and AppMain.java should appeared two new files MessageDeliver.class and AppMain.class.

III. Running Java files from Command Prompt.

Running files after being compiled in Command Prompt is done by writing the following command:

java AppMain

IV. Conclusion.
In this article, It was shown how to compile multiple files and to run them in Command Prompt. The examples were done in Windows OS, but the process is similar in Linux terminal.

Thank you for reading this article! If you have any questions – feel free to ask in comments section.

Leave a comment