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.
Related
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
I can run java in cygwin+windows using the following settings (the sw/jar directory has several jar files, and I pick the relevant one from the java command line):
CLASSPATH=.;C:\sw\java_6u35\lib\\*;C:\sw\jar\\*
java org.antlr.Tool Calc.g
But I am having the following problems when running in linux:
(1) I can't set a directory name in a classpath, the following line reports an error:
setenv CLASSPATH .:/sw/jdk1.6.0_35/lib/\*:/sw/jar/*
(2) when I run explictly with -jar option, I still get an error:
java -jar /sw/jar/antlr-3.4.jar org.antlr.Tool Calc.g
error(7): cannot find or open file: org.antlr.Tool
However, the class does exist. When I do jar tf /sw/jar/antlr-3.4.jar, I get:
...
org/antlr/Tool.class
So my question is: (a) how do I specify in unix that my jar-directory is xxx that contains several jar files, and (2) how do I pick the relevant jar from this dir at runtime?
To specify multiple jars in a directory, directly in the java command, use this
java -cp "/sw/jar/*" org.antlr.Tool Calc.g
This will include all the jars
If you want to set the classpath in Unix/Linux systems, use this
export CLASSPATH=/sw/jar/a.jar:/sw/jar/b.jar
in unix use this to set the classpath:
export CLASSPATH=myClassPath
about not finding your jar, you're using a leading slash (/), that means that you path is absolute (not relative to your home folder) is this what you want?
if you want the path to be relative to your folder try:
java -jar ~/mypathToMyJar
In my ~/.bashrc I export my java CLASSPATH the following way:
CLASSPATH=$CLASSPATH:/home/user/java/libs/apache-ant-1.9.0/lib/*:/home/user/java/MyProject/libs/*
The folder "/home/user/java/MyProject/libs/" contains the following jars:
colt.jar mysql-connector-java-5.1.22-bin.jar
concurrent.jar postgresql-8.4-703.jdbc4.jar
When I try to compile a class of my project that imports parts the colt.jar, I get an error message:
/src$ javac visualization/VisualizeCorrelationMatrix.java
visualization/VisualizeCorrelationMatrix.java:16: cannot access cern.colt.matrix.impl.SparseDoubleMatrix2D
class file for cern.colt.matrix.impl.SparseDoubleMatrix2D not found
int N = cm.columns();
^
But when compling it the followin way it works without errors:
/src$ javac -cp ".:/home/user/java/MyProject/libs/*" visualization/VisualizeCorrelationMatrix.java
I checked that the paths in the CLASSPATH variable and the manuel optino -cp are 100% identical. Also an echo $CLASSPATH returns the correct path.
Why do I still get this error?
You need to export the new classpath variable in your bashrc line:
export CLASSPATH=$CLASSPATH:/home/user/java/libs/apache-ant-1.9.0/lib/*:/home/user/java/MyProject/libs/*
Also if the shell you are compiling the java in was created before you changed your bashrc file you'll want to reread the bashrc file using source ~/.bashrc.
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
I want to run a simple executable jar like this:
java -jar /path/to/my/jar/myjar.jar
However, i have to put that absolute path every time I want to run it. Is there a way to tell the OS to look for the jar in $PATH environment variable?
You can also copy your jar file to /usr/bin folder, then it will work with just $ myjar.jar
Make sure your jar file has the executable bit (chmod +x myjar.jar) and copy it to /usr/bin (sudo cp myjar.jar /usr/bin/myjar.jar)
I think you'd use the classpath; append your directory to it and java -jar myjar.jar should work.
You could use CLASSPATH environment variable instead
The -jar directive overrides the classpath, using whatever is defined in the jar file itself. The best way to do what you need is to use a tool such as ant, a script, or copy it to somewhere where the OS can find it (as Nejc Saje suggested).