0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DockerでJavaの実行環境をさくっと作る

Posted at

あくまで検証やちいさなプログラムを実行する目的として、Javaのポータブルな実行環境をDocker Composeでサクッと構築します。

環境

$ cat /etc/debian_version 
12.8
$ docker --version
Docker version 27.3.1, build ce12230

Dockerでの環境構築

まずMicrosoftのOpenJDKイメージを使って、Docker Composeファイルを記述します。

compose.yaml
services:
  app:
    image: mcr.microsoft.com/openjdk/jdk:21-ubuntu
    volumes:
      - .:/app
    working_dir: /app

適当にHello Worldするプログラムを配置します。

Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

あとは、以下のコマンドでコンパイルして実行します。

$ docker compose run --rm app sh -c "javac Main.java && echo 'compiled!' && java Main"

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?