How to Compile a Java Program
Method 1 of 2:
Using Java Software Development Kit
- 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.
- 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.
- 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
- Type java programname and press ↵ Enter or ⏎ Return. Replace programname with the name of your program. This runs the program at the command line.
Method 2 of 2:
Using an Online Java Compiler
- 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.
- Sign in or create an account. If you're new to Codiva, click Create Account at the top-right corner to sign up.
- Enter a project name and click Create. This creates a new project, which is like a container for your source files.[2]
- 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.
- 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.
- Click Run to run the program. Since the code compiles automatically, clicking Run will just launch your app in its current state.
5 ★ | 1 Vote
You should read it
May be interested
- Download and install Java on the computerto program java on your computer, you need to install the java environment, this tutorial will show you how to download and install java on your computer.
- What is Java? Why choose Java?what is java? java is a high-level programming language, developed by sun microsystem and released in 1995. java runs on a variety of platforms, such as windows, mac and various versions of unix.
- How to Tune a Java Virtual Machine (JVM)the java virtual machine (jvm) runs your java programs. sometimes the default configuration that the jvm comes with may not be the most efficient for your program.
- How to Run a .Jar Java Filethis wikihow teaches you how to open and run executable jar files on a windows or mac computer. jar (java archive) files contain data to be used with the java program. most jar files are simply containers for data that another program...
- How to Write Your First Program in Javajava is an object-oriented programming language created in 1995 by james gosling, which means that it represents concepts as 'objects' with 'fields' (which are attributes that describe the object) and 'methods' (actions that the object can...
- How to Set Up a Java Programming Environmentjava is a popular and long-standing programming language, used by large and small, new and old companies alike. setting up your computer to run java for the first time can be a mild hassle. this wikihow will detail how to configure the...
- Get familiar with NetBeans Java IDEin the following article, we will introduce you the most basic features of netbeans ide through a small test, which is to create the 'hello world' java application. and when finished, you will know the general knowledge and process when programming applications in ide ...
- How to Compile C Programs with GNU (GCC)tipsmake today will teach you how to compile a c program from source code using gnu (full name is gnu compiler collection, abbreviated gcc) - compiler for linux and minimalist gnu (mingw) on windows.
- Test on JAVA test on P9unlike most common programming languages, instead of compiling the source code into machine code or interpreting source code when running, java is designed to compile the source code into bytecode, then bytecode will be run by the runtime environment. . to help you better understand this programming language, the following quiz of network administrator will give you lots of useful knowledge.
- How to Set Java Homedo you want to set java home? the variable java home, generally written as java_home, is set to the install path of java. learning how to set java_home in windows and linux isn't hard to do. assuming that the path for the java development...