I m running a java program from a batch file which refences some external jar files .How do i include those jar files in my batch file.Please help
Look at the Sun's official documentation: Setting the class path to understand your options.
A quick way would be just to include your JAR(s) after the -cp:
on Windows
java -cp C:\java\MyClasses\myclasses.jar;C:\java\MyClasses\myclassesAnother.jar utility.myapp.Cool
on Linux/Unix
java -cp /opt/thirdparty/myclasses.jar:/opt/thirdparty/myclassesAnother.jar utility.myapp.Cool
You need to set classpath http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html. E.g.,
java -cp file1.jar;file2.jar yourApp
or if your jar-files are located in directory lib/
java -cp lib/* yourApp
Not to bother with -classpath parameter you could put references to the jar files into the manifest.mf of your application JAR, if it's you application of course.
Adding Classes to the JAR File's Classpath
Sorry, I dont know others IDE, but works with me on Eclipse.
Right click on your project, select
Properties/ Java Build Path/ Libraries/ Classpath/ Add External JARs...
Then choose whatever files you need :v
You have to fill the Class-Path parameter of the manifest file of the JAR. The standard documentation explains that very well.
When running a jar or class file specify classpath option
Related
We know that we use the below for setting classpath for java execution from UNIX. This will work for ading all the jar files to classpath.
java -cp ..jars/* MainClass
But if I want to add an xml file to classpath, what should I do?
I tried the below and it is not working.
java -cp ..jars/*:../resources/abc.xml MainClass
In eclipse, to do the same thing, I just have to right click the resources folder and select "Use as Source Folder".
Can you please help?
You don't need to add the xml file name to the classpath. Just add the path to the folder which holds the xml file. In your example: ../resources/
I am not able to include include external jars from command line on ubuntu linux while compiling my java program.My external jars are in /home/ubuntu/lib directory
Im using this:
javac -cp /home/ubuntu/lib c.java
but it fails to compile.Can anyone suggest what im doing wrong?
Try this
javac -cp "/home/ubuntu/lib/*" c.java
you have to specify the jars directly as described here
You can try placing the jar files in the ext folder of your jdk/jre/lib/ext folder.
I am inside a E:/foo/bar folder inside windows environment. I have a jar file as E:/foo/bar/a.jar. Also inside foo/bar there are lot of jar files. Also under the foo/bar there is another directory called ext. It also includes lot of jar files.
I want to run the a.jar with those jars included in the class path(both current directory and ext directory). So I used this.
java -jar a.jar -classpath *:/ext/*
This didn't work. I didnt add any entries in the MANIFEST.MF in a.jar thinking that this will be fetched from the -classpath entry. What am I doing wrong here?
Thank you
The -classpath option is ignored if -jar is specified.
Two possible course of action I can see:
Put a manifest in the main Jar that does specify the other Jars.
Specify the class-path at run-time.
Drop the -jar option of the invocation.
Specify all the dependent Jars
Add the fully qualified class name to the end of the command, something like java -classpath *:/ext/* com.our.MainClass
I've got a weird problem that I can't understand... I have a simple HelloWorld jar that I built in Eclipse which has the Apache Loggings jar on it's classpath. I've written a script to run the jar:
#!/bin/sh
export CLASSPATH=lib/*:$CLASSPATH
java -jar HelloWorld.jar
The directory structure here is a main directory with the HelloWorld.jar and a lib subdirectory holding the commons-logging-1.1.1.jar.
Running this script works fine. However, when I place the HelloWorld.jar into the lib directory (i.e. to contain all the JARs in one place), and executing java -jar lib/HelloWorld.jar, I get:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
error. Why?!?!?!?!
I'm asking this because I've used the JarBundler on OSX to create an Application bundle for the HelloWorld app and placed a modified script in the MacOS directory whereas all the libs are placed in the Resources/Java directory. Modified version of the script is:
#!/bin/sh
RESOURCE_DIR=$(cd "../Resources"; pwd)
export CLASSPATH=$RESOURCE_DIR/Java/*:$CLASSPATH
java -jar $RESOURCE_DIR/Java/HelloWorld.jar
and I'm getting the same error as above I'd really appreciate any help understanding why I can't do this and/or how to fix it?
Classpath doesn't work with wildcards. Every jar has to be specified explicitly, either as part of the CLASSPATH variable or in the manifest of another jar that is included in the classpath.
Also, IIRC java -jar ignores all the third party jars that are present in the classpath. Why not do this instead?
java -cp yourJar:logJars <mainClass>
Try to add the commons-logging-1.1.1.jar to the CLASSPATH directly
Java will not work with lib/* but the shell may be expanding it for you. Double check this. Put a line like this after export:
echo $CLASSPATH
Also, I would recommend putting it in the MANIFEST file as already mentioned.
EDIT:
Is it a permission problem? If you run the app as root/admin or put the file somewhere else and use a fully-qualified path does it work?
Use the MANIFEST file (META-INF folder) to deal with Classpath entries. Use relative paths for the libraries.
For further info, take a look here.
Basically, for the case with commons-logging inside a lib folder:
Class-Path: lib/commons-logging-1.1.1.jar
And for both jars in the same folder:
Class-Path: commons-logging-1.1.1.jar
Thanks to everyone for their help in figuring this out. Basically, a manifest file was being created and bundled into the jar without my knowledge so any $CLASSPATH or -cp flags were being ignored. In my Eclipse project, I had my classpath set to $(projectRoot)/lib which coincidentally the same directory structure as my dist directory. However, when they were bundled into one directory by OSX's JarBundler, the directory was no long present, hence the classpath errors!
I tried removing the Class-Path attribute from the MANIFEST.MF that Eclispe created but the command line $CLASSPATH and/or -cp entries still don't seem to make a difference... Does the existence of a manifest file negate all command line classpath entries?
have you set log4j.jar into your class. i think you didnt added log4j.jar of to its class path.
I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line: <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>)
Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
Are you specifying the classpath to java on the command line?
$ java -cp lib/commons-lang-2.4.jar your.main.Class
The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.
If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:
java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).
Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with
java -jar yourProgram.jar
There are several variants depending on your needs.
Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See #BalusC's answer for good coverage of that.
Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.
You can usually go to your project bin directory and do:
java -cp . MyClass
But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.
make sure your jar commons-lang-2.4.jar in classpath and not redudance.
I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth