Sophie Sperner
Sophie Sperner

Reputation: 4556

How to run simple java app on amazon ec2?

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

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?

[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

Upvotes: 11

Views: 24599

Answers (5)

Darshan Lila
Darshan Lila

Reputation: 5868

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

  1. When you login to EC2 instance, check whether JDKis installed or not, fire javac -version for it. If you do not have JDK installed 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.java using any editor.
  4. Edit the file and save.
  5. Compile java File, javac file.java
  6. Run it, java file

Upvotes: 0

captnolimar
captnolimar

Reputation: 105

There's a few steps to do:

  1. Make sure you have a Java compiler installed. You can find this out by entering $javac into 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.java and first.class... execute it by executing $java first

Upvotes: 1

David Levesque
David Levesque

Reputation: 22451

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

yum install java-devel

Upvotes: 16

piokuc
piokuc

Reputation: 26204

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

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.

Upvotes: 2

Esko
Esko

Reputation: 29385

"[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.

Upvotes: 0

Related Questions