Java project with packages & jar file dependency without an IDE & build tool tutorial

Step 1: Create the following folders

1. c:\projects\learn-java is the “project folder“.

2. com\mycompany1\main & com\mycompany1\service are the “packages” under the project.

3. c:\projects\learn-java\bin for the compiled “.class” files.

4. c:\projects\learn-java\target for the packaged project jar file.

5. c:\projects\learn-java\lib for the third-party library jar files.

Step 2: Create the following Java source files, which take an input of numbers separated by comma from an OS console, and outputs a list of odd numbers.

E.g.

Input: 1,2,3,4,5,6
Output: 1,3,5

com/mycompanny1/main/OddNumbersMain.java
com/mycompanny1/service/OddNumbersService.java

Step 3: Compile the source Java files into “c:\projects\learn-java\bin

Step 4: Run the generated class file “com/mycompany1/main/OddNumbersMain.class” in the “c:\projects\learn-java\bin”

Step 5: Build a jar file.

Step 6: Run the jar file in the “c:\projects\learn-java\target” folder.

Step 7: Go to the Maven repository “http://repo1.maven.org/maven2/” and then go to the subfolder “http://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.4/” and download “commons-lang3-3.4.jar” into the “c:\projects\learn-java\lib” folder.

Step 8: Change “com/mycompanny1/service/OddNumbersService.java” to use the “StringUtils” class from the previously downloaded third-party library “commons-lang3-3.4.jar“.

Step 9: Compile with “-cp” option.

Step 10: Build the jar file again as shown earlier, and run the application.

Step 11: Go to the Maven repository “http://repo1.maven.org/maven2/” and then go to the subfolder “http://repo1.maven.org/maven2/junit/junit/4.12/” and download “junit-4.12.jar” into the “c:\projects\learn-java\lib” folder. Also download “hamcrest-core-1.3.jar” from “http://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/”.

Step 12: Write a unit test.

com/mycompanny1/service/OddNumbersServiceTest.java

Step 13: Compile with “lib\junit-4.12.jar”.

Step 14: Create the “jar” file again with the unit test class “com/mycompany1/service/OddNumberServiceTest.class“.

Step 15: Run the unit test.

You could set the “CLASSPATH” environment variable as shown below

and then run:


800+ Java Q&As & tutorials

Top