HomebrewでGlassFishかPayaraをインストールする

# Payara5
- 環境 : macOS Big Sur バージョン11.0.1
    - [Homebrewでインストールする方法はこちら](https://qiita.com/ponsuke0531/items/ba2abf69867e7720c432)

```bash
# 使う前にはアップデートする
% brew update
Updated 3 taps (homebrew/core, homebrew/cask and homebrew/cask-fonts).
==> New Formulae
# ...省略...
aliwangwang             aspera-connect          blizz                   daedalus                docear                  yandexradio             zoomin
% brew --version
Homebrew 2.5.11
Homebrew/homebrew-core (git revision 81faa9; last commit 2020-11-17)
Homebrew/homebrew-cask (git revision 2c26b; last commit 2020-11-17)

# 使う前にはお医者さんにかかっておく・・・あっ・・・OSがサポートされていない・・・けど使えるから気にしない
% brew doctor                                    
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don`t worry or file an issue; just ignore this. Thanks!

Warning: You are using macOS 11.0.
We do not provide support for this released but not yet supported version.
You will encounter build failures with some formulae.
Please create pull requests instead of asking for help on Homebrew`s GitHub,
Twitter or any other official channels. You are responsible for resolving
any issues you experience while you are running this
released but not yet supported version.

# Payaraがあることを確認しておく
% brew search payara
==> Formulae
payara

# インストールする
% brew install payara
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
Updated 10 formulae.

Warning: You are using macOS 11.0.
We do not provide support for this released but not yet supported version.
You will encounter build failures with some formulae.
Please create pull requests instead of asking for help on Homebrew`s GitHub,
Twitter or any other official channels. You are responsible for resolving
any issues you experience while you are running this
released but not yet supported version.

==> Downloading https://homebrew.bintray.com/bottles/libpng-1.6.37.big_sur.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/a8f1c35f9f004c4f7878c30027e35a9fb9551782df963f88deebd3dc29d94d51?response-content-disposition=attachment%3Bfilen
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/freetype-2.10.4.big_sur.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/01b464b98584ba5777d8fc4605121c7a46e713a2f58d729197b82afef1b5f2b9?response-content-disposition=attachment%3Bfilen
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/openjdk%408-1.8.0%2B275.big_sur.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/b37ae302878e8bdbfaaa9eb085c7315bef27c09fee834c1e43a7540b2b986d5a?response-content-disposition=attachment%3Bfilen
######################################################################## 100.0%
==> Downloading https://search.maven.org/remotecontent?filepath=fish/payara/distributions/payara/5.192/payara-5.192.zip
==> Downloading from https://repo1.maven.org/maven2/fish/payara/distributions/payara/5.192/payara-5.192.zip
######################################################################## 100.0%
==> Installing dependencies for payara: libpng, freetype and openjdk@8
==> Installing payara dependency: libpng
==> Pouring libpng-1.6.37.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/libpng/1.6.37: 27 files, 1.3MB
==> Installing payara dependency: freetype
==> Pouring freetype-2.10.4.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/freetype/2.10.4: 64 files, 2.3MB
==> Installing payara dependency: openjdk@8
==> Pouring openjdk@8-1.8.0+275.big_sur.bottle.tar.gz
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-8.jdk

openjdk@8 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have openjdk@8 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openjdk@8/bin:$PATH"' >> ~/.zshrc

For compilers to find openjdk@8 you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk@8/include"

==> Summary
🍺  /usr/local/Cellar/openjdk@8/1.8.0+275: 742 files, 192.9MB
==> Installing payara
==> Caveats
You may want to add the following to your .bash_profile:
  export GLASSFISH_HOME=/usr/local/opt/payara/libexec/glassfish
  export PATH=${PATH}:${GLASSFISH_HOME}/bin

To have launchd start payara now and restart at login:
  brew services start payara
Or, if you don`t want/need a background service you can just run:
  asadmin start-domain --verbose domain1
==> Summary
🍺  /usr/local/Cellar/payara/5.192_1: 2,002 files, 176.7MB, built in 5 seconds
==> Caveats
==> openjdk@8
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-8.jdk

openjdk@8 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have openjdk@8 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openjdk@8/bin:$PATH"' >> ~/.zshrc

For compilers to find openjdk@8 you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk@8/include"

==> payara
You may want to add the following to your .bash_profile:
  export GLASSFISH_HOME=/usr/local/opt/payara/libexec/glassfish
  export PATH=${PATH}:${GLASSFISH_HOME}/bin

To have launchd start payara now and restart at login:
  brew services start payara
Or, if you don`t want/need a background service you can just run:
  asadmin start-domain --verbose domain1

# JDK8用の環境変数を設定する
% echo 'export CPPFLAGS="-I/usr/local/opt/openjdk@8/include"' >> ~/.zshrc
% echo 'export JAVA_HOME="/usr/local/opt/openjdk@8/bin"' >> ~/.zshrc
% echo 'export PATH=${JAVA_HOME}:${PATH}' >> ~/.zshrc

# Pyara用の環境変数を設定する
% echo 'export GLASSFISH_HOME=/usr/local/opt/payara/libexec/glassfish' >> ~/.zshrc 
% echo 'export PATH=${PATH}:${GLASSFISH_HOME}/bin' >> ~/.zshrc

# .zshrcの設定を反映する
% source ~/.zshrc
```

## 起動してみる

```bash
% asadmin
Use "exit" to exit and "help" for online help.
Remote commands not fetched

# バージョンを見てみる
asadmin> version
Version string could not be obtained from Server [localhost:4848].
(Turn debugging on e.g. by setting AS_DEBUG=true in your environment, to see the details.)
Using locally retrieved version string from version class.
Version = Payara Server  5.192 #badassfish (build 115)
Command version executed successfully.

# 起動する
asadmin> start-domain domain1
Waiting for domain1 to start ..................................................
Successfully started the domain : domain1
domain  Location: /usr/local/Cellar/payara/5.192_1/libexec/glassfish/domains/domain1
Log File: /usr/local/Cellar/payara/5.192_1/libexec/glassfish/domains/domain1/logs/server.log
Admin Port: 4848
Command start-domain executed successfully.
asadmin> 
```
http://localhost:4848/ をみてみる
![スクリーンショット 2020-12-15 21.15.40.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/159761/fee3ddff-49c7-b0d2-de52-f7232370130d.png)

## java11だとうまく起動しない・・・

```bash:java11をPATHに通しているとうまく起動しない・・・
# java11を設定していると・・・
% java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+11)
OpenJDK 64-Bit Server VM (build 11.0.8+11, mixed mode)

# 起動しない・・・どうやってjava11を設定すればいいのか・・・
% asadmin start-domain domain1                                                               
Waiting for domain1 to start .Error starting domain domain1.
The server exited prematurely with exit code 1.
Before it died, it produced the following output:

Unrecognized option: --add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

Command start-domain failed.
```

domain.xmlにある`<jvm-options>`の指定で`--add-opens`の直後に「=」を
Eclipseで起動する場合は入れない
asadminコマンドで起動する場合は入れるという情報を見つけた。

- 参考 : [Support for JDK 11 · Issue #11 · payara/ecosystem-eclipse-plugin](https://github.com/payara/ecosystem-eclipse-plugin/issues/11)

エラーになっている箇所は、domain.xmlで`<jvm-options>[9|]--add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED</jvm-options>`となっている。
試しに「=」をスペースに変えて`<jvm-options>[9|]--add-opens jdk.management/com.sun.management.internal=ALL-UNNAMED</jvm-options>`にしてみたらちょっと進んだ。

```bash
# エラーになるオプションがdomain.xmlで変更した行の3行下の内容になった
% asadmin start-domain
...省略...
Unrecognized option: --add-opens=java.base/java.nio=ALL-UNNAMED
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

Command start-domain failed.
# さっきと同様に「=」をスペースに変えてみたがエラーになる・・・どうすれば・・・
% asadmin start-domain
...省略...
Unrecognized option: --add-opens java.base/java.nio=ALL-UNNAMED
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

Command start-domain failed.
```

Payara6になるとJava11がデフォルトで使えるようになりそうだ。
> Payara 6
> 2021年に予定されているPayara Platform 6には，次のような機能が含まれる予定である — ソースコードの分解と再構築; 完全なJavarta EE 9認証; JDK 11+ ベース，すなわち，JDK 11による完全なコンパイルとビルドの実施; 新たな管理コンソールと監視用コンソール; Springのネイティブサポート。
> [Payara Platform 5の2019年のロードマップ](https://www.infoq.com/jp/news/2019/04/payara-5-roadmap-2019/)

## 有効期限が切れているSSL証明書を削除する
[有効期限が切れているSSL証明書を削除する - Qiita](https://qiita.com/ponsuke0531/items/207d216ebb973d94c034)

## Eclipseに設定してみる
- 環境 : Eclipse IDE for Enterprise Java Developers Version: 2020-12 M1 (4.18.0 M1)

[EclipseにPayaraを設定する方法 - Qiita](https://qiita.com/ponsuke0531/items/e2f2f1145cbf5065d142)

# GlassFish4
GlassFish4がほしいと思ってダウンロードページに出かけたら・・・閉鎖されていた:scream:
[GlassFish Commercial Editionの提供が終了](https://www.infoq.com/jp/news/2013/11/glassfish-commercial-dead)
> https://glassfish.java.net/download.html
> ![Screen Shot 2017-05-18 at 0.17.43.png](https://qiita-image-store.s3.amazonaws.com/0/159761/864ed794-8d3f-5c89-ab3b-dd2460d93ed9.png)

JavaEE7に同梱されているみたいだけど・・・GlassFish4だけ欲しい・・・
![Screen Shot 2017-05-18 at 0.19.16.png](https://qiita-image-store.s3.amazonaws.com/0/159761/d4acfc5e-2cb7-d279-a68d-c35275700741.png)

欲しいものは欲しいのでHomebrewでGlassFish4をインストールすることにしました。
[Glassfish — BrewFormulas](http://brewformulas.org/glassfish)

- 環境
    - macOS Sierra version10.12.4 64bit
    - Homebrew 1.2.1

```bash
# 1. Homebewを更新する
$ brew update

# 2. GlassFishをインストールする
$ brew install glassfish
==> Using the sandbox
==> Downloading http://download.java.net/glassfish/4.1.2/release/glassfish-4.1.2.zip
######################################################################## 100.0%
==> Caveats
The home of GlassFish Application Server 4 is:
  /usr/local/opt/glassfish/libexec

You may want to add the following to your .bash_profile:
  export GLASSFISH_HOME=/usr/local/opt/glassfish/libexec
  export PATH=${PATH}:${GLASSFISH_HOME}/bin

Note: The support scripts used by GlassFish Application Server 4
are *NOT* linked to bin.
==> Summary
🍺  /usr/local/Cellar/glassfish/4.1.2: 1,005 files, 126.6MB, built in 1 minute 30 seconds
```
## 起動してみる
```text
$ sudo /usr/local/Cellar/glassfish/4.1.2/libexec/glassfish/bin/asadmin
Use "exit" to exit and "help" for online help.
asadmin> start-domain domain1
Waiting for domain1 to start .........................................
Successfully started the domain : domain1
domain  Location: /usr/local/Cellar/glassfish/4.1.2/libexec/glassfish/domains/domain1
Log File: /usr/local/Cellar/glassfish/4.1.2/libexec/glassfish/domains/domain1/logs/server.log
Admin Port: 4848
Command start-domain executed successfully.
asadmin> 
```
http://localhost:4848/ をみてみる
![Screen Shot 2017-05-18 at 0.48.58.png](https://qiita-image-store.s3.amazonaws.com/0/159761/8556364c-fd48-6753-d821-e79292292ec1.png)

## Eclipseに設定してみる
- 環境 : Eclipse : Neon.3 Release (4.6.3)

![Screen Shot 2017-05-18 at 0.52.58.png](https://qiita-image-store.s3.amazonaws.com/0/159761/20d72a43-fb1f-28ad-f750-2275dfa6be9c.png)
![Screen Shot 2017-05-18 at 0.53.10.png](https://qiita-image-store.s3.amazonaws.com/0/159761/6a005178-2b50-8705-73c5-5217f6a60688.png)
![Screen Shot 2017-05-18 at 0.53.27.png](https://qiita-image-store.s3.amazonaws.com/0/159761/0f02c3b9-24e3-9afd-c2f5-13ca775179f1.png)

## データベースを設定してみる
![Screen Shot 2017-05-18 at 0.56.58.png](https://qiita-image-store.s3.amazonaws.com/0/159761/86d26f8d-3c51-0dc8-77c2-df93cfe44d3d.png)
![Screen Shot 2017-05-18 at 0.57.12.png](https://qiita-image-store.s3.amazonaws.com/0/159761/0cdf39f1-4186-a4fe-b261-d4d9da7d847e.png)
![Screen Shot 2017-05-18 at 1.00.30.png](https://qiita-image-store.s3.amazonaws.com/0/159761/54565450-c916-9f61-4a7f-c98a736aeb84.png)

- [Next...]をクリックしたら「HTTP Status 500 - Internal Server Error」になった場合
    - /usr/local/Cellar/glassfish/4.1.2/libexec/glassfish/domain/domain1/configにある domain.xml にデータベースの情報をガチで書く

![Screen Shot 2017-05-18 at 1.01.08.png](https://qiita-image-store.s3.amazonaws.com/0/159761/4b78e025-9aa7-06f4-92dc-167e2b546394.png)

使いたいデータベースの情報を入れる。
![Screen Shot 2017-05-18 at 1.03.20.png](https://qiita-image-store.s3.amazonaws.com/0/159761/e9ac0c1b-38d5-d959-d44d-cc3d45df7069.png)


# 参考にさせていただいたサイト様
- [ScalaでServletを作る with IDEA14 備忘録 - Qiita](http://qiita.com/abey1192/items/1534f876085c2c6fd0fe)
- [EclipseにGlassFishサーバーを設定 - Qiita](http://qiita.com/muneo/items/a88ba8d1e3702e227179)