How to Compile in Java
- 1). Right-click on the desktop, and click "New." Click "Text Document" to create a new text document named "Untitled.txt" on the desktop.
- 2). Right-click on the file, and click "Rename" to rename the new text file. Type "mytest.java" into the highlighted file name. Make sure your Windows Explorer Folder Options are set to show file extensions so when you rename the file, the new file extension is ".java," and not .java.txt."
- 3). Right-click on the "mytest.java" file, and click "Open With." Click "Notepad" to open the file in the Notepad text editor.
- 4). Type the following code into the blank text document.
//My First Java Program
class MyJavaProgram {
public static void main(String[ ] args){
System.out.println("I compiled a Java Program!!");
}
} - 5). Click the "File" option from the top navigation bar, then click "Save" to save the source code file.
- 6). Click the Windows "Start" button, then type "cmd" (without quotes) into the Search input box, and click the "Search" button. The DOS window opens.
- 7). Type the following line at the DOS prompt, and press the "Enter" key to compile the Java program. Note that the same command is used in Unix systems to compile Java.
javac MyJavaProgram.java - 8). Type "java MyJavaProgram" (without quotes) at the command prompt, and press "Enter" to run the compiled program. The message "I compiled a Java Program!!" appears on the the screen.