How to run simple java app on amazon ec2?

19,587

Solution 1

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

yum install java-devel

Solution 2

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.

Solution 3

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
Share:
19,587
Sophie Sperner
Author by

Sophie Sperner

Smart & happy blond! ))))

Updated on July 24, 2022

Comments

  • Sophie Sperner
    Sophie Sperner almost 2 years

    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
    
  • piokuc
    piokuc about 11 years
    right, sorry, yum is used in Fedora, Ubuntu has apt-get. Anyway, I recommend setting up development environment on your personal computer.
  • piokuc
    piokuc about 11 years
    Development on your local machine is just so much more convenient, with Eclipse and fast access to files. In theory I think you could run Eclipse remotely, but it's never going to work as fast as on your local computer.
  • piokuc
    piokuc about 11 years
    I guess you are on AWS free tier. Copying few megabytes with scp should be free.