How to Compile & Run Java Program Using Command Prompt

Method 1 of 2:

Compiling and Running

  1. How to Compile & Run Java Program Using Command Prompt Picture 1
    Save the program. After using a text editor, such as NotePad, to create your Java program, save the program with a .java extension. Of course, the file name can be anything. For the purposes of this tutorial, "filename" will be used as a placeholder for your file name.
    1. To ensure that your file is saved as a .java file, be sure to write .java after the file name, and select All Files from the dropdown extension menu.
    2. Make note of where you saved the file on your system.
    3. If you are unsure of how to write a program in Java, check out our tutorial on how to program in Java. For the purposes of learning how to compile and run a program, though, you can use any sort of Java program.
  2. How to Compile & Run Java Program Using Command Prompt Picture 2
    Open Command Prompt/Terminal. Accessing the command line differs slightly for Mac and Windows.
    1. Windows: Press Home, then type cmd. Press Enter to open the Command Prompt.
    2. Mac: In Finder, press the Go tab, select Applications, select Utilities, then select Terminal.
  3. How to Compile & Run Java Program Using Command Prompt Picture 3
    Check that Java is installed. Type java -version into your command line. If java is installed, you will see a message stating what version of Java is currently installed.
    1. If not, you may need to install the Java Development Kit from their website. It is free and can be found at this link: http://www.oracle.com/technetwork/java/javase/downloads/index.html.
  4. How to Compile & Run Java Program Using Command Prompt Picture 4
    Navigate to the correct folder. Use the command cd followed by the directory name to change your working directory.
    1. For example, if you were operating in C:UsersBobProject and wanted to get to C:UsersBobProjectTitanProject , enter in cd TitanProject and press Enter.
    2. You can see a list of what is in your current directory by typing dir and pressing Enter.
  5. How to Compile & Run Java Program Using Command Prompt Picture 5
    Compile the program. Once you are in the correct directory, you can compile the program by typing javac filename.java into the command line and pressing enter.
    1. If you have any errors in your program, or if there is difficultly compiling, the command prompt will notify you.
    2. Check out our article about how to fix compiler errors in Java for more help.
  6. How to Compile & Run Java Program Using Command Prompt Picture 6
    Run the program. Enter in java filename and press Enter. Of course, replace "filename" with whatever the name of your file actually is.
    1. After pressing Enter, your program should run. If you receive an error, or if your program fails to run in any way, refer to the troubleshooting method.
Method 2 of 2:

Troubleshooting

  1. How to Compile & Run Java Program Using Command Prompt Picture 7
    Set your path. If you are running a simple program with all of the files in the same directory, you will probably not need to do this. However, if you are running a more complex program with files in multiple directories, you will need to tell the computer where to look for these files.
    1. Windows: Type java -version into the command prompt and press Enter. Based off the version of Java displayed on the first line, type in set path=%path%;C:Program FilesJavajdk1.5.0_09bin to the command prompt and press Enter. Substitute jdk1.5.0_09 with whatever version of Java you have installed.
      1. Be sure you are entering in this command while in the directory containing your Java project.
    2. Mac: Type /usr/libexec/java_home -v 1.7 into the terminal and press Enter to ensure Java is installed on your system. Then, type echo export "JAVA_HOME=$(/usr/libexec/java_home)" >> ~/.bash_profile into the terminal and press Enter. Restart the terminal afterwards.
4 ★ | 1 Vote

May be interested

  • How to Change Directory in Command PromptHow to Change Directory in Command Prompt
    this article shows you how to change folders in the command prompt program on a windows computer. to change commands in command prompt, you must use an administrator account.
  • How to Change Directories in Command PromptHow to Change Directories in Command Prompt
    this wikihow teaches you how to change the folder (also known as the 'directory') in which your windows computer's command prompt program operates. to make changes in command prompt, you must be using an administrator account. open start....
  • How to Open Folders in CMDHow to Open Folders in CMD
    do you need to access something from the command prompt or the windows command prompt? although it looks classic, this program is really easy to use if you know some basic commands. today's tipsmake will show you how to use the 'cd' command to open a folder in windows command prompt. besides, you will also know more great tips to instantly open command prompt in any folder on windows file explorer.
  • How to Open Command Prompt on WindowsHow to Open Command Prompt on Windows
    today's tipsmake will show you how to open the command prompt program on windows. there are many ways to do it, from as simple as searching the start menu to using the run command. however, you should remember, some computers (such as school computers) cannot run command prompt due to restrictions.
  • How to Add and Delete User Accounts with Command Prompt on WindowsHow to Add and Delete User Accounts with Command Prompt on Windows
    command prompt is basically a version of the 'terminal' application (apple) on windows, a program that allows us to enter system adjustment commands. while most commands users enter into command prompt simply execute the action itself (such as opening a folder), you can also use command prompt to quickly create (or delete) accounts. right on your computer!
  • Add Command Prompt to Power User Menu on Windows 10Add Command Prompt to Power User Menu on Windows 10
    on windows 10 build 14971, microsoft replaced command prompt and command prompt (admin) with windows powershell. according to microsoft, this change will bring the best command line experience to users. but in fact, users prefer to use command prompt rather than using powershell.
  • How to Compile a C ProgramHow to Compile a C Program
    this wikihow teaches you how to compile your c program in windows and macos. if you're using linux, check out how to compile a c program using the gnu compiler. go to https://cygwin.com/install.html. cygwin is a free windows tool that...
  • How to open Command Prompt on Windows 10, 8, 7, Vista and XPHow to open Command Prompt on Windows 10, 8, 7, Vista and XP
    command prompt is one of the command line interface programs used to execute commands in the windows operating system. the article will guide you how to open command prompt on windows 10, 8, 7, vista and xp.
  • How to Make Command Prompt Appear at SchoolHow to Make Command Prompt Appear at School
    this wikihow teaches you how to access command prompt on a windows 10 school computer. although you may be able to open command prompt if only the file path to command prompt is blocked, there is no way to bypass an administrator lock on...
  • 6 Best Command Prompt Alternatives for Windows6 Best Command Prompt Alternatives for Windows
    do you find the command prompt a bit complicated and feel you need a tool that is easier to use? this is where other terminal emulators come into play!