简体   繁体   中英

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

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.

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.

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.

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

<!-- 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>

Then simply install maven and run mvn install

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM