如何在 Mac 上用 Java 编译和运行程序? [英] How do I compile and run a program in Java on my Mac?

查看:97
本文介绍了如何在 Mac 上用 Java 编译和运行程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我是新来的.

此外,我还下载了一个在此处向我推荐的程序,称为 text wrangler,如果这对情况有任何影响的话.

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

推荐答案

在 Mac OSX 或任何主要操作系统上编译和运行 Java 应用程序非常容易.Apple 在 OSX 中包含了一个开箱即用的全功能 Java 运行时和开发环境,因此您只需编写一个 Java 程序并使用内置工具来编译和运行它.

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.

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

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!");
    }
}

例如,如果您的用户名是 David,则将其保存为/Users/David/HelloWorld.java".这个简单的程序声明了一个名为 HelloWorld 的类,以及一个名为 main 的方法.main 方法在 Java 中很特殊,因为它是 Java 运行时在您告诉它执行程序时将尝试调用的方法.将其视为程序的起点.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.

现在您已经编写了一个简单的 Java 程序,您需要对其进行编译.运行位于Applications/Utilities/Terminal.app"中的终端应用程序.在终端中输入以下命令:

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

您刚刚在 OSX 上编译了您的第一个 Java 应用程序,尽管它很简单.编译过程将生成一个名为HelloWorld.class"的文件.该文件包含Java字节码,是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.

要运行该程序,请在终端中键入以下命令.

To run the program, type the following command in the terminal.

java HelloWorld

此命令将启动 Java 虚拟机并尝试加载名为 HelloWorld 的类.一旦它加载了那个类,它就会执行我之前提到的 main 方法.你应该看到Hello World!"打印在终端窗口中.这就是全部.

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.

顺便提一下,TextWrangler 只是 OSX 的文本编辑器,与这种情况无关.您可以将其用作本示例中的文本编辑器,但这当然不是必需的.

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.

这篇关于如何在 Mac 上用 Java 编译和运行程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆