'Java not found in CircleCI job, running Node.js tests

For running my Node.js tests (which require Java) I used this configuriation:

version: 2.1

orbs:
  node: circleci/[email protected]
  
jobs:
  build_and_test:
    docker:
      - image: cimg/openjdk:17.0.2-node
    resource_class: large
    steps:
      - checkout
      - run: java --version
      - run: node --version
      - node/install-packages:
          pkg-manager: npm
      - run:
          command: npm run test
          name: Run tests
            
workflows:
    validation:
        jobs:
            - build_and_test
            

As you can see I use the language variant for the OpenJDK with Node. The version checks both succeed, so Java is actually available.

However, when I spawn a process in my tests to run Java, it fails:

            const java = child_process.spawn("java", parameters, spawnOptions);
            if (!java.connected) {
                resolve("Java not installed");

                return;
            }

Is there something special I have to consider when spawing processes in CircleCI or is something else required, which I haven't done yet?



Solution 1:[1]

The problem is the check for the process. While this works well locally, it does not in environments like CircleCI. Instead use the error event of the child process, like:

            java.on("error", (error) => {
                resolve(`Error while running Java: "${error.message}". Is Java installed on you machine?`);
            });


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mike Lischke