This question already has answers here:
Including all the jars in a directory within the Java classpath
(25 answers)
Closed 7 years ago.
I am trying to use a feature from the Apache Commons called StringUtils. However this requires you download the libary and add it so I can use the code import org.apache.commons.lang3.StringUtils;. My problem is I am unsure of where to add it to so that I may compile my program in command prompt. I am also unaware of what file I should to add into the required folder.
Any help would be greatly appreciated.
You'll have to make sure that the library (usually a JAR file) is in the classpath when you compile and run your application that uses the library.
The classpath is the set of JAR files and directories that Java uses to find Java class files.
See PATH and CLASSPATH in Oracle's Java Tutorials. (This is about setting the environment variables PATH and CLASSPATH).
As an alternative to setting the CLASSPATH environment variable, you can use the -cp or -classpath options of the javac and java commands:
javac -cp C:\MyProject\lib\somelibrary.jar;. com\mypackage\MyProgram.java
java -cp C:\MyProject\lib\somelibrary.jar;. com.mypackage.MyProgram
If you are not using any IDE, then include below line in your .java file.
import org.apache.commons.lang3.StringUtils;
then we you compile the .java class from cmd prompt, include the apache commons jar in classpath. like
javac -cp ../commons**.jar your_class.java
You will need the CLASSPATH environment variable to point to it. Link.
Given the context of the question I assume you are not using maven or an IDE. If you are there are simpler ways.
Related
So I followed a couple of tutorials and I am basically trying to run a java program on cmd line with an External Jar. I know that there are plenty of questions about this, BUT after trying the code suggested I get two errors.\
Its a simple program called "HelloJar.java" and it utilized Apache Commons Lang String Utils.
Error I see (As you can see I set the classpath and run it.):
Works fine in Eclipse though:
Folder Structure (In jars is the jar necessary and in Src is the project I need):
Here is the .jar file that I import for StringUtils:
What does the cannot find symbol mean?
-----------------------After Rajesh's Answer------------------------------------
Problem here is, jar's not set in classpath and results in compilation errors:
As per path shared, command to compile should be :
cd C:\Users\Controlled\Documents\Eclipse_Projects\HelloWorld\src
javac -cp .;C:\Users\Controlled\Documents\Eclipse_Projects\HelloWorld\jars\* HelloJar.java
Command to run java program with above path:
java -cp .;C:\Users\Controlled\Documents\Eclipse_Projects\HelloWorld\jars\* HelloJar
What does the cannot find symbol mean?
That means, compiler is unable to find the specified path in the classpath. So, that means, when you are trying to run your jar from command line, your library which has
org.apache.commons.lang.StringUtils
is not found. So, in order to fix that, please check if the library is included in your jar or if you have that in classpath and specifying the correct classpath.
Although you did set your classpath to the correct folder, windows fle path separation is different from a Unix Box.
javac -cp ".;./jars/org.apache.commons.lang.StringUtils.jar/" HelloJar.java
java -cp ".;./jars/org.apache.commons.lang.StringUtils.jar/" HelloJar
Another Java distinction is that the classpath is the compile-time path. It is not the same as the execution-time path.
This question already has answers here:
Specifying classpath for a jar
(5 answers)
Closed 8 years ago.
I am not familiar with Java and I am trying to run a piece of Java code packaged as jar file.
On Windows command line, I specify the CLASSPATH to my jar folder like this:
set CLASSPATH="D:\jarFolder"
And there's a test.jar file in that folder. But when I run this
java -jar test.jar
it still failed with this error:
Unable to access jarfile test.jar
I can run the test.jar by specify the full path. But I want to know why the CLASSPATH doesn't work. My understanding is, it tells the java runtime where to locate the jar file.
Jarfiles have to be on the classpath explicitly. Specifying a classpath dir for a jarfile is not sufficient.
See the related Oracle docs on the Java command line:
When you use this option, the JAR file is the source of all user
classes, and other user class path settings are ignored.
Try to use
java -jar D:\jarFolder\test.jar
If you know main class name of your entry point of jar. You can use
set CLASSPATH="D:\jarFolder"
java yourpackage.yourEntryPoint
You can learn your entry point of your jar file using a zip program to extract jar file and reading file entry point section of manifest.mf file.
When you use -jar option, you need to specify which jar. Think of it this way. If you do not give jar name, which jar java should execute? You may have 10,20 may be 100 jars in your class path.
But if you give class name, then java searches among jars and execute correct one.
i have prepared a java file that uses Json library so i downloaded a JSON library from "http://code.google.com/p/json-simple/downloads/detail?name=json_simple-1.1-all.zip&can=2&q="
but very confuse where to place it, so that my java file can detect Json library.
I tried to place the lib "json_simple-1.1" inside "C:\Program Files\Java\jdk1.6.0_07\lib"
but not working yet.
How can i solve the problem using environment variable or any other means where i don't need to set path on comman prompt?
There are number of ways you can include jar file to run your program.
java -classpath "{yourpath}/json.jar:." my.package.Program
java -cp "{yourpath}/json.jar:." my.package.Program
Other way is to set env variable java.ext.dirs.
-Djava.ext.dirs=jarDirectory
You can put it beside your jar (in case you package your application into a jar) and add a link to it in your manifest, e.g.:
your jar contains a META-INF/MANIFEST.MF file which has (besides others) this entry:
Class-Path: json_simple-1.1.jar
More details here.
Try the following:
set CLASSPATH=%CLASSPATH%;<path to libraries folder>
I'm trying to compile a java program that is using JavaDB/Derby. On the command line I can run
java org.apache.derby.tools.sysinfo
without errors, but the following line in several of the files causes the error in my question title:
import org.apache.derby.client.am.SqlException;
causes
package org.apache.derby.client.am does not exist
I've done a fresh installation of JavaDB, but I don't think that matters. I've compiled this project once before, and I KNOW I didn't have JavaDB installed. I just had a directory at the top level of the project folder called lib with all of derby's .jar files inside. And I'm pretty sure I didn't have to set any environment variables either.
How can I fix this error? If I need to provide any more information, I will be happy to do so.
I'm using Windows 7 and jdk1.7
Sounds like you have an issue with the JavaDB JARs not being on your classpath. Make sure you specify them using -cp or -classpath on your javac command.
This question already has answers here:
What is a classpath and how do I set it?
(10 answers)
Closed last year.
I am trying to compile a library I wrote, using javac and I am getting the error: package org.json does not exist. My program includes org.json.JSONArray and org.json.JSONException.
I have this package installed on my computer because I have successfully compiled android apps that import org.json libraries. I'm pretty sure all I have to do is specify a -classpath but I have been unable to find where these files live on my system (ubuntu 10.10 64-bit sun-java6).
Having been unable to find these on my own system I downloaded the org.json files from here, but I was unable to compile them individually because they were co-dependent on each other.
So I have a couple questions:
Does anyone know where the org.json
package lives from android sdk
install?
Where might I find a tutorial
explaining these basic concepts
regarding compiling, and javac.
Whatever external jars you need to compile with should be on the classpath when you compile. The most non-invasive way to do this is do add these items to the javac command line such as
javac -classpath /path/to/json.jar;. -g YourClass.java
or more likely if you use an IDE, add these jars to your referenced jars of the project in your IDE.
It usually isn't a good idea to pollute the global $CLASSPATH variable, as this then gets pulled in for everything you do with java, which may cause unintended conflicts.
Wherever you like. What you need to do is examine your CLASSPATH variable, and make sure it includes the directory with your library.
Here's the first thing:
$ echo $CLASSPATH
and you'll see your classpath as it is.
Now you need to find the jar file containing the org.json; consult the documentation, but it may be something as simple as json.jar. On most LINUX systems you can then just run
$ locate json.jar
And you'll get a path name for the jarfile. Make sure that path is part of your CLASSPATH and you'll be in fat city.
Oh, and the "Getting started" tutorials at Sun Oracle are the easiest place to start.
Actually, having looked at the files, they may not be packaged as a jar file. In that case, you want to put them into your sources starting at some top directory (src in this example.)
/src
/org/json/ ... put the json files here
... put your files here
and when you compile, they'll all be included, which will resolve all the dependencies.
Again, the place to look for first steps is that tutorial.
use "java" command instead of "javac"