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 am trying to install Mooshak which is distributed as a .jar installer. When I run the file, it gives me the following error about a missing class:
ifinlay#mooshak:~$ sudo java -jar MooshakInstaller.jar -cui
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/ArchiveException
at pt.up.fc.dcc.mooshak.installer.Installer.<init>(Installer.java:26)
at pt.up.fc.dcc.mooshak.installer.Installer.main(Installer.java:52)
... 5 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.compress.archivers.ArchiveException
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:436)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 7 more
So it can't find the org/apache/commons/compress/archivers/ArchiveException class. But I have installed this library and have the following in my CLASSPATH:
ifinlay#mooshak:~$ echo $CLASSPATH
.:/usr/share/maven-repo/org/apache/commons/commons-compress/1.18/commons-compress-1.18.jar
And this .jar file in the CLASSPATH does seem to contain the class in question:
ifinlay#mooshak:~$ jar tvf /usr/share/maven-repo/org/apache/commons/commons-compress/1.18/commons-compress-1.18.jar
# ... other classes edited out
722 Fri Mar 01 16:57:28 UTC 2019 org/apache/commons/compress/archivers/ArchiveException.class
# ... other classes edit out
So why can Java not find this class when it does seem to be in the path? Any help would be so awesome, I'm really scratching my head at this.
Thanks for reading!
EDIT: The Manifest file for the .jar looks like this:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ junit.jar org.hamcrest.core_1.3.0.v201303031735.ja
r commons-compress-1.12.jar BrowserLauncher2-all-1_3.jar
Class-Path: .
Rsrc-Main-Class: pt.up.fc.dcc.mooshak.installer.Installer
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
So this seems to imply that the commons-compress-1.12.jar file should just be placed in the same directory as the installer. This doesn't seem to work though...
Why is Java not finding classes that are in the CLASSPATH?
Short answer: because they are NOT on the actual classpath.
Long answer:
There is either something wrong with the installer, or you are not running it correctly / as it is designed to be run.
When you run a java like this with a -jar option:
java -jar MooshakInstaller.jar -cui
it will ignore the CLASSPATH variable, and any -cp arguments that you add. That explains why Java is not finding classes in the commons-compress-1.18.jar JAR file on your (assumed) classpath.
This means one of the following:
The MooshakInstaller.jar installer is missing a dependency; i.e. it is broken. (I doubt this is the case. They would have spotted this.)
The MooshakInstaller.jar installer is not designed to be run as an executable JAR.
The MooshakInstaller.jar installer is expecting to find the dependency in some location relative to the location of the JAR file. (See if there is a "Class Path" attribute in the JAR's manifest file.)
I would also note that when you run this:
sudo java -jar MooshakInstaller.jar -cui
the value of the CLASSPATH environment variable will probably not be passed through to the root process. Check the manual entry for sudo. This is not the actual problem here ... because the CLASSPATH variable is being ignored anyway, due to the -jar option.
UPDATE
It looks like you simply didn't follow the installation instructions:
To install Mooshak you must execute the following commands. Make sure your system has all requirements. Note that you must have root privileges
% tar xzf mooshak-version.tgz
% cd mooshak-version
% su
# ./install
That's not what you did, and I strongly suspect that what you actually did is not equivalent to the above.
(I'm not going to investigate this further, because the download procedure requires me to give them all sorts of personal details. No way Jose!)
This answer is based on the new information you provided. It changes things.
First of all, what I said about CLASSPATH and -jar remains true.
However, it turns out that the Mooshak 2 installer is an executable JAR that uses a "jar in jar" loader.
The main class is org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.
The manifest specifies that the classpath for the installer should include these nested JARs which are part of the installer JAR:
junit.jar
org.hamcrest.core_1.3.0.v201303031735.jar
commons-compress-1.12.jar
BrowserLauncher2-all-1_3.jar
The JarRsrcLoader entrypoint classes should then set up classloaders for the above JARs, and then start the installer.
And it should all just work.
I do see one strange thing though. The manifest has commons-compress-1.12.jar, but you seem to be trying to use commons-compress-1.18.jar.
You said:
So this seems to imply that the commons-compress-1.12.jar file should just be placed in the same directory as the installer.
I don't read it that way, but I will see if I can find the documentation.
UPDATE: nope. I couldn't find the documentation, but it seems from the JarRsrcLoader source code, that it doesn't work that way.
But I did find a clue. In the current version of JarRsrcLoader there is separate code for Java 8 and Java 9+. So, if the Mooshak 2.0 installer uses an older version of JarRsrcLoader, AND you are trying to install with Java 9 or later, then it is possible that it won't work.
So, make sure that you use Java 8 ... like the requirements say.
However, what you are apparently trying to do here is to use a different version of the compress library. The better way to do this is to set up your own build tree for Mooshak:
... checkout the full source code with svn checkout https://svn.dcc.fc.up.pt/projects/Mooshak/Mooshak/
Then modify the dependencies and build / install as per the instructions that I presume are in the tree.
How to run a java file that has external jars in Linux. Also how to run it in Windows?
I tried the following, but not working.
javac -cp c:/lib/lib1.jar;c:/lib/lib2.jar c:/com/example/Application.java
thanks
------EDITED------
Now my class file got generated after compiling without any error. But when i run the file its showing following error
java -cp C:/lib/lib1.jar;C:/lib/lib2.jar C:/com/example/Application
Exception in thread "main" java.lang.NoClassDefFoundError: C:/com/example/Application
Caused by: java.lang.ClassNotFoundException: C:/com/example/Application
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Any suggestion...
If you are running on linux, then there should be different directory structure from windows. Also the classpath on linux should contains paths separated path by colon(:) instead of semi-colon(;)
Put your jars in a directory on linux machines, may be in /var/tmp/myjars. And then compile your code using the below command:
javac -cp /var/tmp/myjars/lib1.jar;/var/tmp/myjars/lib2.jar Application.java
EDIT
As you have updated your question. You need to run your class on windows as mentioned here. I am taking few assumptions here that your Application class is in com.example package. If that is the case then you can run it from the folder containing the com folder as mentioned here:
java -cp C:/lib/lib1.jar;C:/lib/lib2.jar com.example.Application
javac is the Java compiler. after compiling the program the running on windows should work like this
java -cp c:/lib/lib1.jar;c:/lib/lib2.jar c:/com/example/Application
You can try this
java -cp classpath=%classpath%;< jar-file full-path>; ClassName
Ex: java -cp classpath=%classpath%;c:/lib/lib1.jar;c:/lib/lib2.jar com.ab.cd.MyClass
I've been trying to execute the commands to run the RMI Hello world example but I Failed!
My execution steps are taken from here: http://docs.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html
The commands are:
javac: works fine and I get the class files
rmiregistry &: I get something like [1] 17122
java -Djava.rmi.server.codebase=file:/users/ha/RMI/ example.hello.Server:
Gives me an error message
Exception in thread "main" java.lang.NoClassDefFoundError:
example/hello/Server Caused by: java.lang.ClassNotFoundException:
example.hello.Server at
java.net.URLClassLoader$1.run(URLClassLoader.java:202) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:190) at
java.lang.ClassLoader.loadClass(ClassLoader.java:306) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at
java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I did solve this problem by running each command on a new terminal window.
First I started with javac for the .java files, then run the command rmiregistry &.
Second I opened a new terminal window and run the command java for the Server class.
Third I opened a new terminal window and run the command java for the Client class.
and it works with me!
On the Mac, the Users directory in the root directory is written with a capital letter.
You wrote:
file:/users/ha/RMI/
file:/Users/ha/RMI/ would be correct. This applies only to file systems that have been setup explicitly to setup to ignore case (Case-insensitive). At least that is what the comments below are saying. I cannot verify this myself.
A classpath is also needed under many circumstances. It depends on your dependencies. But if you needed the classpath in windows, you will need a similar one on Mac.
Depending on where you .class files are, I would add the classpath to your invocation.
java -Djava.rmi.server.codebase=file:/users/ha/RMI/ -cp=/Users/ha/RMI example.hello.Server
This assumes, of course, that the class files are under /Users/ha/RMI/example/hello
Just look inside of the directory to verify that the class files are there. It should be the directory that you designated after the javac command and -d argument.
I used three terminal windows for execution of a rmiregistry program.
1.Use this terminal window to compile all your files (javac filename.java) and use the command "rmiregistry &" this is the windows equivalent command for "start rmiregistry". (Note:'start' command doesn't work on the macOS)
2.Use this to run the java file ie. Server file (ie. java RMI_Server)
3.Use this to run the client file ie. Client file (ie. java RMI_Client 127.0.0.1)
And yeah it worked for me. (Note: The Directory should be set properly in the terminal)
I have created some 4 to 5 Java files. They work well when I run using Netbeans or Eclipse, but when I try to run using command prompt I get some errors.
I have put all my files in jdk/bin folder.
I am using MySQL connector jar file - I have put that file in jdk/bin folder
I have set my class path to jdk/bin folder
But I get errors like:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at Dbconnection.Dbconnect(Dbconnection.java:29)
at fileoperation.parsefiles(fileoperation.java:63)
at fileoperation.main(fileoperation.java:127)
Will anybody tell - where is the problem?
Here is what you have to do.
1.It is not a good idea to place source files in the jdk bin directory.so undo that.
2.create a folder say "apps" in your c:(hard disk) if you are using Windows.Exactly this way. C:\apps
3.Save all your source files in that apps folder.
4.Go to your environment variable and create a classpath variable under "user variables for ..."
Click on the new button, under variable name and type "classpath" (without the quotes)
under "variable value",supply the mysql connector/j driver exactly like this with the jar file at the end. C:\mysql-connector-java-x.x.xx\mysql-connector-java-x.x.xx-bin.jar(x marks the version number)
5.click ok.
6.Set the path for your jdk so that you don't keep setting it over and over each time you want to run your program.
C:\program files\java\jdk1.x.x\bin copy this address path from your windows explorer's address bar and put a semicolon (;) before pasting it in the path. You have to do this in your path system variables else,you will end doing this each tme you want to execute some codes.
cd\
cd apps (enter)
set classpath=(enter)
set path=c:\program files\java\jdk1.x.x\bin(enter)
javac ClassName.java(enter)
java ClassName(enter)
(Am so sure you don't want that all the time )
Now to compile,open your command prompt and change directory to your apps directory and type javac to test if your configs did work.You will get a list of the javac options.If you did then you are on the right track again.
open your jdbc code make sure these are rightly coded.
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:portnumber/test","username","password");
compile and run your code again.
Let me know if this helped you
First of all try running java and javac from command propmt, if they run properly its fine.
than Try this:
javac -cp /path to/your/mysql connector jar file/ MyProgram.java
java -cp .;/path to/your/mysql connector jar file/ MyProgram
cp stands for classpath
You can also add multiple jar files after -cp.
if they don't run i mean java or javac not found error comes up than add java.exe and javac.exe to your windows environment variable
Add C:\jdk1.6.0_23\bin which contains java.exe and javac.exe to your environment variable.
You can't set the directory in the classpath, you either need to specify "*.jar" under the directory, or specify each jar by name.