197

I want to execute my program without using an IDE. I've created a jar file and an exectuable jar file. When I double click the exe jar file, nothing happens, and when I try to use the command in cmd it gives me this:

Error: Unable to access jarfile <path>

I use the command: java -jar Calculator.jar

How I created the jar:

  1. Right click on project folder (Calculator)
  2. Select
  3. Click on Java Folder and select "Exectuable Jar File", then select next
  4. Launch Configuration: Main - Calculator
  5. Create Export Destination
  6. Hit "Finish" and profit! Well, not really.
Raedwald
  • 46,613
  • 43
  • 151
  • 237
Joseph Smith
  • 3,243
  • 5
  • 20
  • 18
  • 1
    Sounds like the Jar is corrupt in some way, possibly missing the Manifest entry. – MadProgrammer Aug 14 '12 at 00:04
  • You should show how you created the JARs, what's in them and how you try to run the program. The Java code is irrelevant here. – Jochen Aug 14 '12 at 00:11
  • Have you tried relaunching elcipse and recompiling? Sometimes Eclipse has does some strange things that can be avoided by relaunching. – MattS Aug 14 '12 at 00:12
  • I find the code difficult to read and think about. And what's the point of having everything in a static main function and having a single instance method to clear the console? – Dave Newton Aug 14 '12 at 00:20
  • MadProgrammer: How can I fix this? Jochen: Will Do. I'll edit it now. MattS: Yes i've tried that multiple times Dave: Because I've only scratched the surface of Java and i dont really know what "beautiful" code looks like :) – Joseph Smith Aug 14 '12 at 00:29
  • At the command line are you in the same folder as the jar file when you try to run java -jar Calculator.jar? – Logan Aug 14 '12 at 02:36
  • Yes I have placed the .jar file on my desktop, this is the line exactly - C:\Users\jsmit1061\Desktop – Joseph Smith Aug 15 '12 at 03:56
  • 1
    I have no answers to my problem, i answered it myself, and there is no way to show my appreciation in the comments, other than thanking them via reply. Please Reread your comment. – Joseph Smith Aug 23 '12 at 00:14
  • This error message is displayed also when trying to launch a folder and not a `.jar` file. – Spacemonkey Jul 02 '13 at 14:32
  • 1
    I was able to solve it by passing absolute path for the jar file after the command "java -jar /home/aboslute_path_to_the_jar_file.jar". Hope it helps. – PinkBanter Feb 26 '20 at 13:52
  • happened to me when i had the jar open in one terminal window, would't allow write to directory, then opened up another terminal and tried to run the same jar – RobZ Oct 15 '20 at 16:55
  • for me it was a silly typo.. i was giving lambok.jar instead of lombok.jar – Stunner Nov 03 '20 at 04:28

40 Answers40

116

I had encountered this issue when I had run my Jar file as

java -jar TestJar

instead of

java -jar TestJar.jar

Missing the extension .jar also causes this issue.

Vinay Kadalagi
  • 1,315
  • 1
  • 8
  • 11
  • 1
    This is the correct answer. The .jar extension must be appended to the jar file name. – Abhijay Kumar Apr 22 '19 at 06:29
  • 53
    It's not always the correct answer, I'm appending it and it doesn't work. – Alan B Oct 07 '19 at 10:15
  • 1
    In my case I thought it was a file while it was a folder on the console. The jar file was in a folder that has the same name. – Willy Makend Oct 23 '19 at 16:59
  • This is always the incorrect Answer for this Question - the Question mentioned using the `.jar` extension; it wasn't missing. Although this Answer may help in some similar situations, it wasn't the answer to this Question's particular situation. – cellepo Feb 19 '23 at 23:31
84

Fixed

I just placed it in a different folder and it worked.

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
Joseph Smith
  • 3,243
  • 5
  • 20
  • 18
  • 80
    Can someone explain why this happened? – Anastasios Andronidis Jul 10 '14 at 22:41
  • 12
    Sounds like it could have been a file permissions issue, however total guess. – Aiden Fry Sep 09 '14 at 10:02
  • sometimes it happens in mac. if I copy ms bat file to sh and modify it to make it work in mac, i get the same error. It is better to create new sh file in mac to get rid of the problem. The reason should be hidden chars in ms file. – serkan May 09 '15 at 14:24
  • 2
    I had a similar issue on WIndows, using cygwin. So cygwin uses different paths than windows `/cygdrive/c/` vs `C:\ ` I fixed this by using `realpath --relative-to=$(pwd) path/to/jarfile` So it will always enforce a relative path iso using `/cygdrive/c/.../path/to/jarfile` – Rik Jun 14 '16 at 09:42
  • 1
    I had the same problem, with my jar too, i accidentally mistyped the path of the jar – Ori Wiesel Nov 22 '17 at 07:06
  • 1
    Try original path of the jar file. – Oguzhan Cevik Jun 20 '18 at 18:18
  • 1
    It's because when you changed the location, you had to name the specific path to jar – Manish Patel Jul 31 '19 at 08:50
  • 1
    @Rik Your comment is helpful although it won't work in dos paths and java file in cygwin path. The solution here will be `java -jar $(cygpath -a -d \`realpath --relative-to=$(pwd) $HOME/opt/javafile.jar\`)` – Dmitry Sep 12 '19 at 10:17
  • I've seen this happen with jar files inside `Documents` or `Desktop` folders on Mac and Windows. It allegedly has to do with some security mechanism aimed to prevent non-technical users from executing downloaded files. In our case the jar was executed by a Gradle build so this was at the bottom of a pretty long stack trace. – toniedzwiedz May 31 '23 at 13:45
48

[Possibly Windows only]

Beware of spaces in the path, even when your jar is in the current working directory. For example, for me this was failing:

java -jar myjar.jar

I was able to fix this by givng the full, quoted path to the jar:

java -jar "%~dp0\myjar.jar" 

Credit goes to this answer for setting me on the right path....

Community
  • 1
  • 1
Robert Brown
  • 10,888
  • 7
  • 34
  • 40
  • Thanks for this. Need to give the full path in Linux as well, if trying to run it from a different directory. – Pubudu Jun 29 '16 at 15:02
  • This answer is partly a duplicate of an already given answer on this question. This is the earlier answer with the same method: full quoted path. [Sep 6 '15](https://stackoverflow.com/a/32426647/6006571) by @benez – sophievda Oct 28 '19 at 15:46
25

I had this issue under CygWin in Windows. I have read elsewhere that Java does not understand the CygWin paths (/cygdrive/c/some/dir instead of C:\some\dir) - so I used a relative path instead: ../../some/dir/sbt-launch.jar.

radumanolescu
  • 4,059
  • 2
  • 31
  • 44
23

I had the same issue when trying to launch the jar file. The path contained a space, so I had to place quotes around. Instead of:

java -jar C:\Path to File\myJar.jar

i had to write

java -jar "C:\Path to File\myJar.jar"
Dharman
  • 30,962
  • 25
  • 85
  • 135
benez
  • 1,856
  • 22
  • 28
  • The quotations did the trick for me since I also had spaces inbetween. I then tried it without spaces in the path and without quotation marks and then it worked also – Timothy Feb 19 '17 at 17:25
  • Anno 2019: This solution helped for me, with quotation marks! Without quotation marks and without spaces it doesn't work in my case. – sophievda Oct 28 '19 at 15:38
16

Just came across the same problem trying to make a bad USB...

I tried to run this command in admin cmd

java -jar c:\fw\ducky\duckencode.jar -I c:\fw\ducky\HelloWorld.txt  -o c:\fw\ducky\inject.bin

But got this error:

Error: unable to access jarfile c:\fw\ducky\duckencode.jar

Solution

1st step

Right click the jarfile in question. Click properties. Click the unblock tab in bottom right corner. The file was blocked, because it was downloaded and not created on my PC.

2nd step

In the cmd I changed the directory to where the jar file is located.

cd C:\fw\ducky\

Then I typed dir and saw the file was named duckencode.jar.jar

So in cmd I changed the original command to reference the file with .jar.jar

java -jar c:\fw\ducky\duckencode.jar.jar -I c:\fw\ducky\HelloWorld.txt  -o c:\fw\ducky\inject.bin

That command executed without error messages and the inject.bin I was trying to create was now located in the directory.

Hope this helps.

mcls
  • 9,071
  • 2
  • 29
  • 28
Leon thomas
  • 169
  • 1
  • 3
9

None of the provided answers worked for me on macOS 11 Big Sur. The problem turned out to be that programs require special permission to access the Desktop, Documents, and Downloads folders, and Java breaks both the exception for directly opened files and the permission request popup.

Fixes:

  • Move the .jar into a folder that isn’t (and isn’t under) Documents, Desktop, or Downloads.
  • Manually grant the permission. Go to System Preferences → Security and Privacy → Privacy → Files and Folders → java, and check the appropriate folders.
twhb
  • 4,294
  • 2
  • 20
  • 23
8

If you are using OSX, downloaded files are tagged with a security flag that prevents unsigned applications from running.

to check this you can view extended attributes on the file

$ ls -l@
-rw-r--r--@ 1 dave  staff  17663235 13 Oct 11:08 server-0.28.2-java8.jar
com.apple.metadata:kMDItemWhereFroms         619
com.apple.quarantine          68

You can then clear the attributes with

xattr -c file.jar
nick fox
  • 570
  • 8
  • 15
  • see also https://stackoverflow.com/questions/4833052/how-do-i-remove-the-extended-attributes-on-a-file-in-mac-os-x and https://superuser.com/questions/526920/how-to-remove-quarantine-from-file-permissions-in-os-x – nick fox Oct 13 '17 at 10:24
8

I had a similar problem and I even tried running my CMD with administrator rights, but it did not solve the problem.

The basic thing is to make sure to change the Directory in cmd to the current directory where your jar file is.

Do the following steps:

  1. Copy jar file to Desktop.

  2. Run CMD

  3. Type command cd desktop

  4. Then type java -jar filename.jar

This should work.


Edit: From JDK-11 onwards ( JEP 330: Launch Single-File Source-Code Programs )

Since Java 11, java command line tool has been able to run a single-file source-code directly. e.g.

java filename.java
Gabor Dicso
  • 3
  • 1
  • 1
  • 4
Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
5

It can also happen if you don't properly supply your list of parameters. Here's what I was doing:

java -jar test@gmail.com testing_subject file.txt test_send_emails.jar

Instead of the correct version:

java -jar test_send_emails.jar test@gmail.com testing_subject file.txt
Buffalo
  • 3,861
  • 8
  • 44
  • 69
5

This worked for me.

cd /path/to/the/jar/

java -jar ./Calculator.jar
4

For me it happens if you use native Polish chars in foldername that is in the PATH. So maybe using untypical chars was the reason of the problem.

Huxwell
  • 171
  • 2
  • 14
3

sometime it happens when you try to (run or create) a .jar file under /libs folder by right click it in android studio. you can select the dropdown in top of android stuio and change it to app. This will work

enter image description here

anand krish
  • 4,281
  • 4
  • 44
  • 47
2

My particular issue was caused because I was working with directories that involved symbolic links (shortcuts). Consequently, trying java -jar ../../myJar.jar didn't work because I wasn't where I thought I was.

Disregarding relative file paths fixed it right up.

MattSayar
  • 2,078
  • 6
  • 23
  • 28
2

In my case the suggested file name to be used was jarFile*.jar in the command line. The file in the folder was jarFile-1.2.3.jar . So I renamed the file to jarFile. Then I used jarFile.jar instead of jarFile*.jar and then the problem got resolved

suku
  • 10,507
  • 16
  • 75
  • 120
2

It can happen on a windows machine when you have spaces in the names of the folder. The solution would be to enter the path between " ". For example:

java -jar c:\my folder\x.jar    --> 
java -jar "c:\my folder\x.jar"
Sundar
  • 4,580
  • 6
  • 35
  • 61
assaf
  • 21
  • 1
  • 2
    this does not answer the question, you should use comments to ask for more information. – awd Oct 31 '17 at 09:16
2

To avoid any permission issues, try to run it as administrator. This worked for me on Win10.

Gico
  • 1,276
  • 2
  • 15
  • 30
2

I know this thread is years ago and issue was fixed too. But I hope this would helps someone else in future since I've encountered some similar issues while I tried to install Oracle WebLogic 12c and Oracle OFR in which its installer is in .jar format. For mine case, it was either didn't wrap the JDK directory in quotes or simply typo.

Run Command Prompt as administrator and execute the command in this format. Double check the sentence if there is typo.

"C:\Program Files\Java\jdk1.xxxxx\bin\java" -jar C:\Users\xxx\Downloads\xxx.jar

If it shows something like JRE 1.xxx is not a valid JDK Java Home, make sure the System variables for JAVA_HOME in Environment Variables is pointing to the correct JDK directory. JDK 1.8 or above is recommended (2018).

A useful thread here, you may refer it: Why its showing your JDK c:program files\java\jre7 is not a valid JDK while instaling weblogic server?

not_Prince
  • 320
  • 3
  • 17
2

For me it happen because i run it with default java version (7) and not with compiled java version (8) used to create this jar.

So i used:

%Java8_64%\bin\java -jar myjar.jar

Instead of java 7 version:

java -jar myjar.jar
Adir Dayan
  • 1,308
  • 13
  • 21
1

I had a similar problem where TextMate or something replaced the double quotes with the unicode double quotes.

Changing my SELENIUM_SERVER_JAR from the unicode double quotes to regular double quotes and that solved my problem.

TankorSmash
  • 12,186
  • 6
  • 68
  • 106
1

this is because you are looking for the file in the wrong path 1. look for the path of the folder where you placed the file 2. change the directory cd in cmd use the right path

felipe
  • 11
  • 1
1

I use NetBeans and had the same issue. After I ran build and clean project my program was executable. The Java documentation says that the build/clean command is for rebuilding the project from scratch basically and removing any past compiles. I hope this helps. Also, I'd read the documentation. Oracle has NetBeans and Java learning trails. Very helpful. Good luck!

1

Maybe you have specified the wrong version of your jar.

cosbor11
  • 14,709
  • 10
  • 54
  • 69
1

I finally pasted my jar file into the same folder as my JDK so I didn't have to include the paths. I also had to open the command prompt as an admin.

  1. Right click Command Prompt and "Run as administrator"
  2. Navigate to the directory where you saved your jdk to
  3. In the command prompt type: java.exe -jar <jar file name>.jar
1

Keep the file in same directory where you are extracting it. That worked for me.

Aishwary joshi
  • 71
  • 1
  • 1
  • 4
1

If you are on WSL (Windows Subsystem for Linux), and following a guide which say, says this:

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

You actually need to specify the full path, even though you've provided it in the java.library.path part.

java -Djava.library.path=/mnt/c/dynamodb_local/DynamoDBLocal_lib -jar /mnt/c/dynamodb_local/DynamoDBLocal.jar -sharedDb
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
1

This is permission issue, see if the directory is under your User. That's why is working in another folder!

Dimitrios
  • 1,143
  • 11
  • 10
1

Rename the jar file and try

Explanation : yes, I know there are many answers still I want to add one point here which I faced.

I built the jar and I moved it into the server where I deploy (This is the normal process) here the file name which I moved already existed in the server, here the file will override obviously right. In this case, I faced this issue. maybe at the time of overriding there can be a permission copy issue.

Hope this will help someone.

0

Have you tried to run it under administrator privoleges? meaning, running the command in "Run As" and then select administrator with proper admin credentials

worked for me

0

I was trying this:

After giving the file read, write, execute priviledges:

chmod 777 java-repl.jar

alias jr="java -jar $HOME/Dev/java-repl/java-repl.jar"

Unable to access bla bla..., this was on Mac OS though

So I tried this:

alias jr="cd $HOME/Dev/java-repl/ && java -jar java-repl.jar"

Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40
Kinglee
  • 53
  • 1
  • 8
0

This did not work "Unable to access jarfile"

"C:\Program Files\java\jdk-13+33-jre\bin\javaw.exe" -jar "C:\Program Files\Maxim Integrated Products\1-Wire Drivers x64\ OneWireViewer.jar"

This does work

"C:\Program Files\java\jdk-13+33-jre\bin\javaw.exe" -jar "C:\Program Files\Maxim Integrated Products\1-Wire Drivers x64\OneWireViewer.jar"

The difference is the single space in front of OneWireViewer.jar not withstanding that it is surrounded with quotes and even has other spaces.

angar
  • 1
0

For me the problem got resolved after accessing the folder in which the jar was placed earlier I was giving command on D: drive, but when I navigated to the folder in which that jar was placed this problem got resolved

nikhil udgirkar
  • 367
  • 1
  • 7
  • 23
0

If you're working with shared folders on a virtual machine (in my case virtualbox), it seems all my permission were missing to access the file. Additionally, it required elevated privileges to chmod the file.

sudo chmod +rwx filename.jar

After this you should be able to access the jarfile normally.

rishi
  • 652
  • 1
  • 6
  • 20
0

In my case, the / of the jar's path pointed to the root directory of the filesystem, not the root of the project.

The path was fixed by replacing / by ./ to get the current working directory instead of the root directory.

This is possible if you're running the command on another filesystem like with heroku: heroku local web for example.

Herohero
  • 1
  • 2
0

Try specifying the full path to the .jar file 0 That was a likely cause of the Accepted Answer.

So for example (if executing from the root of the project): java -jar target/Calculator.jar

cellepo
  • 4,001
  • 2
  • 38
  • 57
0

Just need to provide exact location of the jar file in quotation ""

instead

java -jar -Dspring.profiles.active=local compass.jar

you have to provide (if it is inside target folder)

java -jar -Dspring.profiles.active=local "target/compass.jar"
-1

first I created a variable %JAVA_HOME% :

When you downloaded the file, make sure to check UNBLOCK TAB in file properties as shown above in "maartencls" post .

c:\users\my user\downloads\location of the file\all> java -jar yourfilename.jar

hope this will resolve the issue :)

Ali Ahsan
  • 985
  • 11
  • 16
-1

You can specify the full path to your java.exe file, for example:

"c:\Program Files\Java\jdk-9.0.4\bin\java.exe" -jar path_to_your_jar_file.jar

it helped me.

Or check what java.exe file runs for default in your system, especially if you have many versions of JDK/JRE.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Ihar Pak
  • 31
  • 2
-2

In my issue to access directory from Command line (in windows) this error

"unnable to access ..."

because wrong file extension

file.jar.jar

! , so change it to correct one

file.jar

:) good luck

Sayid Bro
  • 9
  • 1
-2

First run: java -version

and then run: java -jar Calculator.jar

This worked for me.

ArielGro
  • 795
  • 1
  • 11
  • 24
Sruthy
  • 1
  • Not sure why the `java -version` is needed here? Also, running `java -jar Calculator.jar` is what the OP did and didn't work.... – ArielGro May 26 '20 at 06:31