Unable to get runtime dependency

I have following derivation to install jdtls. It requires java executable at runtime. So I have added propagatedBuildInputs = [ jdk19_headless ];.

Flake.nix:

{
  description = "A very basic flake";

  outputs = { self, nixpkgs }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs { inherit system; };
    in
    {
      packages."${system}" = rec {
        jdtls = pkgs.callPackage ./jdtls.nix {};
        default = jdtls;
      };
    };
}

jdtls.nix:

{
  stdenv,
  fetchurl,
  gnutar,
  jdk19_headless,
  ...
}: stdenv.mkDerivation {
  pname = "jdtls";
  version = "1.28.0";

  # sourceRoot = ".";
  src = fetchurl {
    url = "https://www.eclipse.org/downloads/download.php?file=/jdtls/snapshots/jdt-language-server-1.28.0-202309211406.tar.gz";
    sha256 = "q9O44I8iLuNT/eD1IDVnoa1TQgzEIhl2YF40wD0y/sA=";
  };

  nativeBuildInputs = [ gnutar ];
  propagatedBuildInputs = [ jdk19_headless ];
  # buildInputs = [ jdk19_headless ];

  dontUnpack = true;


  installPhase = ''
    mkdir -p $out/bin
    mkdir -p $out/share
    tar xf $src -C $out/share

    ln -s $out/share/bin/* $out/bin/
  '';
}

However when running, jdtls cannot find java executable.

Error:

❯ nix run
warning: Git tree '/home/s1n7ax/Workspace/nix-flakes' is dirty
Traceback (most recent call last):
  File "/nix/store/bs4figh6ni99f7y3vwkkhni1xa5lzwjp-jdtls-1.28.0/bin/jdtls", line 17, in <module>
    jdtls.main(sys.argv[1:])
  File "/nix/store/bs4figh6ni99f7y3vwkkhni1xa5lzwjp-jdtls-1.28.0/share/bin/jdtls.py", line 82, in main
    java_executable = get_java_executable(known_args.validate_java_version)
  File "/nix/store/bs4figh6ni99f7y3vwkkhni1xa5lzwjp-jdtls-1.28.0/share/bin/jdtls.py", line 33, in get_java_executable
    out = subprocess.check_output([java_executable, '-version'], stderr = subprocess.STDOUT, universal_newlines=True)
  File "/nix/store/2c7sgx69p6mmp76cvmi5j6c72dj76jj8-python3-3.10.12/lib/python3.10/subprocess.py", line 421, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/nix/store/2c7sgx69p6mmp76cvmi5j6c72dj76jj8-python3-3.10.12/lib/python3.10/subprocess.py", line 503, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/nix/store/2c7sgx69p6mmp76cvmi5j6c72dj76jj8-python3-3.10.12/lib/python3.10/subprocess.py", line 971, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/nix/store/2c7sgx69p6mmp76cvmi5j6c72dj76jj8-python3-3.10.12/lib/python3.10/subprocess.py", line 1863, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'java'

I tried creating a new shell with jdk19_headless and running nix run and it works.

nix-flakes on  main [+]
❯ nix-shell -p jdk19_headless

[nix-shell:~/Workspace/nix-flakes]$ nix run
warning: Git tree '/home/s1n7ax/Workspace/nix-flakes' is dirty
WARNING: Using incubator modules: jdk.incubator.concurrent, jdk.incubator.vector
Sep 23, 2023 3:48:17 PM org.apache.aries.spifly.BaseActivator log
INFO: Registered provider ch.qos.logback.classic.servlet.LogbackServletContainerInitializer of service jakarta.servlet.ServletContainerInitializer in bundle ch.qos.logback.classic
Sep 23, 2023 3:48:17 PM org.apache.aries.spifly.BaseActivator log
INFO: Registered provider ch.qos.logback.classic.spi.LogbackServiceProvider of service org.slf4j.spi.SLF4JServiceProvider in bundle ch.qos.logback.classic

What am I doing wrong here?

I have created a simple derivation to check the issue.

{
  stdenv,
  jdk19_headless,
  ...
}: stdenv.mkDerivation {
  pname = "test";
  version = "1";

  # sourceRoot = ".";
  src = builtins.toFile "test" ''
      #!/usr/bin/env bash
      echo "hello world"
      java -version
    '';

  propagatedBuildInputs = [ jdk19_headless ];

  dontUnpack = true;


  installPhase = ''
    java -version
    mkdir -p $out/bin
    cp $src $out/bin/test
    chmod a+x $out/bin/test
  '';
}

Here java -version command runs successfully in the installPhase but when nix run, test executable fails due to no java command.

Log:

@nix { "action": "setPhase", "phase": "patchPhase" }
patching sources
@nix { "action": "setPhase", "phase": "updateAutotoolsGnuConfigScriptsPhase" }
updateAutotoolsGnuConfigScriptsPhase
@nix { "action": "setPhase", "phase": "configurePhase" }
configuring
no configure script, doing nothing
@nix { "action": "setPhase", "phase": "buildPhase" }
building
no Makefile or custom buildPhase, doing nothing
@nix { "action": "setPhase", "phase": "installPhase" }
installing
openjdk version "19.0.2" 2023-01-17
OpenJDK Runtime Environment (build 19.0.2+7-nixos)
OpenJDK 64-Bit Server VM (build 19.0.2+7-nixos, mixed mode, sharing)
@nix { "action": "setPhase", "phase": "fixupPhase" }
post-installation fixup
shrinking RPATHs of ELF executables and libraries in /nix/store/6idhn9msq9fjy8q1p6w3fksq8hml85zf-test-1
checking for references to /build/ in /nix/store/6idhn9msq9fjy8q1p6w3fksq8hml85zf-test-1...
patching script interpreter paths in /nix/store/6idhn9msq9fjy8q1p6w3fksq8hml85zf-test-1
/nix/store/6idhn9msq9fjy8q1p6w3fksq8hml85zf-test-1/bin/test: interpreter directive changed from "#!/usr/bin/env bash" to "/nix/store/vqvj60h076bhqj6977caz0pfxs6543nb-bash-5.2-p15/bin/bash"
stripping (with command strip and flags -S -p) in  /nix/store/6idhn9msq9fjy8q1p6w3fksq8hml85zf-test-1/bin

Error:

[main 08a426d] adding hello
 Date: Sat Sep 23 15:59:58 2023 +0530
 1 file changed, 18 insertions(+), 14 deletions(-)
hello world
/nix/store/6idhn9msq9fjy8q1p6w3fksq8hml85zf-test-1/bin/test: line 3: java: command not found

EDIT:

I also tried following options to no success

buildInputs = [ jdk19_headless ];
propagatedBuildInputs = [ jdk19_headless ];
propagatedNativeBuildInputs = [ jdk19_headless ];