In VSCode, my IDE, I'm configure the Language support for Java(TM) by Red Hat extension with a "Java" docker container. Thus far I have been able to set up the runtime, however I didn't manage to point my IDE to the Java home directory of the docker container.
According to the extension's page, this is what I need to achieve in order to make the extension work:
This JDK will be used to launch the Java Language Server. And by default, will also be used to compile your projects.
The path to the Java Development Kit can be specified by the
java.homesetting in VS Code settings (workspace/user settings). If not specified, it is searched in the following order until a JDK meets current minimum requirement.
- the
JDK_HOMEenvironment variable- the
JAVA_HOMEenvironment variable- on the current system path
How I set up the runtime
As I already said, I manage to set up a runtime. This is how I did it:
Added a
javabinary for the docker container, in my case I put it in/usr/local/bin.#!/bin/sh docker run \ --name usr-local-bin-java \ -i \ --rm \ --network=host \ -w /root/project \ -v "$PWD":/root/project \ -v "$PWD"/.docker/mvn/cache:/root/.m2 \ maven:3-jdk-8-slim \ java "$@" exit $?I'm using a Maven image because I also needed Maven, which also includes Java. Of course a OpenJDK image could also have been used.
Added/updated my IDE's user
settings.jsonwith the following:"java.configuration.runtimes": [ { "name": "JavaSE-1.8", "path": "/usr/local/bin/java", "default": true } ],
Where I am stuck at
So the last step I've in front of me, is to specify the path for java.home. But how will I do that if it's located in a docker container? Well the most obvious solution would be to mount the path in the docker container to my host. Unfortunately that won't work because the path I need is stored in the docker image, thus when I mount my host to the path then it will result in a empty directory.
To summarize this won't work (note: java home in my docker image is at /usr/local/openjdk-8):
- Add the following argument to the
javabinary, to set up a volume mount:-v "$PWD"/.docker/java/jdk:/usr/local/openjdk-8 \ - Add the
java.homeattribute to thesettings.jsonconfig:"java.home": "${cwd}/.docker/java/jdk",
This will result in something like: "The java.home configuration doesn't point to a valid JDK".
Does anyone know: What are my options here? Can I point to directory in a interactive automatically removed docker container?
PS: I don't want to install a JDK on my OS directly.
UPDATE #1: I managed to mount a volume from the docker container to my host
- Create a new docker volume:
docker volume create openjdk - Add the volume mount arg to the the
javabinary:-v openjdk:/usr/local/openjdk-8 \ - Run the binary:
java -version - Add the
JDK_HOMEorJAVA_HOMEvariable to your bash env:JAVA_HOME="$(docker inspect -f '{{ index .Mountpoint }}' openjdk)" - Reload the IDE and get errors...
Java 11 or more recent is required to run the Java extension. Please download and install a recent JDK. You can still compile your projects with older JDKs by configuring 'java.configuration.runtimes'
I even tried to set the java.home attribute to the mountpoint of the volume directly, which resulted in permission denied error, thus I chmod +rx -R my whole "var lib docker" directory (the place where the volumes are stored). Eventually the same error occurred as described in the steps above.