Compiling and running a Java application on Mac OSX, or any major operating system, is very easy. Apple includes a fully-functional Java runtime and development environment out-of-the-box with OSX, so all you have to do is write a Java program and use the built-in tools to compile and run it.

Writing Your First Program

The first step is writing a simple Java program. Open up a text editor (the built-in TextEdit app works fine), type in the following code, and save the file as "HelloWorld.java" in your home directory.

public class HelloWorld {
    public static void main(String args[]) {
        System.out.println("Hello World!");
    }
}

For example, if your username is David, save it as "/Users/David/HelloWorld.java". This simple program declares a single class called HelloWorld, with a single method called main. The main method is special in Java, because it is the method the Java runtime will attempt to call when you tell it to execute your program. Think of it as a starting point for your program. The System.out.println() method will print a line of text to the screen, "Hello World!" in this example.

Using the Compiler

Now that you have written a simple Java program, you need to compile it. Run the Terminal app, which is located in "Applications/Utilities/Terminal.app". Type the following commands into the terminal:

cd ~
javac HelloWorld.java

You just compiled your first Java application, albeit a simple one, on OSX. The process of compiling will produce a single file, called "HelloWorld.class". This file contains Java byte codes, which are the instructions that the Java Virtual Machine understands.

Running Your Program

To run the program, type the following command in the terminal.

java HelloWorld

This command will start a Java Virtual Machine and attempt to load the class called HelloWorld. Once it loads that class, it will execute the main method I mentioned earlier. You should see "Hello World!" printed in the terminal window. That's all there is to it.

As a side note, TextWrangler is just a text editor for OSX and has no bearing on this situation. You can use it as your text editor in this example, but it is certainly not necessary.

Answer from William Brendel on Stack Overflow
🌐
Princeton CS
introcs.cs.princeton.edu › java › mac › terminal.php
Using Java from the Terminal in Mac OS X
The Terminal is necessary for redirecting standard input, redirecting standard output, and piping—you will use these features in Section 1.5. These instructions apply to Mac OS X 10.4.11 and higher. You will use the Java compiler javac to compile a Java program and the Java interpreter java ...
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › mac-cmd.html
Java and the Mac OS X Terminal
You should now have a Terminal window somewhere on the screen. It will have a prompt that looks something like: ... To check that you are running the right version of Java, type the commands in boldface below. You should see something similar to the information printed below. The important part is that it says 1.6 or 1.5 (and not 1.4). machine:~ wayne$ java -version java version "1.6.0_26" Java(TM) SE Runtime Environment (build 1.6.0_26-b03-384-10M3425) Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-384, mixed mode) Then type
Discussions

macos - How To Run Mac OS Terminal Commands From Java (Using Runtime?) - Stack Overflow
If a unix cmd (or use the Unix part of an OSX app e.g. /Applications/AppName.app/Contents/MacOS/AppName) then there are two ways to fix this · Put the full path to the executable in the Java code e.g. More on stackoverflow.com
🌐 stackoverflow.com
macos - How do I run a Java program on Terminal (Mac)? - Stack Overflow
I am new to Java (as of today!) and am trying to run a very simple program in terminal. Normally, when I run a python (still pretty new) program in Terminal I would simple type in "python name.py"... More on stackoverflow.com
🌐 stackoverflow.com
How do I compile a program via terminal on macbook?
You used something other than a plain text editor to create your source code. This stackoverflow question should answer your problem, even though it's a C language question rather than Java, the origin of the problem is the same: https://stackoverflow.com/questions/23669037/error-on-rtf1-ansi-when-compiling-c-program More on reddit.com
🌐 r/learnjava
9
1
December 22, 2021
macos - Opening terminal in mac through java - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
December 13, 2017
Top answer
1 of 6
218

Compiling and running a Java application on Mac OSX, or any major operating system, is very easy. Apple includes a fully-functional Java runtime and development environment out-of-the-box with OSX, so all you have to do is write a Java program and use the built-in tools to compile and run it.

Writing Your First Program

The first step is writing a simple Java program. Open up a text editor (the built-in TextEdit app works fine), type in the following code, and save the file as "HelloWorld.java" in your home directory.

public class HelloWorld {
    public static void main(String args[]) {
        System.out.println("Hello World!");
    }
}

For example, if your username is David, save it as "/Users/David/HelloWorld.java". This simple program declares a single class called HelloWorld, with a single method called main. The main method is special in Java, because it is the method the Java runtime will attempt to call when you tell it to execute your program. Think of it as a starting point for your program. The System.out.println() method will print a line of text to the screen, "Hello World!" in this example.

Using the Compiler

Now that you have written a simple Java program, you need to compile it. Run the Terminal app, which is located in "Applications/Utilities/Terminal.app". Type the following commands into the terminal:

cd ~
javac HelloWorld.java

You just compiled your first Java application, albeit a simple one, on OSX. The process of compiling will produce a single file, called "HelloWorld.class". This file contains Java byte codes, which are the instructions that the Java Virtual Machine understands.

Running Your Program

To run the program, type the following command in the terminal.

java HelloWorld

This command will start a Java Virtual Machine and attempt to load the class called HelloWorld. Once it loads that class, it will execute the main method I mentioned earlier. You should see "Hello World!" printed in the terminal window. That's all there is to it.

As a side note, TextWrangler is just a text editor for OSX and has no bearing on this situation. You can use it as your text editor in this example, but it is certainly not necessary.

2 of 6
11

I will give you steps to writing and compiling code. Use this example:

 public class Paycheck {
    public static void main(String args[]) {
        double amountInAccount;
        amountInAccount = 128.57;
        System.out.print("You earned $");
        System.out.print(amountInAccount);
        System.out.println(" at work today.");
    }
}
  1. Save the code as Paycheck.java
  2. Go to terminal and type cd Desktop
  3. Type javac Paycheck.java
  4. Type java Paycheck
  5. Enjoy your program!
🌐
Quora
quora.com › How-do-I-run-a-Java-program-in-Mac-using-a-terminal
How to run a Java program in Mac using a terminal - Quora
Answer (1 of 2): There are two different ways you can run a Java program in mac using terminal.. 1. Writing the program using nano editor in the terminal itself and running it. 2. Creating a java file, saving it with .java extension and then opening terminal to run it. *Make sure you've Java ins...
🌐
O'Reilly
oreilly.com › library › view › mac-os-x › 0596003706 › ch10s04.html
Java on the Command Line - Mac OS X in a Nutshell [Book]
January 27, 2003 - Java on the Command Line You can run Java programs from the command line through the java command. Generally, you invoke it in one of two ways: java options class argument1... - Selection from Mac OS X in a Nutshell [Book]
Authors   Jason McIntoshChuck Toporek
Published   2003
Pages   832
🌐
Princeton
cs.princeton.edu › courses › archive › spr04 › cos126 › hello › mac.html
Hello, World in Java on Mac OS X
You will use the Java compiler javac to compile your Java programs and the Java interpreter java to run them. Mac OS X includes implementation of Java 2 Standard Edition (J2SE) 1.4.1, so there is nothing to do in this step. You will type commands in an application called the Terminal.
🌐
Medium
medium.com › @akhiltheguitarist › setting-up-java-path-on-macos-11d51e89a55f
Setting up java path on macOS. So recently, I changed my work machine… | by akhil | Medium
September 24, 2021 - It is a bit different to set up on a mac, than in windows. To keep it short, following the below steps helps set the required java home path. ... Pre-requisite: You must have java downloaded and installed on your system. Step 1: View the available java version on the system. Open up terminal and ...
Find elsewhere
🌐
Stony Brook University
www3.cs.stonybrook.edu › ~alexkuhn › cse114-fall2020 › resources › installJavaOnMac.html
Java Compiler On Mac OS X
July 28, 2020 - Finder -> Applications [task bar lower right] -> Utilities -> Terminal (or Terminal.app) By now, you are talking to the OSX Unix operating system. Type in the following command to the Unix prompt: java -version and see what you get. It will tell you which version of Java your system is actually ...
🌐
Macworld
macworld.com › home › how-to
Java is a great programming language to learn. Here's how to set it up on a Mac
January 9, 2023 - Open the download file (ours is ... Check that Version 8 Update 74 (or later) is installed. Alternatively open Terminal and enter “java -version.”...
🌐
Reddit
reddit.com › r/learnjava › how do i compile a program via terminal on macbook?
r/learnjava on Reddit: How do I compile a program via terminal on macbook?
December 22, 2021 -

SOLVED

Hello All. I'm new to programming and java. I'm trying to compile a program via terminal on a macbook and I keep running into errors. I was wondering what I need to change to fix this issue. First I changed the directory of my terminal to the folder that has the .java program in it. I ran "ls" to verify that my .java program was in that directory and it was. However, when I run javac Planet.java I get 60+ errors. This is just a small trial code nothing elaborate. Further info: I have had no issues compiling this code when using an online compiler called CodingGround found here: https://www.tutorialspoint.com/compile_java_online.php I just seem to have issues when using the terminal.

Here is my code (I removed spaces in case I had invisible characters that were causing me issues):

class Planet{
void revolve(){
System.out.println("Revolve");
}
public static void main(String[] args){
Planet earth = new Planet();
earth.revolve();
}
}

I have also tried making it a "public" class by changing the first line to public class Planet{ and still run into the same issues.

Picture of Errors Here:

https://postimg.cc/qNGHhnPk

https://postimg.cc/QK7RXWgZ

PS: Solution: was solved by thisisjustascreename: The text editor is the problem. Macbook's default TextEdit.app dose not automatically save in plain text. You have open the file and go to Format up at the top and then click on Make Plain Text and after saving it and making sure it is still just a .java file and run it via terminal with the javac command it will work just fine.

🌐
Stack Overflow
stackoverflow.com › questions › 47781552 › opening-terminal-in-mac-through-java
macos - Opening terminal in mac through java - Stack Overflow
December 13, 2017 - how to run a command at terminal from java program? (6 answers) Closed 8 years ago. I am using following code to open cmd in windows and execute the cd command and its working fine for me. Process p = Runtime.getRuntime().exec("cmd /c \" cd C:\\Users""); I want to do the same in mac by opening console, so what should i write instead of cmd ?
Top answer
1 of 2
9

To entirely use a (bash) shell to install e.g. Java 8u112 JRE do the following:

Download:

cd ~/Downloads
curl -v -j -k -L -H "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u112-b15/jre-8u112-macosx-x64.dmg > jre-8u112-macosx-x64.dmg

Mount:

hdiutil attach jre-8u112-macosx-x64.dmg

Install:

sudo installer -pkg /Volumes/Java\ 8\ Update\ 112/Java\ 8\ Update\ 112.app/Contents/Resources/JavaAppletPlugin.pkg -target /

Clean-up:

diskutil umount /Volumes/Java\ 8\ Update\ 112 
rm jre-8u112-macosx-x64.dmg

To install the latest Oracle JDK (at the time of writing this answer it's Java 8u121 JDK) the various paths (besides the version number) are slightly different compared to the older JRE version. The URL contains an additional directory (i.e. /e9e7ea248e2c4826b92b3f075a80e441) and the dmg file contains a .pkg file instead of an installer app:

cd ~/Downloads
curl -v -j -k -L -H "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/jdk-8u121-macosx-x64.dmg > jdk-8u121-macosx-x64.dmg
hdiutil attach jdk-8u121-macosx-x64.dmg
sudo installer -pkg /Volumes/JDK\ 8\ Update\ 121/JDK\ 8\ Update\ 121.pkg -target /
diskutil umount /Volumes/JDK\ 8\ Update\ 121
rm jdk-8u121-macosx-x64.dmg
2 of 2
25

The best/safest way to install Java on macOS is with Homebrew; just type this:

brew install oracle-jdk --cask

Or if you don’t have Homebrew installed already, just type this command first:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install oracle-jdk --cask
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › java › how to check java version on a mac: 2 simple methods
How to Quickly and Easily Check Java Version on a Mac
September 28, 2025 - To check the Java version on your Mac, open System Settings and select the "Java" menu item. You can open a terminal and type the command java -version to see your Java version.
🌐
Oracle
docs.oracle.com › javase › 9 › install › installation-jdk-and-jre-macos.htm
8 Installation of the JDK and the JRE on macOS
Enter the following in a Terminal window (note the escaped space character, ignore line break): % /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version ... If you have not yet installed Apple's Java OS X 2012-006 update, then you are still using a version of Apple Java 6 that includes the plug-in and the Java Preferences application. See Notes for Users of macOS That Include the Apple Java 6 Plug-in.
🌐
Uakron
cs.uakron.edu › ~xiao › isp › JavaOnMacOS.html
Installing Java on MacOS
Go to this link to download Java JDK. You can download the latest version if you wish · Download the .dmg file that says 'macOS Installer' Once it is downloaded, open the .dmg file and follow the instructions. You usually just have to click continue and instsll then enter your password at the end. Optional-> You can delete that .dmg file after it is installed · Now, go back to the Terminal and type in the same command: 'java -version' If you still do not see something like: 'java version "15" 2020-09-15', then you need to set up environment variable.
🌐
Medium
tejaksha-k.medium.com › a-step-by-step-guide-to-installing-java-on-macos-5188bfdf99d7
A Step-by-Step Guide to Installing Java on macOS | by Tejaksha K | Medium
July 29, 2024 - JDK includes the Java Runtime Environment (JRE) and development tools necessary for Java development. ... Once the JDK package is downloaded, locate the .dmg file, and double-click to open it. This will mount the disk image, and you will see the JDK package inside. Install JDK: Double-click the JDK package icon to start the installation process. Follow the on-screen instructions to complete the installation. Verify Installation: After installation, open Terminal again and enter the following command to verify that Java is installed correctly: