如何在Mac上编译和运行Java程序?

时间:2021-02-15 09:24:41

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

如何在我的mac上编译和运行Java程序?

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.

我还下载了一个程序,这个程序在这里被称为文本管理器,如果这与情况有任何关系。

4 个解决方案

#1


169  

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.

在Mac OSX或任何主要操作系统上编译和运行Java应用程序非常简单。 Apple包含一个功能齐全的Java运行时和OSX开箱即用的开发环境,因此您所要做的就是编写一个Java程序并使用内置工具来编译和运行它。

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.

第一步是编写一个简单的Java程序。打开文本编辑器(内置的TextEdit应用程序正常工作),输入以下代码,并将文件保存为主目录中的“HelloWorld.java”。

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.

例如,如果您的用户名是David,请将其另存为“/Users/David/HelloWorld.java”。这个简单的程序声明了一个名为HelloWorld的类,只有一个名为main的方法。 main方法在Java中是特殊的,因为它是Java运行时在您告诉它执行程序时将尝试调用的方法。将其视为您计划的起点。 System.out.println()方法将在屏幕上打印一行文本“Hello World!”在这个例子中。

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:

现在你已经编写了一个简单的Java程序,你需要编译它。运行终端应用程序,该应用程序位于“Applications / Utilities / Terminal.app”中。在终端中键入以下命令:

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.

您刚刚在OSX上编译了第一个Java应用程序,尽管它很简单。编译过程将生成一个名为“HelloWorld.class”的文件。此文件包含Java字节代码,这是Java虚拟机可以理解的指令。

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.

此命令将启动Java虚拟机并尝试加载名为HelloWorld的类。一旦加载该类,它将执行我之前提到的main方法。你应该看到“Hello World!”打印在终端窗口。这里的所有都是它的。

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.

作为旁注,TextWrangler只是OSX的文本编辑器,与此情况无关。您可以在此示例中将其用作文本编辑器,但肯定不是必需的。

#2


6  

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. 将代码保存为Paycheck.java
  3. Go to terminal and type cd Desktop
  4. 转到终端并键入cd Desktop
  5. Type javac Paycheck.java
  6. 输入javac Paycheck.java
  7. Type java Paycheck
  8. 输入java Paycheck
  9. Enjoy your program!
  10. 享受你的节目!

#3


3  

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

下载并安装Eclipse,你很高兴。 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/

Apple提供自己的Java版本,因此请确保它是最新的。 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:

Eclipse是一个集成的开发环境。它有许多功能,但在此阶段与您相关的功能是:

  • 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.

随着您获得更多经验,您将开始欣赏其余丰富的功能。

#4


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.)

您需要确保计算机上存在与mac兼容的java版本。从终端做java -version检查。如果没有,请从苹果网站下载apple jdk。 (Sun不会为苹果自己创造一个,IIRC。)

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

从那里,按照相同的命令行说明编译您将在任何其他平台上用于java的程序。

#1


169  

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.

在Mac OSX或任何主要操作系统上编译和运行Java应用程序非常简单。 Apple包含一个功能齐全的Java运行时和OSX开箱即用的开发环境,因此您所要做的就是编写一个Java程序并使用内置工具来编译和运行它。

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.

第一步是编写一个简单的Java程序。打开文本编辑器(内置的TextEdit应用程序正常工作),输入以下代码,并将文件保存为主目录中的“HelloWorld.java”。

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.

例如,如果您的用户名是David,请将其另存为“/Users/David/HelloWorld.java”。这个简单的程序声明了一个名为HelloWorld的类,只有一个名为main的方法。 main方法在Java中是特殊的,因为它是Java运行时在您告诉它执行程序时将尝试调用的方法。将其视为您计划的起点。 System.out.println()方法将在屏幕上打印一行文本“Hello World!”在这个例子中。

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:

现在你已经编写了一个简单的Java程序,你需要编译它。运行终端应用程序,该应用程序位于“Applications / Utilities / Terminal.app”中。在终端中键入以下命令:

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.

您刚刚在OSX上编译了第一个Java应用程序,尽管它很简单。编译过程将生成一个名为“HelloWorld.class”的文件。此文件包含Java字节代码,这是Java虚拟机可以理解的指令。

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.

此命令将启动Java虚拟机并尝试加载名为HelloWorld的类。一旦加载该类,它将执行我之前提到的main方法。你应该看到“Hello World!”打印在终端窗口。这里的所有都是它的。

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.

作为旁注,TextWrangler只是OSX的文本编辑器,与此情况无关。您可以在此示例中将其用作文本编辑器,但肯定不是必需的。

#2


6  

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. 将代码保存为Paycheck.java
  3. Go to terminal and type cd Desktop
  4. 转到终端并键入cd Desktop
  5. Type javac Paycheck.java
  6. 输入javac Paycheck.java
  7. Type java Paycheck
  8. 输入java Paycheck
  9. Enjoy your program!
  10. 享受你的节目!

#3


3  

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

下载并安装Eclipse,你很高兴。 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/

Apple提供自己的Java版本,因此请确保它是最新的。 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:

Eclipse是一个集成的开发环境。它有许多功能,但在此阶段与您相关的功能是:

  • 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.

随着您获得更多经验,您将开始欣赏其余丰富的功能。

#4


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.)

您需要确保计算机上存在与mac兼容的java版本。从终端做java -version检查。如果没有,请从苹果网站下载apple jdk。 (Sun不会为苹果自己创造一个,IIRC。)

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

从那里,按照相同的命令行说明编译您将在任何其他平台上用于java的程序。