Reputation: 581
I created one java file then i converted into jar now i want to use it in another java class how to import that jar and how to compile and execute.
Upvotes: 1
Views: 16339
Reputation: 421020
Given a "library" class pkg/LibClass.java
:
package pkg;
public class LibClass {
public static void sayHello() {
System.out.println("Hello world");
}
}
Compile and create a jar file:
$ javac pkg/LibClass.java
$ jar cvf lib.jar pkg/LibClass.class
added manifest
adding: pkg/LibClass.class(in = 404) (out= 284)(deflated 29%)
(creates a lib.jar
in the current directory)
Create an application that uses the library:
import pkg.LibClass;
public class MainClass {
public static void main(String[] args) {
LibClass.sayHello();
}
}
Compile and run the application using lib.jar
:
$ javac -cp lib.jar MainClass.java
$ java -cp lib.jar:. MainClass
Upvotes: 5
Reputation: 52779
Set the jar in your class path or pass as an argument and compile or execute -
Compile - javac -classpath your.jar other.java
Execute - java -classpath your.jar other
Upvotes: 0
Reputation: 240898
You need to make that jar file available in classpath at compile time and runtime when that another java file is being compiled/executing
Upvotes: 0