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
Related
I have just shifted back from an IDE to Notepad to write a Java program. The program is using 20 JARs. I compiled successfully. When I decided to run the Java class file using
java -cp ".\\*" MyProgram
it was giving the standard error "Couldn't find or load main class....".
I was confused because when I used to run the java command with all files in an existing folder, it would just get those JARs as the current folder is already in the classpath. As the program is running from the current folder, I tried using -cp "." to include it explicitly in the classpath but that didn't work either.
Finally I was able to run the program with this command:
java -cp ".\\*;." MyProgram.java
I am asking this question to understand the actual logic behind Java's classpath.
Correct me if I am wrong, but I think that the JAR is just a standard archive in which all the packages are encapsulated in respective folders. If all the JARs are in my current folder including my main class file then why can't I run it with:
java -cp "." MyProgram
or simply:
java MyProgram
If the problem is with the multiple JAR files to include and that's why we used ".\\*" to include all the JARs in the classpath, then why do we have to explicitly include the current folder again in the classpath using:
java ".\\*;." MyProgram
To include all jar required to run your program in command prompt use wildcard *:
java -classpath E:\lib\* HelloWorld
You are using "." that defines current directory and "./*" defines all files in current directory.
The class path is a list of jar files and directories containing the classes and resources of your program. Mentioning a jar file adds its contents to the class path.
"./*" will get you only the jar files in the current directory, "." adds the current directory to the class path. This allows to access all classes (and the jar files as raw file resources but not its contents, i.e. the classes contained in them).
If you need both in the class path, you have to specify both.
You've answered your own question, sort of.
. means that it will look for .class files in the current directory.
JARs act just like a directory. So to have the abc.jar "directory" you would specify abc.jar in your classpath.
If you need both the .class files present in the current directory, and the .class files packaged into JARs found in the current directory, you would have the following classpath: -cp ".:*.jar
All the answers here are telling you to use the wildcard without extension (* or ./*) but this is a bad practice, you don't want Java to go look into irrelevant files, so specify the extension: *.jar.
"." means current directory not files in the directory
"./*" means all files in current directory.
So you want to use all jars in current directory so 2nd will work
This question already has answers here:
NoClassDefFound when trying to run java with external libraries
(4 answers)
Closed 7 years ago.
How do I run a program in Java, with several referenced libraries, .jar files, from a command line?
I have put all my .jars in /lib folder, which is in the root folder of my project, and added those .jars to the build path.
So my project now looks something like this:
Project:
-->/src/Entry.java, ... (all them .java files)
-->/bin/Entry.class, ... (all them other .class files)
-->/lib/commons-codec-1.10./(all them .jars)*
Now when i try to run the program from the cmd:
I locate myself within the /bin folder and execute java Entry, but I get NoClassDefFound exception
How should I run this?
you need to tell JVM where to look for classes while running the program.
the parameter that we use to tell jvm that is known as classpath
there are different ways to achieve that
Recomended Add the classpath location to the run command , alternatively pass the jar locations, assuming you have two jar files a.jar and b.jar under you lib folder, the command should be java -cp".;lib/a.jar;lib/b.jar" Entry
Either put the jar files into a location that is already under classpath (Since current folder is always under classpath, easiest option would be to put the jar under current folder , but this is not a recomended way to achieve)
Modify you classpath variable under environment properties to list the folder containing your jar , which is a trivial way for achieving this.
Create one batch file (.bat) and keep the jars inside that. whenever you want to run then directly run that batch file. I guess its very simple and efficient.
Example:
#echo off
SET PATH=%PATH%;E:\Java\jdk1.6.0_45\bin // JDK path
SET LIB=%cd%\lib
set CP=""
set CP=%CP%;%LIB%\antlr-2.7.6
set CP=%CP%;%LIB%\commons-codec-1.8.jar
set CP=%CP%;%LIB%\opencsv-2.3.jar
javac -classpath %cp% *.java
java -classpath %CP% -Xms256m -Xmx1024m -Xss2m T2DPreProcessing
pause
I hope it will help you. Thanks.
I have a Java application which I'm executing on Linux direct from an executable jar file
java -cp .:./lib -Duser.timezone=GMT -Dlog4j.debug -jar programName.jar
The program uses a number of other jar files which are all in one directory and 4 properties files all of which are in another directory (the current directory). Both directories are included in the CLASSPATH.
Simple enough right.
It would be, except that Log4j fails to find log4j.properties. The only way I have managed to make it find log4j.properties is to include it in programName.jar
This is not what I want, I want to have it using log4j.properties residing in the same directory as all the other properties files, they are in the CLASSPATH and are found as you would expect.
The other jar files being used are:
jdom-2.0.5.jar
log4j-1.2.17.jar
ojdbc7.jar
quartz-2.2.1.jar
slf4j-api-1.7.7.jar
slf4j-log4j12-1.7.7.jar
I'm wondering if slf4j-log4j12-1.7.7.jar does some configuration which prevents log4j from scanning the CLASSPATH when looking for the properties file. My code does not include any instructions which aim to specify the location of the properties file.
I've not yet tried executing the program without the -jar option, I will try that next.
Does this ring any bells so far ?
Add an argument to jvm (log4j.configuration). e.g.:
java -cp .:./lib -Dlog4j.configuration=file:log4j.properties -Duser.timezone=GMT ...
You may want to see this answer for more options.
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.
I have a jar file which I do not have the source code but want to use.
The jar file prompts for a file to read and generates an output file using a combinatin of the input file and a number of 'helper' files it uses for data. It works perfecty fine if run from its expected home directory, but I'm trying to write a script which will allow running the jar from anywhere.
The problem is that if I try running the jar file from anywhere other then its home directories it fails to find the support files it needs to properly generate its data.
If I run the file from its expected home directory I have to give the full address of the input file or it won't find it. I would prefer to be able to give just the relative path and Java know to look at whatever directory the person calling my script is in.
Is there a way I can have a bash script pass a command line argument to Java that would ensure that this jar looks at both of the relevant directories (directory of the helper files and the current dir of the person calling the script) when trying to resolve a relative file path? Something like the -classpath argument?
With the --classpath (or -cp) you can tell your Java program where it should take the dependency classes. So, probably if you do like in your files directory
$JAVA_HOME/bin/java -cp '.:/path/to/the/original/program' My.class myfile.txt
then it will wind the program, and find your files as well.
UPDATE
If it doesn't work, you can try to force the file loading some other way. The Javadoc says:
By default the classes in the java.io package always
resolve relative pathnames against the current user directory. This
directory is named by the system property user.dir, and
is typically the directory in which the Java virtual machine was
invoked.
So, you can try running the program from the original directory this way:
$JAVA_HOME/bin/java -Duser.dir=/path/to/the/files/directory My.class myfile.txt
UPDATE2:
As I wrote in a comment, you can try symlinks. Execute the following commands in the original directory:
ln -s /path/to/the/files/directory datafiles
$JAVA_HOME/bin/java My.class datafiles/myfile.txt
Sorry - ignore. I missed the first line of your question.
You could pass the two paths as an argument to the jar file - then append the path location at runtime. Many ways to do that, here is one:
java -DdirectoryA="/somewhere" -DdirectoryB="/elsewhere" -jar program.jar
and in your code
String pathA = System.getProperty("directoryA");