linuxshell

Can't access Java from shell script


I am trying to execute a jar file from a bash script, but for whatever reason, java subcommands are not working within the shell script. Attached is an example of a simple script, and the error it returns. The same error is outputted when I use any subcommand for java.

What do I need to fix?

#!/bin/sh
java -version

Error Picture


Solution

  • The bash file has dos line endings. To fix the issue, follow the this answer.

    How do we know? @KamilCuk commented a good explanation.

    The line : not found test.sh: starts with a :. It looks like: shell: test.sh: \r: not found, but the : not found part was moved to the beginning of the line. And also -version does not work, which would not if it would be -version$'\r'