1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

javac, jar, javaコマンドチートシート

Posted at

Javaでプログラムを作成後の実行までの順序。

  1. compile。javacコマンドを使う。
  2. jarファイルの作成。jarコマンドを使う。
  3. run。javaコマンドを使う。

javacコマンド

help

javac --help

javac -help, javac -?も同じ。
javac -hは別物。

compile Java8以前 module不使用

compile-java8-earlier-none-module.sh
javac -d /path/to/bin/directory \
--class-path /path/to/files1:/path/to/files2 \
--release 8 \
$(find /path/to/src/directory -name "*.java")

--releaseでversion選択。
$(find /path/to/src/directory -name "*.java")で指定ディレクトリ内の*.javaを全てコンパイルする。

compile Java9以降 module使用

compile-java9-later-with-module.sh
javac -d /path/to/bin/directory \
--module-path /path/to/module/dir1:/path/to/module/dir2 \
--release 11 \
$(find /path/to/src/directory -name "*.java")

jarコマンド

help

jar --help

jar -?, jar -hも同じ。

create

jar-create-simple.sh
jar -c \
-f /path/to/Export.jar \
-C /path/to/src/directory .

-Cを使うときは、末尾に.を忘れないこと。

create Executable-jar

jar-create-executable.sh
jar -c \
-f /path/to/Executable.jar \
-e package.to.MainClass \
-C /path/to/src/directory .

-eでエントリーポイントを指定する。

javaコマンド

help

java --help

run

run.sh
java -cp /path/to/file1:/path/to/file2 \
package.to.MainClass

run Executable-jar

run-executable-jar.sh
java -cp /path/to/file1:/path/to/file2 \
-jar /path/to/Executable.jar

run Java9以降 module使用

run-with-module.sh
java --module-path /path/to/module/dir1:/path/to/module/dir2 \
--module name.of.module/package.to.MainClass

build and run sample

build and run Java8以前 module不使用

build-and-run-none-module.sh
#!/bin/sh

path_src="/path/to/src/directory"
path_bin="/path/to/bin/directory"
path_export_jar="/path/to/Export.jar"
path_lib="/path/to/lib/*"
entry_point="package.to.MainClass"
version="8"

# compile
javac -d ${path_bin} \
--class-path ${path_lib} \
--release ${version} \
$(find ${path_src} -name "*.java")

# jar
jar -c \
-f ${path_export_jar} \
-C ${path_bin} .

# run
java -cp ${path_lib}:${path_export_jar} \
${entry_point}

build and run Java9以降 module使用

build-and-run-with-module.sh
#!/bin/sh

path_src="/path/to/src/directory"
path_bin="/path/to/bin/directory"
path_export_jar="/path/to/Export.jar"
path_lib="/path/to/lib"
entry_point="name.of.module/package.to.MainClass"
version="11"

# compile
javac -d ${path_bin} \
--module-path ${path_lib} \
--release ${version} \
$(find ${path_src} -name "*.java")

# jar
jar -c \
-f ${path_export_jar} \
-C ${path_bin} .

# run
java --module-path ${path_lib}:${path_export_jar} \
--module ${entry_point}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?