Error unable to access jarfile C:\Jar - java

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?

That's because of the space you have in the path name. On Windows, use quotes:
java -jar "C:\Jar Folder\P2.jar"

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

Related

JAR file is not read even though added in class path

I have a shell script which runs a java class presnet in /opt/mydir/ directory. In this script I am adding the class file along with few other jar files to the java classpath.
Ex:
/usr/bin/java -cp "/opt/mydir"
When the script is run, the java .class file fails with
Exception in thread "main" java.lang.NoClassDefFoundError: com/internal/altsite
The com.inetrnal.altsite comes from an external jar, and is also present in the same classpath as the main class file.
If i add the name of the jar explicitly to the classpath, the shell script runs successfully without any error. Ex:
/usr/bin/java -cp "/opt/mydir:/opt/mydir/altsite.jar:"
The directory structor is as follows:
-/opt
---mydir/
-------runner.class,altsite.jar,jose4j.jar

Read properties file from classpath (non-maven)

When I try to run my jar from another directory it cannot see the "config" folder with the "url.properties" file in it.
Inside MyProperties class I have the following code, which runs perfectly when run from the same dir:
Properties properties = new Properties();
FileInputStream in = new FileInputStream("./config/url.properties");
properties.load(new InputStreamReader(in, Charset.forName("UTF-8")));
in.close();
The class that contains this code is in the following tree:
myproject\MyAppClass.class
myproject\data\MyProperties.class
It runs fine if I run this jar which contains the above piece of code by doing:
C:\myjarfolder\>java -jar myApp.jar
But it does not find "url.properties" inside "config" and returns a error if I do:
C:\>java -jar c:\myjarfolder\myApp.jar
Obviously it seems a classpath problem, so I try the following without success and return the same "file not found" error:
C:\> java -cp "c:\myjarfolder\*;c:\myjarfolder\config" myproject.MyAppClass
C:\> java -cp "c:\myjarfolder\*;c:\myjarfolder\config\" myproject.MyAppClass
C:\> java -cp "c:\myjarfolder\*;config" myproject.MyAppClass
C:\> java -cp "c:\myjarfolder\*;config\" myproject.MyAppClass
C:\> java -cp "c:\myjarfolder\*;\config\" myproject.MyAppClass
C:\> java -cp "c:\myjarfolder\*;c:\myjarfolder\config\url.properties" myproject.MyAppClass
And many other variants with and without quotes, all without success.
Then I thought it could be a loaded resources problem and tryed to change the InputStream(its in a static method):
Properties properties = new Properties();
properties.load(MyProperties.class.getClassLoader().getResourceAsStream("./config/url.properties"));
and then
properties.load(MyProperties.class.getClassLoader().getResourceAsStream("config/url.properties"));
This way it does not work even when running in the same folder.
I also tried to put the config folder in the class-path: inside manifest. Dind't worked.
I tried many things I found here on stack and none seems to work.
Is there a way to make the first option (without getresources) to run from another dir?
If I somehow make it load from getResource, will it run from another dir if I refer the class path?
When using absolute locations like FileInputStream("./config/url.properties"):
Add the config folder inside the jar. From the IDE you can add it as a source and it will compile the folder inside the jar automatically. Then it can be run with "java -jar c:\myjarfolder\myJar.jar" from another folder no problem.
When using getResources("url.properties"):
Can't use the absolute location here or it will be the same problem as before, so to let the folder outside the jar and add it to the classpath (you may not run it from the IDE if not added to the run parameters classpath or the manifest).
The folder can be added by running it like:
'java -cp "c:\myjarfolder*;c:\myjarfolder\config" myproject.MyAppClass'
Also, it can be added to the MANIFEST.MF class-path, as example "class-path: config/".
(it only worked here with "/") and can be run with "java -jar c:\myjarfolder\myJar.jar".
I don't think this is a normal issue people will encounter because normaly you run the jar from the same location it is. But we were creating a RDP to run from server where it can't run the jar it self, but using the java.exe.

java jar Error: Unable to access from command line

I cant execute java jar file from cmd windows 10.
I have generated a jar file using Intellji.
when I run the command Java -jar I got this error
Error: Unable to access jarfile
I tried running the cmd with administration right but same error. the class path is set correctly. the jar file is located in the right directory .
What could be causing the problem?
Thank you
make sure you use
java -jar TestJar.jar
instead of
java -jar TestJar
If this is not your error, the file might not exist or you donĀ“t have permission to open it.

executing java -jar via classpath vs in the jar file dir

After having used NetBeans to create a Java program call it Addition and then having successfully cleaned and built an Executable Jar File in a folder c:\Users\Ben\Doc\NetBeansProjects\Addition\dist
WHY is it that when executing, from command prompt,
c:\Users\Ben Java -Jar -cp "c:\Users\Ben\Doc\NetBeansProjects\Addition\dist" Addition.jar
it does NOT work (i get 'unable to access jarfile Addition.jar)
BUT if i use cd to change my current dir to c:\Users\Ben\Doc\NetBeansProjects\Addition\dist and THEN run 'java -jar Addition.jar' from there, the Addition program runs fine
The -classpath argument is ignored when you use the -jar option. See the documentation.
because java doesn't look in classpath to launch jar file for this command it needs file as input
so if you set the directory where your jar file is placed and try to execute java -jar command and expect it to pick up jar from that directory because it is in classpath it is not valid
you can give full path to jar like from any directory
java -jar c:\Users\Ben\Doc\NetBeansProjects\Addition\dist\Addition.jar

Packing files as jar file

I am trying to create a jar file off of a class and xml file I have in a directory called SOACustomFunction.
OS: Windows 7 Enterprise - 64 Bit
Java: jdk1.6.0_21
I tried using the command below and I keep getting the error:
Error: "Unable to access jarfile SOACustomFunction.jar"
C:\SOACustomFunction>"C:\Program Files\Java\jdk1.6.0_21\bin\java.exe" -jar -cvf
SOACustomFunction.jar *.*
Any ideas what I am doing wrong?
Thanks,
S
The command you show is what you would use to EXECUTE the jar file. Use jar.exe instead of java.exe -jar
Typically you would have
jar cvf yourJarFile.jar yourClass.class yourSecondClass.class
java -jar is for running a class from a jar file.
Assuming your classes and xml reside inside your SOACustomFunction directory, the command would be
cd SOACustomFunction
jar cvf myjar.jar yourclass.class yourxml.xml
But do maintain a package structure for your classes and other resources instead of having them all in the jar's root directory.

Categories