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
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!
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › mac-cmd.html
Java and the Mac OS X Terminal
Try running your program with the command line · machine:~/introcs/hello wayne$ java -cp ./ HelloWorld If this works, your classpath is set incorrectly. I get the error "class file has wrong version 50.0, should be 49.0" when I compile from the Terminal. What does this mean? It's probably because DrJava is configured to use Java 6.0 and and your Terminal is configured to use Java 5.0.
Discussions

macos - How do I run a Java program on Terminal (Mac)? - Stack Overflow
I opened TextWrangler and selected "Run in Terminal" and it returned this error: "This file doesn't appear to contain a valid 'shebang' line (application error code: 13304)" My program is named "hello.java" and it contains the code below. ... Of course, both javac and java must be available in the operating system's PATH variable, but in a Mac ... 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 - How to run a Java program from mac terminal? - Stack Overflow
I've downloaded the source code for Java program (Mario AI Benchmark) that I'd like to work with to eventually integrate some Python scripts. No matter what .java file I try running I get the same ... More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
macos - How to run a Java Class in Terminal - Stack Overflow
I followed all the instructions on how to run a class in MacOs Terminal, but all I got for an answer in my terminal instead of my program was: Danylo-RIB:~ mac$ java /Users/mac/Documents/workspace/Calculator/bin/Calculator.class Error: Could not find or load main class .Users.mac.Documents... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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...
🌐
YouTube
youtube.com › watch
How To Compile And Run Java Programs Using Terminal in MacOS M1/M2 - YouTube
In this Video we will see How we can Compile and Run Java Program from Terminal in Mac OS, And in the same way we can run from the Windows Machine.Commands U...
Published   March 22, 2023
🌐
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.

🌐
YouTube
youtube.com › watch
Run Java using Terminal on MacOS | Compile and Run java using Command Line - YouTube
In this video i have shown how you can compile and run java program using terminal or command line on your MacOS ( M1/ M2 ). Timestamp : 00:00 - Introduction...
Published   March 11, 2023
Find elsewhere
🌐
YouTube
youtube.com › watch
Compile and Run a Java Program Using Command Line Terminal on Mac - YouTube
In this demo we create a simple Hello World java program on Mac using TextEdit. We then compile it and run it using the Terminal.
Published   September 3, 2018
🌐
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.
🌐
JavaForge
javaforge.com › home › how to run java on mac
Run Java on Mac: Complete Setup & Programming Guide - JavaForge
March 28, 2024 - In the main window, you’ll find the HelloWorld.java file. Enter the following code: ... Save your program via File > Save, and run it by clicking the green Run Project icon. For those preferring the Terminal, here’s how to compile and execute a Java program:
🌐
Medium
medium.com › @seymagizem › how-to-run-java-program-file-via-macos-terminal-7e64a731b585
How to Run Java Program File via MacOs Terminal? | by Gizem Sivri | Medium
July 28, 2023 - First of all click on spotlight search for textedit click on it and click on new document. ... Secondly you have to change the format into plain text so its work like in notepad just like windows.
🌐
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 - Now compile the program by entering javac HelloWorld.java in Terminal. Enter java HelloWorld to run it. You should see Hello World outputted into the Terminal. You are now completely set up to learn Java on your Mac.
🌐
Princeton CS
introcs.cs.princeton.edu › java › mac › terminal.php
Using Java from the Terminal in Mac OS X
This document instructs you on ... 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 to execute it....
🌐
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
🌐
javaspring
javaspring.net › blog › how-to-run-java-in-mac
How to Run Java on a Mac — javaspring.net
Open the terminal, navigate to the directory where HelloWorld.java is located, and compile the program using the javac command: ... This will generate a HelloWorld.class file in the same directory. Run the compiled Java program using the java command:
🌐
Reddit
reddit.com › r/javahelp › how can i compile and run java on my mac? do i need to install any program? or can i just use the terminal application?
r/javahelp on Reddit: How can I compile and run Java on my mac? do I need to install any program? or can I just use the Terminal application?
October 14, 2014 - Then run what you've made with Terminal. ... Mac comes with the JDK pre installed most of the time. And it is going to be run through a command line with the command javac to compile and then java to run iirc.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-execute-and-run-java-code
How to Execute and Run Java Code from the Terminal
March 10, 2022 - So if we simply copy the directory and add the .class file name without the extension ( .class ) later with a period ( . ), then it satisfies the condition for executing any Java code that has packages declared in the source code. The same process is also applicable for the other operating systems as well. I am providing screenshots from a Linux OS here: Running Java codes having packages within a Linux Machine · Great job! 👏 You can now run any Java code/programs directly using a terminal.
🌐
Uvic
connex.csc.uvic.ca › access › content › group › cscassist › public › website › osx_jdk_tutorial › osx_jdk_tutorial.html
Computer Science Assistance Centre
This tutorial will guide you through the process of creating and testing a simple Java program on Mac OS X. Recent versions of OS X come with the Java compiler (javac) already installed, so there is no need to install it yourself. If you have an older Mac, or if the javac command does not work ...