如何在亚马逊 ec2 上运行简单的 Java 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15933343/
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-10-31 21:19:42  来源:igfitidea点击:

How to run simple java app on amazon ec2?

javaamazon-ec2

提问by Sophie Sperner

I have access to amazon cloud service ec2, linux instance. I created vi first.javafile with this content:

我可以访问亚马逊云服务 ec2,linux 实例。我vi first.java用这个内容创建了文件:

class first {
    public static void main(String[] args) {
        System.out.println("abc");
    }
}

I want to compile the file using:

我想使用以下方法编译文件:

[root@ip-21-24-273-243 ec2-user]# javac first.java 
bash: javac: command not found

Command not found? I do:

没有找到指令?我做:

[root@ip-21-24-273-243 ec2-user]# java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.9) (amazon-57.1.11.9.52.amzn1-x86_64)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

So java is installed. How can I run a simple app?

这样java就安装好了。如何运行一个简单的应用程序?

[root@ip-21-24-273-243 ec2-user]# yum install java
Loaded plugins: priorities, security, update-motd, upgrade-helper
amzn-main                                                                                                                      | 2.1 kB     00:00     
amzn-updates                                                                                                                   | 2.3 kB     00:00     
Setting up Install Process
Package 1:java-1.6.0-openjdk-1.6.0.0-57.1.11.9.52.amzn1.x86_64 already installed and latest version
Nothing to do

回答by David Levesque

You need to install java-1.6.0-openjdk-devel:

你需要安装 java-1.6.0-openjdk-devel:

yum install java-devel

回答by piokuc

As already mentioned, to compile a Java program you need JDK. Here you may find some useful information on how to install JDK on a Fedora AMI: Compiling and running Java app

如前所述,要编译 Java 程序,您需要 JDK。在这里你可以找到一些关于如何在 Fedora AMI 上安装 JDK 的有用信息: 编译和运行 Java 应用程序

However, you should notice that you don't need to compile on your ec2 instance. You can compile your Java program on your home desktop/laptop computer and transfer compiled .class (packed in a .jar) files to the instance, and run them there - the already installed JRE should be enough to run the program. That is a preferable approach, cause you can comfortably use Eclipse for development. Develop, test on your local machine, deploy on ec2.

但是,您应该注意到您不需要在 ec2 实例上进行编译。您可以在家用台式机/笔记本电脑上编译 Java 程序,并将编译后的 .class(打包在 .jar 中)文件传输到实例,并在那里运行它们 - 已经安装的 JRE 应该足以运行该程序。这是一种更可取的方法,因为您可以轻松地使用 Eclipse 进行开发。在本地机器上开发、测试,在 ec2 上部署。

回答by captnolimar

There's a few steps to do:

有几个步骤要做:

  1. Make sure you have a Java compiler installed. You can find this out by entering $javacinto your console and seeing what comes up. If it's not installed, follow the previously mentioned instructions to $yum install java-devel
  2. Create your script (first.java)
  3. Compile it using $javac first.java
  4. You will now have first.javaand first.class... execute it by executing $java first
  1. 确保安装了 Java 编译器。您可以通过进入$javac控制台并查看出现的内容来找到这一点。如果未安装,请按照前面提到的说明进行操作$yum install java-devel
  2. 创建您的脚本 ( first.java)
  3. 编译它使用 $javac first.java
  4. 您现在将拥有first.javafirst.class...通过执行来执行它$java first

回答by Darshan Lila

In order to create and run a basic java program on Amazon EC2, you would require to do following:

为了在 上创建和运行基本的 Java 程序Amazon EC2,您需要执行以下操作:

  1. When you login to EC2instance, check whether JDKis installed or not, fire javac -versionfor it. If you do not have JDKinstalled you would see something like this: JDK not installed
  2. Install JDK, fire sudo rpm -i jdk-8u11-linux-x64.rpm
  3. Having java installed, you can create a file file.javausing any editor.
  4. Edit the file and save.
  5. Compile java File, javac file.java
  6. Run it, java file
  1. 当您登录EC2实例时,检查是否JDK已安装,为它启动javac -version。如果你还没有JDK安装,你会看到这样的: 未安装JDK
  2. 安装JDK,开火 sudo rpm -i jdk-8u11-linux-x64.rpm
  3. 安装 java 后,您可以file.java使用任何编辑器创建文件。
  4. 编辑文件并保存。
  5. 编译java文件, javac file.java
  6. 运行, java file

回答by Esko

"[Java] Runtime Environment" or "JRE" for short doesn't include the developer tools, one of them being the Java Compiler. You need to install Java Development Kit or "JDK" for short. The exact mechanism depends on which AMI you used to build your instance.

“[Java] 运行时环境”或简称“JRE”不包括开发人员工具,其中之一是 Java 编译器。您需要安装 Java Development Kit 或简称“JDK”。确切的机制取决于您用于构建实例的 AMI。