Post date: Jan 19, 2014 6:01:23 PM
PROBLEM:
I need to program in Java and import classes that are contained in a .jar file. How can I do that?
For example, I will use a class named Classname that is contained in a .jar file org.example.jar. Say the source code to which I will use Classname is mysource.java.
SOLUTION:
Do this:
import org.example.Classname;
public class mysource{
public static void main(String[] argv){
….
}
}
When compiling your code, need to do this:
In unix-like OS:
javac -cp ".:org.example.jar" mysource.java
When running the code, need to do this:
java -cp ".:org.example.jar" mysource
the option -cp is used to specify classpath. it is followed by the directory of the .jar files to be used in compiling/running a class.
the pathname ".:org.example.jar" is used to specify that the compilation/execution will need org.example.jar file in the directory minibase_eclipse and packages in the current directory, i.e. the symbol `.' Finally, to separate multiple classpath, the symbol `:' is used.
.jar can be viewed as a collection of java classes
#java #jar