How do I compile and run a program in Java on my Mac?
Asked Answered
B

6

114

How do I compile and run a program in Java on my mac?

I'm new.

Also I downloaded a program that was suggested to me on here called text wrangler if that has any bearing on the situation.

Brisco answered 2/3, 2010 at 4:58 Comment(0)
P
211

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.

Packthread answered 2/3, 2010 at 5:30 Comment(6)
this happened: Last login: Mon Mar 1 23:41:53 on ttys000 david-allenders-macbook-pro:~ davidallender$ cd ~ david-allenders-macbook-pro:~ davidallender$ javac Helloworld.java error: cannot read: Helloworld.java 1 error david-allenders-macbook-pro:~ davidallender$ what did i do wrong?Brisco
It looks like you either saved the file in the wrong directory or saved it using an incorrect filename. Double check your home directory in the finder for a file named "HelloWorld.java". Case is also important here, so make sure it is called "HelloWorld.java", not "Helloworld.java".Packthread
Ok. i followed your instructions better and everything worked. thank you for your help. i really appreciate it.Brisco
Are every programs in Java .class files ? I've never seen this, OSX or PC. Is there another way of compiling the program so that it'll be usable by most people ?Ariminum
I had the problem that textEdit automatically saves the file as .rtf (rich text format) and doesn't allow .java . The way to solve this is in textEdit do format>makePlainText.Danialdaniala
@SteveChamaillard Use github.com/oracle/graal/tree/master/substratevm to compile to a normal executablePercept
M
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!
Micahmicawber answered 1/11, 2014 at 20:59 Comment(1)
FYI if you have a package name at the top of the file (and you are running a single java file), you will see Error: Could not find or load main class <package name>. In this instance, just comment out the package by putting two slashes at the startOberg
S
3

Download and install Eclipse, and you're good to go.
http://www.eclipse.org/downloads/

Apple provides its own version of Java, so make sure it's up-to-date.
http://developer.apple.com/java/download/


Eclipse is an integrated development environment. It has many features, but the ones that are relevant for you at this stage is:

  • The source code editor
    • With syntax highlighting, colors and other visual cues
    • Easy cross-referencing to the documentation to facilitate learning
  • Compiler
    • Run the code with one click
    • Get notified of errors/mistakes as you go

As you gain more experience, you'll start to appreciate the rest of its rich set of features.

Spaniard answered 2/3, 2010 at 5:19 Comment(2)
@Brisco From Dictionary, "an obscuring of the light from one celestial body by the passage of another between it and the observer or between it and its source of illumination: an eclipse of the sun". but in this case, a great IDE for JAVA development.Santonin
You can also use NetBeans as an alternativeOberg
K
1

You need to make sure that a mac compatible version of java exists on your computer. Do java -version from terminal to check that. If not, download the apple jdk from the apple website. (Sun doesn't make one for apple themselves, IIRC.)

From there, follow the same command line instructions from compiling your program that you would use for java on any other platform.

Kemerovo answered 2/3, 2010 at 5:4 Comment(1)
@rob, i think you underestimate how new i am at all this. COuld you please explain all that more simply.Brisco
C
0

Other solutions are good enough to answer your query. However, if you are looking for just one command to do that for you -

Create a file name "run", in directory where your Java files are. And save this in your file -

javac "$1.java"
if [ $? -eq 0 ]; then
  echo "--------Run output-------"
  java "$1"
fi

give this file run permission by running -

chmod 777 

Now you can run any of your files by merely running -

./run <yourfilename> (don't add .java in filename)
Clergy answered 7/12, 2018 at 23:34 Comment(0)
A
0

First of all, check if you have java JDK, open cmd or terminal, type java -version. You should see the version.

To run a simple program with java, open file in desktop with test.java name, then write the following code;

public class test {
    public static void main(String[] args) {
        System.out.println("This is a test"); 
    }
}
  1. Remember to save it as test.java
  2. Go to terminal and type cd Desktop
  3. To compile java file type javac test.java
  4. To see the result just type without .class, only java test

You will see "This is a test"

Assumpsit answered 21/3 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.