How to Compile a Java Program Using an SDK or Online Compiler

Download Article
Use the SDK or an online compiler to check your Java pograms
Download Article

This How.com.vn teaches you how to turn your Java source code into an executable app using a local and online compiler. If you're using a computer, the most common way to compile Java code is using Java Software Development Kit (Java SDK) from the command line. If you're using a phone or tablet (or a computer without a compiler), you can use an online compiler like Codiva.

Things You Should Know

  • With the Java SDK installed, open your computer's command prompt to compile your Java program.
  • Use an online compiler for an easy and lightweight option to check if your code is working properly.
  • Online compilers often autosave, so you don't have to worry about losing progress if something were to go wrong.
Method 1
Method 1 of 2:

Using Java Software Development Kit

Download Article
  1. How.com.vn English: Step 1 Open the command prompt.
    You can use the Java Software Development Kit (Java SDK) from the command prompt on Windows, macOS, or Linux. If you don't have the Java SDK installed, see How to Install the Java Software Development Kit. Here's how to get to the command prompt on each system:
    • Windows: Right-click the Start menu and select Command Prompt. If you don't see this option, type cmd into the Windows Search bar and click Command Prompt in the search results.
    • macOS: Click the magnifying glass at the top-right corner of the screen to open Spotlight, type terminal, and then click Terminal in the search results.
    • Linux: Press Ctrl+Alt+T.
  2. How.com.vn English: Step 2 Use the cd command to access the directory with your Java code.
    The source code is the file that ends with the .java file extension.
    Advertisement
  3. How.com.vn English: Step 3 Type javac sourcecode.java and press ↵ Enter or ⏎ Return.
    Replace sourcecode.java with the name of your source file.[1] This compiles your source code into an executable file, which ends with the .class extension.
    • To see the name of the new file in the current directory, run the dir (Windows) or ls -a (Mac/Linux) command.
    • If you see an error when trying to
  4. How.com.vn English: Step 4 Type java programname and press ↵ Enter or ⏎ Return.
    Replace programname with the name of your program. This runs the program at the command line.
  5. Advertisement
Method 2
Method 2 of 2:

Using an Online Java Compiler

Download Article
  1. How.com.vn English: Step 1 Navigate to https://www.codiva.io...
    Navigate to https://www.codiva.io in a web browser. Codiva is an online Java compiler that's great for people who can't install a compiler locally—it can even be used on a phone or tablet.
    • There are a variety of online compilers out there if Codiva doesn't work for your needs. Some other popular options are Jdoodle, and OnlineGDB.
  2. How.com.vn English: Step 2 Sign in or create an account.
    If you're new to Codiva, click Create Account at the top-right corner to sign up.
  3. How.com.vn English: Step 3 Enter a project name and click Create.
    This creates a new project, which is like a container for your source files.[2]
  4. How.com.vn English: Step 4 Create a Java source file and click Create.
    Java source files must end with the .java file extension. The new file will appear in project's tree, which appears in the right panel.
    • For example, if you're creating a Java program called HelloWorld, name the source file HelloWorld.java.
  5. How.com.vn English: Step 5 Write or paste your code into the editor.
    The code will compile in the background as you type. Additionally, it will display any code errors as they occur.
  6. How.com.vn English: Step 6 Click Run to run the program.
    Since the code compiles automatically, clicking Run will just launch your app in its current state.
  7. Advertisement

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!

      Expert Interview

      Thanks for reading our article! If you’d like to learn more about dealing with html, check out our in-depth interview with Jessica Andzouana.

      About This Article

      How.com.vn English: Jessica Andzouana
      Written by:
      Software Engineer
      This article was written by Jessica Andzouana and by How.com.vn staff writer, Nicole Levine, MFA. Jessica Andzouana is a Software Engineer based in the San Francisco Bay Area. With over five years of professional experience in front-end development, digital art, and design, she is passionate about emerging technologies such as blockchain and AI. Her background as both a programmer and artist, paired with a highly design-conscious mindset, provides her a fresh perspective and unique skill set to produce creative solutions in her field. She works at Alcacruz as a Software Engineer, and received a dual BS/BA degree from Santa Clara in Computer Science and Studio Art. This article has been viewed 40,599 times.
      How helpful is this?
      Co-authors: 11
      Updated: December 23, 2023
      Views: 40,599
      Article SummaryX

      1.Open the command prompt.
      2.Change to the directory containing your code.
      3.Type "javac (your source file)" and press Enter.
      4.Type "java (compiled file name)" and press Enter.

      Did this summary help you?

      Thanks to all authors for creating a page that has been read 40,599 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement