如何在EC2实例中使AWS Java应用编译?

huangapple go评论90阅读模式
英文:

How do you get an AWS Java application compile in EC2 instance?

问题

在AWS的EC2实例上编译我的.java文件时遇到了一些问题。我在本地创建文件,那里可以正常运行。然后,我将.java文件移动到我的EC2实例,并尝试使用以下命令进行编译:

javac imageRecognition.java

但是我遇到了如下错误:

imageRecognition.java:5: 错误: 找不到包com.amazonaws.regions
import com.amazonaws.regions.Regions;
                            ^
imageRecognition.java:6: 错误: 找不到包com.amazonaws.services.rekognition
import com.amazonaws.services.rekognition.AmazonRekognition;

我猜这是因为EC2实例上没有这些包,但是我不确定如何下载/放置它们。或者是否需要完全不同的操作。任何帮助将不胜感激。

注:我必须在EC2实例上编译文件,不能在实例上移动可执行的.jar文件或类似文件。

英文:

Been having some trouble trying to get my .java file to compile on an EC2 instance in AWS. I create the file on my local and it runs just fine there. I then move the .java file into my EC2 instance and try to compile it with the following:

javac imageRecognition.java

But I end up with errors like so:

imageRecognition.java:5: error: package com.amazonaws.regions does not exist
import com.amazonaws.regions.Regions;
                            ^
imageRecognition.java:6: error: package com.amazonaws.services.rekognition does not exist
import com.amazonaws.services.rekognition.AmazonRekognition;

I'm assuming it's cause there are no packages on the EC2 instance, but I am unsure how to download/where to put them. Or if there is something completely different I need to do. Any help would be appreciated.

Note: I have to compile it the file on the EC2 instance, I cannot move an executable .jar on the instance or the like.

答案1

得分: 1

如果你打算使用javac,你需要将jar包添加到类路径中,你可以从AWS SDK Java下载该jar包。然而,这是一种非常老旧的做法,你可能会遇到依赖问题,因为它还需要其他库。如果我是你,我会考虑使用类似Maven或者Gradle这样的构建工具来帮助处理这种情况。

英文:

If you are going to use javac you are going to need to have the jar on the classpath which you can download from AWS SDK Java. However this is a really old-school way to do it and you will probably run into dependency hell as it will need other libraries. If I were you I would check out a build tool like Maven or Gradle which helps with this kind of thing.

答案2

得分: 1

似乎你的 EC2 实例中缺少 AWS SDK 包。你可以尝试使用 AWS CodePipeline 功能来进行代码构建和部署。

使用 CodeBuild 来构建代码包,然后使用 CodeDeploy 将可执行文件部署到 EC2 实例中。或者你也可以使用其他的 CI/CD 工具来进行代码构建和部署。

英文:

Looks like AWS SDK package is missing in your EC2 instance. You can try using AWS Code Pipeline feature to get the code build and deploy.

Use Code Build to build the package and Code deploy to deploy the executable file into EC2. Or use any other CI/CD to build and deploy the code.

答案3

得分: 0

最好使用Maven或Gradle。我在这种情况下使用了Maven,并按以下方式添加了AWS依赖项:

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk -->
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.11.863</version>
</dependency>

然后只需安装Maven并运行 mvn install

英文:

Better use maven or gradle. I used maven in my case for such, and added AWS dependency as follows:

&lt;!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
    &lt;artifactId&gt;aws-java-sdk&lt;/artifactId&gt;
    &lt;version&gt;1.11.863&lt;/version&gt;
&lt;/dependency&gt;

Then simply install maven and run mvn install

huangapple
  • 本文由 发表于 2020年9月18日 01:18:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63943141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定