Fix the $' ': Command Not Found Error in Java

Sheeraz Gul Jan 30, 2023 Aug 19, 2022
  1. Cause of the $'\r': command not found Error in Java
  2. Fix the $'\r': command not found Error in Java
Fix the $'
': Command Not Found Error in Java

This tutorial demonstrates the $'\r': command not found error in Java.

Cause of the $'\r': command not found Error in Java

Different operating systems use different commands for the same purpose. The error $'\r': command not found occurs when we are trying to run Unix-style commands on the Linux platform of the platforms like Cygwin.

The \r is used for the carriage return, which usually means the cursor should move to the left-most. Here is an example of this error.

Let’s try permanently setting the JAVA_HOME path using the Cygwin Bash on Windows. Use the following bash commands and configuration:


export PATH="$JAVA_HOME/bin:$PATH"
export JAVA_HOME=$JAVA_HOME:"/cygdrive/C/Program Files/Java/jdk-17.0.2"


if [ -f ~/.bashrc ]; then
   source ~/.bashrc

Now the code above is two files in Bash trying to permanently set the JAVA_HOME path.

When running these two files using Cygwin, the output is:

-bash: $'\377\376if': command not found
-bash: $'then\r': command not found
: No such file or directory
-bash: /cygdrive/c/Users/Sheeraz//.bash_profile: line 3: syntax error near unexpected token `fi'
-bash: /cygdrive/c/Users/Sheeraz//.bash_profile: line 3: `fi'

Now the error shows that the command is not working. The reason is that Windows does not support the Unix/OSX format of the commands, and Cygwin is also considered a platform to run Linux Unix applications on Windows.

Fix the $'\r': command not found Error in Java

Let’s look at the solutions for this error.

Solution 1

When the error $'\r': command not found occurs on Windows or Cygwin, the best solution is to use the dos2unix command.

  1. The dos2unix will modify the newline or \r character to be compatible on Unix or Cygwin platforms.
  2. Before using the dos2unix command, you must back up the file because this command will modify the file. If you have not backed it up, use the command unix2dos, which will reverse the change.
  3. The dos2unix command is not supported on macOS X.
  4. Before using this command, you must install dos2unix.

Solution 2

If you don’t want to install dos2unix, then there is another solution:

  1. We can remove the \r character causing this error. Use the following command to remove this trailing character:

    sed -i 's/\r$//' filename
  2. Here filename will be the file that is causing the error.

  3. The -i option is used for in-place editing.

  4. The command will delete the trailing \r from the input file.

Solution 3

This one is more simple. We can use software like Notepad++ or VScode to change the command format.

For example, in Notepad++:

  • Open the Bash file in Notepad++.
  • Go to Edit -> EOL conversion.
  • Select Unix/OSX format.
  • Save your file and run it.

These three solutions will solve the error described above.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Error