How to compile and run a program in Java on my Mac?

How to compile and run a program in Java on my Mac?

I'm new at this.

I also downloaded the program that was offered to me here, called text wrangler, if this has anything to do with the situation.

+91
java compilation macos
Mar 02
source share
5 answers

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.

+178
Mar 02 '10 at 5:30
source share
β€” -

I will give you the steps to write and compile the 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."); } } 
  • Save the code as Paycheck.java
  • Go to terminal and type cd Desktop
  • Type javac Paycheck.java
  • Type java Paycheck
  • Enjoy your program!
+6
Nov 01 '14 at
source share

Download and install Eclipse and you will be fine. http://www.eclipse.org/downloads/

Apple provides its own version of Java, so make sure it is updated.
http://developer.apple.com/java/download/




Eclipse is an integrated development environment . It has many functions, but those that are important to you at this stage:

  • Source code editor
    • With syntax highlighting, colors, and other visual cues
    • A simple cross-reference to documentation for easy learning.
  • Compiler
    • Run the code with one click
    • Receive error / error notifications when you go.

As you gain more experience, you will begin to appreciate the rest of your rich feature set.

+3
Mar 02
source share

You need to make sure that your computer has a Java compatible version of java. Make java -version from the terminal to check this out. If not, download apple jdk from the apple website. (The sun does not do one for the apple itself, IIRC.)

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

+1
Mar 02
source share

Other solutions are good enough to answer your request. However, if you are looking for only one team to do this for you -

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

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

give this file permission to run by running -

 chmod 777 

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

 ./run <yourfilename> (don't add .java in filename) 
0
Dec 07 '18 at 23:34
source share



All Articles