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

查看:5518
本文介绍了如何在我的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包括一个完全功能的Java运行时和OSX开箱即用的开发环境,所以你要做的就是编写一个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程序,编译它。运行Terminal应用程序,它位于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天全站免登陆