Java programming

  1. Creating a non executable jar and using it in application
Creating a non executable jar and using it in application

1. mkdir testjars
2. mkdir testjars/cbadb
3. cd testjars/cbadb
4. touch class1.java
5. touch class2.java
6. code class1.java as follows:
package cbadb;

public class class1
{
public void greet()
{
System.out.println("Class one greeting");
}
}

7. code class2.java as folllows:
package cbadb;

public class class2
{
public void greet()
{
System.out.println("Class two greeting");
}
}

8. compile class1: javac class1.java
9. compile class2: javac class2.java
10. Create jar file of 2 classes in directory above cbadb:
cd ..
jar cf cbadb.jar cbadb/class1.class cbadb/class2.class

11. Verify jar contents: jar tvf cbadb.jar
0 Tue Nov 28 00:31:26 GMT 2017 META-INF/
69 Tue Nov 28 00:31:26 GMT 2017 META-INF/MANIFEST.MF
406 Tue Nov 28 00:30:26 GMT 2017 cbadb/class1.class
406 Tue Nov 28 00:30:34 GMT 2017 cbadb/class2.class

12. create a testing application runner.java as follows:
import cbadb.*;

public class runner
{

public static void main(String[] args)
{
class1 c1 = new class1();
class2 c2 = new class2();

c1.greet();
c2.greet();
}

}

13. compile it as follows:
javac -cp '.:cbadb.jar' runner.java

14. run the testing application as follows:
java -cp '.:cbadb.jar' runner
OUTPUT:
Class one greeting
Class two greeting