Compiling and running a Java application on Mac OSX or on any major operating system is very simple. Apple includes a fully functional Java runtime and an OSX development environment, 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
At the first stage, a simple Java program is written. Open a text editor (the built-in TextEdit application works fine), enter 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 one class called HelloWorld one method called main . The main method is special in Java because it is the method that the Java runtime will execute when you tell it to execute your program. Think of this as a starting point for your program. The System.out.println() method prints a line of text on the "Hello World!" Screen. in this example.
Using compiler
Now that you have written a simple Java program, you need to compile it. Launch the "Terminal" application, which is located in the "Applications / Utilities / Terminal.app" section. Enter the following commands into the terminal:
cd ~ javac HelloWorld.java
You just compiled your first Java application, albeit simple, on OSX. During compilation, a single file will be created called "HelloWorld.class". This file contains Java byte codes, which are instructions that the Java Virtual Machine understands.
Program launch
To start the program, enter the following command into the terminal.
java HelloWorld
This command will start the Java virtual machine and try to load the class called HelloWorld . When it loads this class, it will execute the main method, which I mentioned earlier. You should see "Hello World!" printed in a terminal window. That is all that is needed.
As a note, TextWrangler is just a text editor for OSX and has nothing to do with this situation. You can use it as a text editor in this example, but this is optional.
William Brendel Mar 02 '10 at 5:30 2010-03-02 05:30
source share