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 › 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
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!
Discussions

macos - How To Run Mac OS Terminal Commands From Java (Using Runtime?) - Stack Overflow
If a unix cmd (or use the Unix .../Contents/MacOS/AppName) then there are two ways to fix this · Put the full path to the executable in the Java code e.g. String[] cmd = {"/full/absolute/path/to/love", "/Users/mtc06/testgame"}; Alter the path to include the executable. This depends on the method java is launched. a)If java is run from the command line then ... More on stackoverflow.com
🌐 stackoverflow.com
How to open Terminal and execute command using java code in OSX - Stack Overflow
How to launch terminal and execute some commands using java code in MAC? Similar question i have found for LINUX OS from below link. how to run a command at terminal from java program? More on stackoverflow.com
🌐 stackoverflow.com
macos - How to have java run terminal commands on Mac? (Echo command) - Stack Overflow
How do you have java run commands on Mac? I see some examples of complex commands that is hard to follow. If I wanted to run a simple echo command from java, how would I do that? Not using osascrip... 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
🌐
Princeton CS
introcs.cs.princeton.edu › java › mac › terminal.php
Using Java from the Terminal in Mac OS X
This document instructs you on ... 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 to execute it. Follow these instructions for installing a programming environment on Mac OS X if you have not already done so. You will type commands in an application ...
🌐
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
🌐
O'Reilly
oreilly.com › library › view › mac-os-x › 0596006063 › ch16s04.html
Java on the Command Line - Mac OS X Panther in a Nutshell, 2nd Edition [Book]
In this case, the -jar option tells the Java interpreter to launch a program encapsulated in a jar file. This works only if the jar file knows its own Main-class, which defines the main method. jar files, like Mac OS X applications, are self-contained and ignore the user’s class path definitions. This section involves using the Terminal. If you are completely unfamiliar with this application or with using a Unix command ...
🌐
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...
🌐
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.
Find elsewhere
🌐
Stony Brook University
www3.cs.stonybrook.edu › ~amione › CSE114_Course › materials › resources › installJavaOnMac.html
Java Compiler On Mac OS X
July 28, 2020 - Double click on the file to install it. This will show a .pkg file. Double click on the package and follow the prompts. Just accept defaults along the way (and agree to the license terms). Now, open a Terminal window as before, and try the 'java -version' command ...
🌐
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 - Step 2: Assign the java home with the value. Pick the required java version to be set and execute the below command with change to the java version as required. export JAVA_HOME=”/usr/libexec/java_home -v 1.6.0_65-b14–462 ... The appropriate java version would be displayed in the terminal.
🌐
Stack Overflow
stackoverflow.com › questions › 23650193 › how-to-open-terminal-and-execute-command-using-java-code-in-osx
How to open Terminal and execute command using java code in OSX - Stack Overflow
String action1 = jsonDriver.readApplicationDetails("OFF"); Runtime runtime = Runtime.getRuntime(); String applescriptCommand = "tell application \"Terminal\"\n" + "activate\n" + "delay 1\n" + "tell application \"System Events\"\n" + "keystroke \"" + action1 + "\"\n" + "end tell\n" + "end tell"; String[] args1 = { "osascript", "-e", applescriptCommand }; Process process = runtime.exec(args1); Thread.sleep(5000);
🌐
Stack Overflow
stackoverflow.com › questions › 62329459 › how-to-have-java-run-terminal-commands-on-mac-echo-command
macos - How to have java run terminal commands on Mac? (Echo command) - Stack Overflow
import java.io.IOException; import java.lang.ProcessBuilder; public class PrcBldT2 { public static void main(String[] args) { // This command is for Windows operating system. // For MacOS, try: new ProcessBuilder("echo", "hi") ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "echo", "hi"); pb.redirectError(ProcessBuilder.Redirect.INHERIT); pb.redirectOutput(ProcessBuilder.Redirect.INHERIT); try { Process p = pb.start(); int result = p.waitFor(); System.out.println("Exit status = " + result); } catch (IOException | InterruptedException x) { x.printStackTrace(); } } }
🌐
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.

🌐
Apple Community
discussions.apple.com › thread › 250048237
Java Command Line Tool - Apple Community
December 28, 2018 - JDK popup on Mojave After installing ... the java command-line tool you need to install a JDK" and provides a link to the Java Developer Kit website. I had the same problem with El Capitan but found a link in Apple support that successfully got rid of it, but can no longer locate. Can anyone help? 6051 3 · Uninstalled Java but it still shows up in Terminal I had downloaded both the JDK 20.0.2 and the Java 8 Update 381 on my MacBook Pro M1 2020 ...
🌐
Coderanch
coderanch.com › t › 753235 › java › Command-Line-mac
Command Line on mac (Beginning Java forum at Coderanch)
July 23, 2022 - If not, you should be able to easily install OpenJDK from the MacOS package repository. The problem with getting rid of the "undesirables" is that sooner or later someone will decide that YOU are an undesirable. ... javac -version openjdk version "18.0.2" 2022-07-19 OpenJDK Runtime Environment (build 18.0.2+9-61) OpenJDK 64-Bit Server VM (build 18.0.2+9-61, mixed mode, sharing) javac 18.0.2
🌐
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 Terminal. Enter mkdir HelloWorld to create a new directory and cd HelloWorld to move into it. Enter touch HelloWorld.java to create an empty Java file.
🌐
Stack Overflow
stackoverflow.com › questions › 55633912 › running-a-command-in-macos-terminal-from-java
Running a command in MacOs terminal from java - Stack Overflow
String[] arguments = new String[] {"/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal", "-c", "pwd"}; Process proc = new ProcessBuilder(arguments).start(); The code seems to run fine until it reaches the "-c" parameter, and then it does nothing else. Does someone know how to solve this problem? Thanks! EDIT: I've also tried without the "-c", but same result. ... The reason why this doesn't work from Java is that it doesn't work from anywhere. You should first find a command that can run a program in a new terminal, and only then try to invoke that from Java.
🌐
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 - . It's the icon of a small black box with a white command prompt in the corner. This will open your Mac's Terminal. ... Type java -version and press ⏎ Return.