How to Fix the $' ': Command Not Found Error in Java
![How to Fix the $'
': Command Not Found Error in Java](/img/Java/java-feature-image.webp)
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:
.bashrc
:
export PATH="$JAVA_HOME/bin:$PATH"
export JAVA_HOME=$JAVA_HOME:"/cygdrive/C/Program Files/Java/jdk-17.0.2"
.bash_profile
:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
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.
- The
dos2unix
will modify the newline or\r
character to be compatible on Unix or Cygwin platforms. - 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 commandunix2dos
, which will reverse the change. - The
dos2unix
command is not supported on macOS X. - Before using this command, you must install
dos2unix
.
Solution 2
If you don’t want to install dos2unix
, then there is another solution:
-
We can remove the
\r
character causing this error. Use the following command to remove this trailing character:sed -i 's/\r$//' filename
-
Here filename will be the file that is causing the error.
-
The
-i
option is used for in-place editing. -
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.
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 FacebookRelated Article - Java Error
- How to Fix the Error: Failed to Create the Java Virtual Machine
- How to Fix the Missing Server JVM Error in Java
- How to Fix the 'No Java Virtual Machine Was Found' Error in Eclipse
- How to Fix Javax.Net.SSL.SSLHandShakeException: Remote Host Closed Connection During Handshake
- How to Fix the Error: Failed to Create the Java Virtual Machine
- How to Fix Java.Lang.VerifyError: Bad Type on Operand Stack