多分、ビルドシステムとか統合開発環境に詳しい人には、何て事ない話なのでしょうが、解決に死ぬ程、苦労したので、備忘録として記載します。環境は、Mac M1です。
尚、Intellijを使わずに、ターミナルでJarファイルを構築方法は、こちら。
https://www.tech-teacher.jp/blog/java-jar/
IntelliJで、jarファイルを作成して、そのjarファイルを実行すると下記のエラーが発生。
UserX@UserX-mac HelloWorld_jar % jar -tf HelloWorld.jar
Error: Invalid or corrupt jarfile HelloWorld.jar
IntelliJ上で、runすると、「プロセスは終了コード 0 で終了しました」と正常終了しているので、コード自体は間違ってないが、jarファイルが実行できていないと言う状況です。
Jarファイルの中身を見ると、こんな感じ。
UserX@UserX-mac HelloWorld_jar % jar -tf HelloWorld.jar
com/
com/example/
com/example/helloworld/
com/example/helloworld/HelloWorld.class
yonishik_jp@yonishik_jp-mac HelloWorld_jar %
死ぬ程、調べました。(知っている人からすれば、拍子抜けでしょうが。)
下記サイトにソリューションが書いてありました。
https://programmerah.com/how-to-solve-error-invalid-or-corrupt-jarfile-32717/
>So I tried a lot of methods first, and found that there is no META_INF folder in the jar package at all through comparison, which is the root cause of the jar’s inability to run;
要は、「META_INFファイルがJarフォルダーに内包されている必要があるとの事。」
そんな訳で、下記の手順に忠実に従って、Jarファイルを再作成した。
今度は、META_INFファイルが内包されております。
UserX@UserX-mac HelloWorld_jar % jar -tf HelloWorld.jar
META-INF/MANIFEST.MF
META-INF/
Main.class
com/
com/example/
com/example/helloworld/
com/example/helloworld/HelloWorld.class
実行した。完了した。
UserX@UserX-mac HelloWorld_jar % java -jar HelloWorld.jar
hellokazu, world
以上。
尚、HelloWorldのJavaコードはこちら。
package com.example.helloworld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hellokazu, world");
}
}