Is it possible to add a utility jar to Window's System Path?
I have findclass.jar that is an exicutable jar located in my C:\ directory.
If I open cmd.exe, change directories to C:\, and type:
java -jar findclass.jar \path\tojar.jar nameofclass.class
it will search another jar for a given class.
How can I add findclass.jar to my Window's System Path so I don't need to change directories to C:\?
I tried set path=C:\;%PATH% and set path=C:\findclass.jar;%PATH%
but I get Error: Unable to access jarfile findclass.jar when I try to execute it from a different directroy.
Is it not possible to add a .jar file to the path?
Add it to CLASSPATH environment variable.
Related
Windows 10 1803 64-bit, Java JRE 8. No JDK installed.
I have a .jar in a folder:
c:\users\myuser\KickAssembler\KickAss.jar
Under System environment variables I have the CLASSPATH as:
c:\users\myuser\KickAssembler\
At a command prompt the CLASSPATH is set (checked with ECHO).
At a prompt when I try:
java -jar KickAss.jar
It returns:
Error: Unable to access jarfile KickAss.jar
If I CD into the directory first, then run it, it works fine. So it looks like CLASSPATH is not being used somehow. I have added the location into the standard Windows PATH also, still doesn't work.
Different combinations of casing for the filename doesn't work. Windows is set to show hidden files. It can't be a permissions issue because it works when I change into the directory first.
Anything else I can try?
CLASSPATH is used to define the path from which Java will load classes. CLASSPATH can contain folders and JAR files, such as: C:\mybin;c:\myjars\some.jar This classpath would load any fully qualified class file residing in or beneath c:\mybin and any fully qualified class within c:\myjars\some.jar. CLASSPATH will not allow JAVA to find your JAR file as you are expecting when using the -jar switch, it does not search for any JAR file along the classpath, it will only look in the ones explicitly stated in the CLASSPATH and then only for class files within them. Note, fully qualified means package + class, such as: com.myorg.somepackage.someclass, not just someclass.
As you observed, if you are in the folder where kickass.jar resides your command line works as the JAR file is present. If you fully reference the JAR file while executing the command from another folder the command line should work as well.
See https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html for more detail on how CLASSPATH works. It is important to have a solid understanding of CLASSPATH when using JAVA.
This question already has an answer here:
Including Jar while executing Java from Bat file
(1 answer)
Closed 4 years ago.
This might be similar to THIS but I can't find a solution written there
Problem is, I am running a java app thru a batch file which needs some jar files to be included. And I can't find a way to do it
File locations:
(java class)
D:\workspace\src\MyClass.java
(jars needed)
D:\workspace\src\lib\opencsv-4.1.jar
D:\workspace\src\lib\common-lang3.jar
Current code on bat file:
#echo off
set CLASSPATH=.
set CLASSPATH=%CLASSPATH%;D:workspace\src
set CLASSPATH=%CLASSPATH%;\lib\opencsv-4.1.jar
set CLASSPATH=%CLASSPATH%;\lib\common-lang3.jar
set ARGS=one two
javac D:workspace\src\MyClass.java
java -cp %CLASSPATH% MyClass %ARGS%
pause
Please help on how to properly include these jar files to be able to run the program. Thanks
Please help on how to properly include these jar files to be able to run the program
Don't start from here. You should:
Deploy your application as a JAR file, naming the main class in the META-INF/MANIFEST.MF file's Main-class entry.
Name the external JAR files in the META-INF/MANIFEST.MF file, in the Class-Path entry. Note that this only allows you to use relative pathnames to the other JAR files, so you essentially have to distribute them with the application JAR file.
Use the java -jar option.
In which case you barely need the batch file at all.
Your relative and absolute path in your script file is not valid...
Especially the relative path in your script...The java file location must be either relative path or absolute path.
You don't need to use the '-cp' flag since you set the CLASSPATH variable.
So, if your jars path is a lib directory under workspace in a D volume.
The script looks like as follows;
#echo off
set CLASSPATH=.
set CLASSPATH=%CLASSPATH%;D:\workspace\src
set CLASSPATH=%CLASSPATH%;D:\workspace\lib\opencsv-4.1.jar
set CLASSPATH=%CLASSPATH%;D:\workspace\lib\common-lang3.jar
set ARGS=one two
cd D:\workspace\src\
javac MyClass.java
java MyClass %ARGS%
pause
I am completely new to programming and I am now doing a project that I need to read a specific XML file in java. My code is using the absolute path of the file I need but I have to distribute the project to somewhere. That required XML file will not be in the project folder but in another folder which I will distribute with the JAR file of the project. Is it possible for it to get that path if I don't know where the user will put my program and the folder containing the XML file?
The program is supposed to run on Windows.
You have different possible solutions:
pass the file location as parameter to the main method (if you have a main method)
set an system property with the location of the file (you can do that with the -D option on the java command, with the syntax -Dproperty=value)
set an environment variable (with export command in linux)
put the file in the classpath and retrieve it with getResourceAsStream
access the file from an external server exposing it via HTTP
I would like to set the -Djava.library.path VM option to a specific folder. However, it is not relative to my project folder so that I could say for example:
-Djava.library.path=native\windows
(The folder native is in the project folder.)
Do you know if this is somehow possible to set it like above and not with -Djava.library.path=C:\...?
You actually can set a relative path. For example if you start your program a specific folder, you can access libraries in a folder "libs" right next to it by setting the path to "../libs" e.g.
In my own project with native libraries I have this in my shell script:
-Djava.library.path=../../native/unix
Hope this answers your question.
I'm not sure if you are asking how to refer to a relative directory on windows, or how to set this path without the -Djava.library.path=... parameter. So, I will answer both.
To set a relative path, use:
-Djava.library.path=.\windows
To set this path on Windows without using -D, augment the PATH environment variable:
setenv PATH %PATH%;C:\path\to\folder
On Linux/Mac, set/augment the LD_LIBRARY_PATH with this folder location.
Under Linux I am trying to run a jar file as follows:
java -jar plantuml.jar -testdot
while having CLASSPATH set to any of the following (the file is located at /home/user/plantuml.jar):
export CLASSPATH=/home/user
export CLASSPATH=/home/user/
export CLASSPATH=/home/user/plantuml.jar
In either case, no matter how I define CLASSPATH, the java command gives an error Unable to access jarfile plantuml.jar. What am I doing wrong here?
You have to supply the complete path after the parameter -jar. So for your example you have to call
java -jar /home/user/plantuml.jar -testdot
The $CLASSPATH is only evaluated to find additional files (classes/resources) but not the jar file defined in the command line.
export CLASSPATH="/path/to/class_or_jar1":"/path/to/class_or_jar2":"${CLASSPATH}"
Maybe you are missing name of the main class or path to the jar. Have you tried execute it:
java -jar full_path/plantuml.jar package.YourClass -testdot
Is your program depending on other classes? If yes you might want to add -cp parameter.
The classpath is used to find classes when you refer to them by name. It's essentially a list of paths (directories AND jar/zip files) where the JVM needs to look for classes, or other resources when using methods like ClassLoader.getResourceAsStream().
The value passed to the -jar option on the command line is the file-path to the JAR file.
So, it won't find a jar file if you are only referring to the jar file by name. The JAR file path in the CLASSPATH is supposed to be a path element that 'contains' other resources.
What you need to do here, is either
Provide the full path to the jar file when trying to execute the jar
Set the classpath to the jar file's path, and run the java command giving the name of the main class you want to execute.