Pages

Friday, August 19, 2016

Java 8 : How to create executable JAR without IDE with packages, command-line :: explained

  Following post is the extension to of the Executable-One project. In the previous post was explained how to create simple executable jar file. 
  The goal of this post is to show how to deal with more complicated package structure. 
We create MagicService which provides us getMessage() method with the String output. 
Let's open the command line and create new folder SERVICE with file MagicService.java: 
command
$mkdir src/main/java/com/exec/one/service
$vi src/main/java/com/exec/one/service/MagicService.java
  The newly created MagicService may look in following example: 
1 package com.exec.one.service;                                                                                                                                                          
2                                                                                                                                                                                        
3 public class MagicService {                                                                                                                                                            
4                                                                                                                                                                                        
5     private final String message;                                                                                                                                                      
6     public MagicService(){                                                                                                                                                             
7         this.message = "Magic Message";                                                                                                                                                
8     }                                                                                                                                                                                  
9                                                                                                                                                                                        
10     public String getMessage(){                                                                                                                                                        
11         return message;                                                                                                                                                                
12     }                                                                                                                                                                                  
13                                                                                                                                                                                        
14 }
  The MagicService is obviously located on the different place in the package structure then the Main class. Now we go back to the Main class and import the newly created MagicService
  After the import and service instantiation the Main class will receive the access to the getMessage() method. 
The Main class will change: 
1 package com.exec.one;                                                                                                                                                                  
2                                                                                                                                                                                        
3 import com.exec.one.service.MagicService;                                                                                                                                              
4                                                                                                                                                                                        
5 public class Main {                                                                                                                                                                    
6     public static void main(String[] args){                                                                                                                                            
7         System.out.println("Main Class Start");                                                                                                                                        
8         MagicService service = new MagicService();                                                                                                                                     
9         System.out.println("MESSAGE : " + service.getMessage());                                                                                                                       
10     }                                                                                                                                                                                  
11 } 
  Now we have reached the point where the code is ready to compile. 
Let's go back to the command line and go into root folder of the Executable-One project. 
  The first step will be to compile/re-compile Executable-One project into the OUT folder. For those purposes we need add the location of newly created class MagicService.java. 
javac -cp ./src/main/java ./src/main/java/com/exec/one/*.java ./src/main/java/com/exec/one/**/*.java -d ./out/
  The second step is to create executable JAR file from compiled classes. We don't need to make any changes to the command because we have not changes JAR file logic. It means MANIFEST.FM file stays without any changes: 
1 Manifest-Version: 1.0                                                                                                                                                                  
2 Class-Path: .                                                                                                                                                                          
3 Main-Class: com.exec.one.Main 

And we  are free to use command from previous blog post:
jar cvfm ExecutableOne.jar ./src/main/resources/META-INF/MANIFEST.MF -C ./out/ .
end
We are done with the JAR file. Now we can execute it in the command line with following output:
$java -jar ExecutableOne.jar 
output: 
Main Class Start
MESSAGE : Magic Message

  We have done enough changes for the upcoming blogpost focused on creating fatJAR file with libraries and again without ANY IDE or building tools such as Gradle, Maven or Ant, all is pure command line and Java JDK

NOTE: details to javac, jar tools were explained in the PREVIOUS POST.

No comments: