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:
Right click on project folder (Calculator)
Select
Click on Java Folder and select "Exectuable Jar File", then select next
Launch Configuration: Main - Calculator
Create Export Destination
Hit "Finish" and profit! Well, not really.
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.
Fixed
I just placed it in a different folder and it worked.
[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....
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.
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"
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.
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.
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:
Copy jar file to Desktop.
Run CMD
Type command cd desktop
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
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
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
This worked for me.
cd /path/to/the/jar/
java -jar ./Calculator.jar
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.
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
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.
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
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"
To avoid any permission issues, try to run it as administrator. This worked for me on Win10.
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?
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
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.
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
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!
Maybe you have specified the wrong version of your jar.
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.
Right click Command Prompt and "Run as administrator"
Navigate to the directory where you saved your jdk to
In the command prompt type: java.exe -jar <jar file name>.jar
Keep the file in same directory where you are extracting it. That worked for me.
This is permission issue, see if the directory is under your User.
That's why is working in another folder!
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.
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
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"
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.
I installed JDK on windows 10, 64bit, followed the documentation instructions.. CMD does not recognize javadoc command. What I tried:
-copied the path of the "bin" folder from Java and in cmd I wrote the command:
set path = "full_path_to_java_sdk_bin_folder"
I saw this on youtube and it worked for the guy, my cmd still did not recognize javadoc command -I set the PATH in system variables from control panel->system->advanced->environment variables and made sure that there are is no other bin folder...
Didn't find any other tips online...
The usual approach for Java development is to set a JAVA_HOME environment variable, and use that to update the PATH (for one thing, it makes it much easier to support multiple versions of Java). Also of note is that Windows puts the quotes oddly on the command line (oddly compared to every other platform that is) and if your path contains spaces you need to quote it correctly. Like,
set "JAVA_HOME=<full_path_to_jdk>"
set "PATH=%JAVA_HOME%\bin;%PATH%"
I've been trying to compile and run this example for javafx https://openjfx.io/openjfx-docs/#maven using Cygwin on Windows 10. It took awhile to get past the compile because my javafx is stored in C:\Program Files\javafx-sdk-13.0.1, the trouble being the space in the folder name. After trying lots of different things I finally found How to cd into a directory with space in the name?, which in a nutshell tells you to put quotes around your environment variable, "PATH_TO_FX".
Then I tried to run the example
$ java --module-path "PATH_TO_FX":mods -m hellofx/hellofx.HelloFX
Error occurred during initialization of boot layerjava.nio.file.InvalidPathException: Illegal char <:> at index 10: PATH_TO_FX:mods
So I thought the PATH_TO_FX was the problem but it turns out it is not.
$ java --module-path src:mods -m hellofx/hellofx.HelloFX
Error occurred during initialization of boot layerjava.nio.file.InvalidPathException: Illegal char <:> at index 3: src:mods
src is a valid directory and I still get the same problem. I think it is related to java being stored in a directory with spaces in it but I'm not sure.
#Ray_Write
isn't ; for Windows? Cygwin uses bash
This has nothing to do with the shell. The parsing of --module-path is handled entirely by the java interpreter, and according to the docs uses ; on Windows instead of :, presumably for congruence with Windows PATH separators.
Since this Java installation is a native Windows application and not one built for Cygwin, one should still use ;. So in effect, this has nothing to do with Cygwin.
For passing file paths to java you may also need to use cygpath to convert the path to its native Windows path.
You're right #Iguananaut, I do need a semi colon, to get my example to work I had to escape it.
java --module-path "$PATH_TO_FX"\;mods -m hellofx/hellofx.HelloFX
where PATH_TO_FX is in .bash_profile as
PATH_TO_FX="C:/Program Files/javafx-sdk-13.0.1/lib"
I made a java program. I used eclipse and it's a maven project. Now when i run the program from windows command prompt, then it's run fine. Here How i am running it from windows command prompt
D:\Personal Work\eclipse 32 Bit\workspace\....\target\classes>
java -cp ".;..\dependency-jars\*" com/softech/ls360/integration/BatchImport vintners
It is working fine. My dependency jar folder contains these jar files
Now when i run the same program from linux. Here how i am runnign it
root#Basit:/home/test/script/classes# java -cp .;../dependency-jars/*; com.s
oftech.ls360.integration.BatchImport vintners
Then i am getting the errors that
....
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
show splash screen with specified image
../dependency-jars/commons-collections-3.2.1.jar: line 1: PK??: command not found
../dependency-jars/commons-collections-3.2.1.jar: line 2:
../dependency-jars/commons-collections-3.2.1.jar: line 2: ?8: command not found
../dependency-jars/commons-collections-3.2.1.jar: line 3: syntax error near unex
pected token `)'
../dependency-jars/commons-collections-3.2.1.jar: line 3: ? ¶META-INF/MANIFE
ST.MF?VKo
_¦?z? ?%+v?N??!ö!P#
(
_?o.5?$
com.softech.ls360.integration.BatchImport: command not found
Why i am getting these errors. How can i run it on linux? Please help
Thanks
You need to use : instead of ; in classpath on linux envrionment. Assuming you have jars placed properly, then simply changing this:
java -cp .;../dependency-jars/*; com.s
oftech.ls360.integration.BatchImport vintners
to
java -cp .:../dependency-jars/*: com.s
oftech.ls360.integration.BatchImport vintners
should work
Learn more about setting classpath here: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
The semicolon is making Bash invoke the java command with no classpath, then tries to execute each jar directly, seeking a shebang where none exists. This leads to JAR headers being printed as part of the error.
Use : to separate jars instead of semicolons on Linux.
You need to make two changes:
First, the classpath separator is ':' rather than ';' on Linux
Second, you'll need to escape the wildcard character with a backslash ('\'), or the shell will expand it and mess things up. You want Java to see the '*' character and expand it itself. The Windows shell doesn't expand wildcards on command lines, so this isn't a problem there.
So, all together, you'll want to use something like
java -cp .:../dependency-jars/\*: com.softech.ls360.integration.BatchImport vintners
You should use : instead of ; as a sperator of the class path files.
I use Eclipse to develop Java so I have a folder full of Eclipse Java Project folders. The /bin folder resides in each folder, so to run the project from Cygwin, the classpath must be set (on my system) to: "E:/programming/java/workspace/SomeProject/bin". Since there are ~40 projects in my folder, I'd rather make a script to add the paths to the CLASSPATH. My script seems to add the paths to CLASSPATH, but when I try to run Java I get a class not found error. In my .bashrc here is my script:
JAVAWORKSPACE="/cygdrive/e/programming/java/workspace/*"
BIN="/bin;"
for f in $JAVAWORKSPACE
do
if [ -d $f ] ; then
export CLASSPATH="$f$BIN$CLASSPATH"
fi
done
When I start Cygwin and echo $CLASSPATH, all of the directories show up, but java can't find the classes. I have also tried JAVAWORKSPACE="E:\programming\java\workspace\* but this resulted in nothing being added to CLASSPATH. If I go through the Windows settings and manually enter "E:/programming/java/workspace/MyProject/bin" to the CLASSPATH, command line Java has no trouble finding the classes. What's up with this? I'm not sure if it's a problem with the script or if CLASSPATH doesn't like unix-style paths. If I need to add windows paths, please help me change my script to do this. Thanks!
I don't have Cygwin set up right now, but I ran into this problem a number of years ago. Java knows nothing about Cygwin pathnames, and bash treats a single backslash as an escape character, stripping it before it can be transmitted to java(c). If you do
echo E:\programming\java\workspace\*
You'll see it outputs E:programmingjavaworkspace*, not what you're expecting. The key is to either escape the escape chars, like
E:\\programming\\java\\workspace\\*
or even better, use cygpath like this.