[Q#2360995][A#2361108] How do I compile and run a program in Java on my Mac?
https://stackoverflow.com/q/2360995
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.
Answer
https://stackoverflow.com/a/2361108
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. 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. 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. 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: 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. To run the program, type the following command in the terminal. 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.
APIzation
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World!");
}
}
cd ~
javac HelloWorld.java
java HelloWorld
package com.stackoverflow.api;
public class Human2361108 {
public static void printHello() {
System.out.println("Hello World!");
}
}
package com.stackoverflow.api;
/**
* How do I compile and run a program in Java on my Mac?
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/2361108">https://stackoverflow.com/a/2361108</a>
*/
public class APIzator2361108 {
public static void compile() {
System.out.println("Hello World!");
}
}