AWS Cloud9でJava8開発環境を構築

## はじめに
AWS Cloud9導入編はこちら
https://qiita.com/tu-kun/items/d7b4f1fa19cc93bc5b75

## 対象
- AWS Cloud9でJava8にバージョンアップしたい人
- よく分からないけどJava8を導入したい人

## バージョンの確認方法
### Java
```bash
$ java -version
java version "1.7.0_181"
OpenJDK Runtime Environment (amzn-x.x.xx.x.xx.amzn1-x86_64 u181-b00)
OpenJDK 64-Bit Server VM (build xx.xxx-b00, mixed mode)
```
### Javac
```bash
$ javac -version
javac 1.7.0_181
```

## パッケージの確認
java8のパッケージは`java-1.8.0-openjdk-devel`です。
こちらは開発者向けOpenJDKも一緒にインストールされるパッケージとなります。

```bash
$ yum search java-1.8.0-openjdk-devel
Loaded plugins: priorities, update-motd, upgrade-helper
epel                                                                                                                 12509/12509
1045 packages excluded due to repository priority protections
================================================ N/S matched: java-1.8.0-openjdk ================================================
java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.8.0-openjdk-headless.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.8.0-openjdk-javadoc-zip.noarch : OpenJDK API Documentation compressed in single archive
java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle
```

## パッケージの導入
yum installはrootユーザーで実行しないといけないため、sudoコマンドを先頭につけます。
※ 途中で`Is this ok [y/d/N]:`と対話する箇所があるので`y`を入力

```bash
$ sudo yum install java-1.8.0-openjdk-devel                                                           
Loaded plugins: priorities, update-motd, upgrade-helper
amzn-main                                                                                                 | 2.1 kB  00:00:00     
amzn-updates                                                                                              | 2.5 kB  00:00:00     
1045 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check
---> Package java-1.8.0-openjdk-devel.x86_64 1:1.8.0.171-7.b10.37.amzn1 will be installed
--> Processing Dependency: java-1.8.0-openjdk(x86-64) = 1:1.8.0.171-7.b10.37.amzn1 for package: 1:java-1.8.0-openjdk-devel-1.8.0.171-7.b10.37.amzn1.x86_64
--> Processing Dependency: libawt_xawt.so(SUNWprivate_1.1)(64bit) for package: 1:java-1.8.0-openjdk-devel-1.8.0.171-7.b10.37.amzn1.x86_64
--> Processing Dependency: libawt_xawt.so()(64bit) for package: 1:java-1.8.0-openjdk-devel-1.8.0.171-7.b10.37.amzn1.x86_64
--> Running transaction check
---> Package java-1.8.0-openjdk.x86_64 1:1.8.0.171-7.b10.37.amzn1 will be installed
--> Processing Dependency: java-1.8.0-openjdk-headless(x86-64) = 1:1.8.0.171-7.b10.37.amzn1 for package: 1:java-1.8.0-openjdk-1.8.0.171-7.b10.37.amzn1.x86_64
--> Running transaction check
---> Package java-1.8.0-openjdk-headless.x86_64 1:1.8.0.171-7.b10.37.amzn1 will be installed
amzn-main/latest/filelists_db                                                                             | 5.7 MB  00:00:13     
amzn-updates/latest/filelists_db                                                                          | 1.2 MB  00:00:00     
epel/x86_64/filelists                                                                                     | 7.3 MB  00:00:01     
--> Processing Dependency: copy-jdk-configs >= 1.1-3 for package: 1:java-1.8.0-openjdk-headless-1.8.0.171-7.b10.37.amzn1.x86_64
--> Processing Dependency: lksctp-tools(x86-64) for package: 1:java-1.8.0-openjdk-headless-1.8.0.171-7.b10.37.amzn1.x86_64
--> Running transaction check
---> Package copy-jdk-configs.noarch 0:1.2-1.2.amzn1 will be installed
---> Package lksctp-tools.x86_64 0:1.0.10-7.7.amzn1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================
 Package                                Arch              Version                                  Repository               Size
=================================================================================================================================
Installing:
 java-1.8.0-openjdk-devel               x86_64            1:1.8.0.171-7.b10.37.amzn1               amzn-updates             12 M
Installing for dependencies:
 copy-jdk-configs                       noarch            1.2-1.2.amzn1                            amzn-main                13 k
 java-1.8.0-openjdk                     x86_64            1:1.8.0.171-7.b10.37.amzn1               amzn-updates            249 k
 java-1.8.0-openjdk-headless            x86_64            1:1.8.0.171-7.b10.37.amzn1               amzn-updates             39 M
 lksctp-tools                           x86_64            1.0.10-7.7.amzn1                         amzn-main                89 k

Transaction Summary
=================================================================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 51 M
Installed size: 144 M
Is this ok [y/d/N]: Y
Downloading packages:
(1/5): copy-jdk-configs-1.2-1.2.amzn1.noarch.rpm                                                          |  13 kB  00:00:00     
(2/5): java-1.8.0-openjdk-1.8.0.171-7.b10.37.amzn1.x86_64.rpm                                             | 249 kB  00:00:05     
(3/5): lksctp-tools-1.0.10-7.7.amzn1.x86_64.rpm                                                           |  89 kB  00:00:02     
(4/5): java-1.8.0-openjdk-devel-1.8.0.171-7.b10.37.amzn1.x86_64.rpm                                       |  12 MB  00:00:15     
(5/5): java-1.8.0-openjdk-headless-1.8.0.171-7.b10.37.amzn1.x86_64.rpm                                    |  39 MB  00:00:16     
---------------------------------------------------------------------------------------------------------------------------------
Total                                                                                            2.3 MB/s |  51 MB  00:00:21     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : copy-jdk-configs-1.2-1.2.amzn1.noarch                                                                         1/5 
  Installing : lksctp-tools-1.0.10-7.7.amzn1.x86_64                                                                          2/5 
  Installing : 1:java-1.8.0-openjdk-1.8.0.171-7.b10.37.amzn1.x86_64                                                          3/5 
  Installing : 1:java-1.8.0-openjdk-headless-1.8.0.171-7.b10.37.amzn1.x86_64                                                 4/5 
  Installing : 1:java-1.8.0-openjdk-devel-1.8.0.171-7.b10.37.amzn1.x86_64                                                    5/5 
  Verifying  : 1:java-1.8.0-openjdk-devel-1.8.0.171-7.b10.37.amzn1.x86_64                                                    1/5 
  Verifying  : 1:java-1.8.0-openjdk-1.8.0.171-7.b10.37.amzn1.x86_64                                                          2/5 
  Verifying  : 1:java-1.8.0-openjdk-headless-1.8.0.171-7.b10.37.amzn1.x86_64                                                 3/5 
  Verifying  : lksctp-tools-1.0.10-7.7.amzn1.x86_64                                                                          4/5 
  Verifying  : copy-jdk-configs-1.2-1.2.amzn1.noarch                                                                         5/5 

Installed:
  java-1.8.0-openjdk-devel.x86_64 1:1.8.0.171-7.b10.37.amzn1                                                                     

Dependency Installed:
  copy-jdk-configs.noarch 0:1.2-1.2.amzn1                             java-1.8.0-openjdk.x86_64 1:1.8.0.171-7.b10.37.amzn1      
  java-1.8.0-openjdk-headless.x86_64 1:1.8.0.171-7.b10.37.amzn1       lksctp-tools.x86_64 0:1.0.10-7.7.amzn1                    

Complete!
```

## Java8に切り替え
AWSではjavaバージョン管理にalternativesが使用されているため、ただインストールするだけではなく切り替え作業が必要となってきます。

`*+`が先頭についているjavaのバージョンが現在適用されているバージョンです。
`Enter to keep the current selection[+], or type selection number:`と対話する箇所があるので、jre-1.8.0である`2`を入力します。

### Java
```bash
$ sudo alternatives --config java
There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
   2           /usr/lib/jvm/jre-1.8.0-openjdk.x86_64/bin/java

Enter to keep the current selection[+], or type selection number: 2
```

### Javac
```bash
$ sudo alternatives --config javac

There are 2 programs which provide 'javac'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/java-1.7.0-openjdk.x86_64/bin/javac
   2           /usr/lib/jvm/java-1.8.0-openjdk.x86_64/bin/javac

Enter to keep the current selection[+], or type selection number: 2
```

最後にバージョンの確認を行い、1.8.0に変わっていたら完了です。

```bash
$ java -version
openjdk version "1.8.0_171"
OpenJDK Runtime Environment (build 1.8.0_171-b10)
OpenJDK 64-Bit Server VM (build 25.171-b10, mixed mode)
```

```bash
$ javac -version
javac 1.8.0_171
```

# まとめ
インストールして満足するだけでなく、切り替え作業を忘れずにやりましょう！