14

My jar file P2.jar (which has a main method and manifest file) is located in C:\Jar Folder. I tried to run it from cmd like this:

C:SomeRandomFolder> java -jar C:\Jar Folder\P2.jar

I get the error:

unable to access jarfile C:\Jar

It works when I run it from the folder that the jar is in like this:

C:\Jar Folder> java -jar P2.jar

Why can't I use the 1st command? Why do I have to go to the directory of my jar file?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Apple Grinder
  • 3,573
  • 7
  • 22
  • 35

2 Answers2

30

That's because of the space you have in the path name. On Windows, use quotes:

java -jar "C:\Jar Folder\P2.jar"

Kenny Linsky
  • 1,726
  • 3
  • 17
  • 41
Isaac
  • 16,458
  • 5
  • 57
  • 81
  • 1
    Note: Parameters passed to jar file should not be included in quotes. Ex: java -jar "path with spaces\jarfile" foo bar – lupchiazoem Jun 28 '19 at 17:27
  • This does not solve the issue. I have the exact same problem without the spaces in the path name and with/without quotes. – SomJura May 08 '20 at 16:24
  • @SomJura it solved the op's issue, and possibly many others'. There could be other reasons. You're welcome to post your findings and we'll try to help you out. – Isaac May 08 '20 at 16:54
1

If you are in a terminal and the folder you are in is deleted out from underneath you and replaced with the same content, the jar file will be be visible with ls, but will not be available since your current directory was deleted and replaced with one that looks just like it.

I got that error too:

el@apollo:/myproject/build/jar$ java -jar CalculateStats.jar
Unable to access jarfile CalculateStats.jar

To fix it. cd up a directory and cd back in, like this:

el@apollo:/myproject/build/jar$ cd ..
el@apollo:/myproject/build$ cd jar/
el@apollo:/myproject/build/jar$ java -jar CalculateStats.jar
Program completed successfully
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335