Sunday, July 25, 2021

How to Fix "The system cannot find the path specified." Error in Command Prompt? Example

Recently I was trying to run the Apache Tomcat server from the command prompt to test my Java web application on localhost, only to find the "The system cannot find the path specified" error in command prompt. I haven't seen this error before so I was wondering whether tomcat is throwing or something is wrong with my system's PATH environment variable.  In order to start the tomcat server, I was running the catalina.bat file as tomcat/bin/catalina.bat start and it wasn't starting the Tomcat at all, instead, it was keep throwing "The system cannot find the path specified." error as shown below:

C:\apache-tomcat-7.0.73\bin>catalina.bat start
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.

This actually gives me hint that it's not the Tomcat but something is really wrong with the PATH environment variable, but what could be wrong? What does this error mean? and most importantly how to solve this error?




Cause of "The system cannot find the path specified." Error

It seems this error occurs when you have invalid paths in your PATH environment variable e.g. directories which don't exist anymore. To find out if that's the case just run following command in your DOS command prompt:

C:\apache-tomcat-7.0.73\bin>echo %PATH%
C:\Windows\system32;C:\Windows;C:\Windows\System32\WindowsPowerShell\v1.0\;
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.

You can see that after printing some paths, the command prompt is throwing "The system cannot find the path specified." error again, which means it has nothing to do with Tomcat or Catalina but just with the PATH environment variable. There are some directories in the PATH which system is not able to resolve or find, hence it is throwing "The system cannot find the path specified." error.

To be honest, the error message makes sense if you read it, but we are so used to panic when we see the error, we don't try to understand the error message in first place. Now that, we have tried printing the value of PATH environment variable and seeing the message after few valid PATH, it suddenly starts making more sense.

This error is nothing to do with Tomcat, it can come while running any program/application from command prompt or even when a program trying to use the PATH environment variable.



The solution of "The system cannot find the path specified." Error

Now that you know the cause of this error is invalid directories and path in the PATH environment variable, you can easily solve this problem by finding and removing those invalid path entries which system is not able to found.

Btw, finding invalid path can be a problem too if you have several directories listed in PATH environment variable. In order to check every path is correct, you need to split the whole PATH String by semicolon and check if they are correct by copying and opening a directory in command prompt. This could be troublesome but it is the sure short way to find all invalid path in your PATH environment variable.

Can we do better? is there a way to omit some of the valid paths? Well, if you have paid close attention to the output of echo %PATH% command, you see it printed a couple of directories before throwing the "The system cannot find the path specified." error, which means they are valid, so we should start the search from the point point you see the error.

For example, in our case, the last valid path in the output above was "C:\Windows\System32\WindowsPowerShell\v1.0\;", so next path must be invalid. We can confirm that by copying that path and opening from the command prompt by just copy pasting it on run window and hitting enter.

If it opens a directory then it's a valid path, otherwise, run window will tell it's an invalid path. The next step is to correct or remove that path and run the echo %PATH% again. At this point, you will see the error when the system will hit the next invalid PATH, just repeat the process i.e. copy the invalid path by copying path after last valid path printed by echo %PATH% command and open that directory by using run command window or file explorer. If it's valid then it will open a directory otherwise you see the error, something like path doesn't exist.


Repeat the steps until the error completely goes aways, at this time, all the paths in your PATH environment variable is valid and you are ready to run your program from command prompt again e.g. Java, Tomcat, NetBeans or anything else.

Btw, if you are wondering how to see the value of PATH environment variable in Windows 8 or Windows 10, you can follow these steps to copy the PATH String and edit it to correct or remove invalid entries in PATH environment variables.

  1. From your desktop, rick-click "My Computer" and click "Properties"
  2. In the System Properties window, click on the Advanced tab.
  3. In the Advanced section, click the Environment Variables button.
  4. In the Environment Variables window, highlight the Path variable in the Systems Variable section and click the Edit button.


Here you can copy the value of PATH environment variable then you can search for last valid path e.g. "C:\Windows\System32\WindowsPowerShell\v1.0\;", then next path is your invalid PATH. Verify that by opening the directory using run command window or file explorer, if it's valid either correct it or remove it and then save and open a new command prompt to try again by running echo %PATH%.

If it throws "The system cannot find the path specified." error again means you have some more invalid paths in PATH environment variable. Just keep correcting or removing them until your PATH is printed completely by echo %PATH%. At this point, your PATH has only valid entries and you can tomcat or Java from the command line.

In my case, though the root cause was still invalid path in PATH environment variable, the reason of being invalid was quite interesting. My system PATH environment variable has sub-directories with an ampersand (&) character on it e.g. C:\Program Files (x86)\A & B\bin. When I copy pasted and open the directory from the file explorer or run window it was managed to open the directory but when I put the same path in PATH environment variable it was throwing the "The system cannot find the path specified." error.

I tried to escape & with backslash \ it didn't work then I tried to put single quote around A & B e.g. 'A & B', that didn't work too, finally I removed the directory from PATH because it wasn't really needed and boom the error "The system cannot find the path specified." was gone. Now the full path is printed when I type the path or echo %PATH% in command prompt. So, that resolved my problem.

Just remember that every time you make a change on PATH or Path environment variable under System Properties section, you must open a new command prompt window, don't run the PATH or echo %PATH% on old command prompt window because it will have old value of system properties. The new, updated value of PATH will only be available on new command prompt window.

Solution of "The system cannot find the path specified." Error in Command Prompt



Important points

1. The root cause of "The system cannot find the path specified." is an invalid path in PATH environment variable.

2. The name of the PATH environment variable can be Path or path as well.

3. You should use the value of the PATH environment variable from System Properties Window in Advanced Settings, Environment Variables.

4. Watch out for & character in PATH, it's invalid and cause "The system cannot find the path specified.", remove them from PATH.

5. To find the invalid path, just type the path or echo %PATH% in the command prompt and see where it is failing to display the directory path. That's your invalid entry. Find out what is wrong and then correct it or remove it from PATH.


That's all about how to solve "The system cannot find the path specified." error while running the program from the command prompt. The root cause of this error is invalid directories, sub-directories in the PATH environment variable, just remove them and the error will be solved. When searching for invalid directories, look where the path is failing to display directory name, when you type the path or echo %PATH% in command prompt. Watch out for special characters like & in PATH, even though run window can open such path, it seems they are not valid for the PATH environment variable.


Other Java troubleshooting guides you may like to explore:
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java? [solution]
  • How to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [solution]
  • SQLServerException: The index 58 is out of range - JDBC [solution]
  • java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Java 
  • Spring - java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener [solution]
  • How to fix java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver in Java? [solution]
  • java.lang.ClassNotFoundException: com.mysql.jdbc.Driver [Solution]
  • Solving java.lang.ArrayindexOutOfBoundsException: 1 in Java  [guide]
  • 3 ways to solve "No JVM Installation Found. Please install a 64-bit JDK" error [guide]
  • How to solve OutOfMemoryError in Eclipse? [guide]
  • How to solve javax.net.ssl.SSLHandshakeException: unable to find valid certification path to requested target? [solution]
  • 2 Reasons of org.springframework.beans.factory.BeanCreationException: Error creating bean with name [Solution]
  • Solving org.springframework.beans.factory.BeanCreationException: Error creating bean with name [Solution]
  • Solving java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test [steps]
  • java.lang.numberformatexception for input string null - Cause and Solution [guide]
  • Maven Eclipse Error - "No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK" [guide]
  • java.sql.SQLException: No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [Solution]
  • Minecraft - java.lang.UnsatisfiedLinkError: lwjgl64.dll : Access Denied Solution [solution]
  • java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer [Solution]
  • Exception in thread "main" java.lang.NoClassDefFoundError: helloworldapp/HelloWorldApp in comand prompt [guide]

10 comments :

Anonymous said...

Also check registry for missing path in AutoRun key:

Computer\HKEY_CURRENT_USER\Software\Microsoft\Command Processor
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor

This was my problem when uninstalling miniconda, after running "conda init"

Unknown said...

thanks man you helped:)

javin paul said...

@Unknown, happy to hear that.

Anonymous said...

Registry changes worked.
Thank you.

Anonymous said...

%PATH% was not working for me on Windows 10.
Tried PATH and it worked.

Anonymous said...

Thanks for helping even I was also facing similar issue with conda init

Anonymous said...

I also had a '&' sign in my path and this issue was freaking me out. Thanks for sharing your experiences in this article, helped me out.

Unknown said...

Path
%PATH%

Anonymous said...

I don't have admin access to change my path and I would rather not bother our TI staff if I don't have to.

I tried updating my PATH variable on the dos command line via the set command. Unfortunately that didn't work. Does dos have a bash export equivalent that i have to invoke?

Also, I set the JAVA_HOME variable, but when I invoke tomcat, it doesn't show the JAVA_HOME as set. Instead says the JRE_HOME environment is set.

javin paul said...

Hello Anonymous, there are multiple option to set PATH even if you don't have admin access. For example, you can set PATH for your user account, just type "edit environment variable" in Windows Run box and you will see the option to edit environment variables for your account, there you can add or edit JAVA_HOME, JRE_HOME, PATH etc.

Admin access only require to change the System environment variable and user values will take precedence over system.

You can do the same using DOS window just like you mentioned but any change you mentioned will only be applicable to that session, it will not be save or persist, which means if you open new DOS window, the JAVA_HOME will be reset.

I hope this helps, if not, please feel free to ask again and may be you can paste what you are doing and what output are you getting to assist you better.

Post a Comment