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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2360995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:33:10  来源:igfitidea点击:

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

javamacoscompilation

提问by David

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.

我还下载了一个在此处向我建议的程序,称为 text wrangler,如果这与情况有关的话。

采纳答案by William Brendel

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 在 OSX 中包含一个功能齐全的 Java 运行时和开箱即用的开发环境,因此您只需编写一个 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 mainmethod 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 mainmethod 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 的文本编辑器,与这种情况无关。您可以将其用作本示例中的文本编辑器,但这当然不是必需的。

回答by Rob Lachlan

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 来检查。如果没有,请从苹果网站下载苹果 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 的程序相同的命令行指令进行编译。

回答by polygenelubricants

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.

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

回答by Rishi Shah

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

回答by Darpan

Other solutions are good enough to answer your query. However, if you are looking for just one command to do that for you -

其他解决方案足以回答您的查询。但是,如果您只需要一个命令来为您执行此操作 -

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

在 Java 文件所在的目录中创建一个名为“run”的文件。并将其保存在您的文件中 -

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

give this file run permission by running -

通过运行授予此文件运行权限 -

chmod 777 

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

现在您只需运行即可运行任何文件 -

./run <yourfilename> (don't add .java in filename)