Sunday, February 4, 2018

Getting Started with Python's Virtualenv

This blog will teach you how to get started with virtualenv in Python. First install virtualenv if you have not installed it.
pip install virtualenv
Create a new virtualenv directory called hello.
virtualenv hello
The above command will a standard Python installation.
hello/
├── bin
├── include
├── lib
└── pip-selfcheck.json

To activate the virtualenv we just created, we need to source the activate script in the bin directory. Once we are in the virtualenv environment, we can install any Python libraries we want and the libraries will be installed in the hello virtualenv we just created, e.g.
cd hello
source bin/activate
pip install requests
To quit the virtualenv, simply type
deactivate
Let's now create a simple script that uses the new request module we just installed.
test.py
import requests

if __name__ == '__main__':
    r = requests.get('http://www.google.com')
    print(r.status_code)
To run the script in a virtualenv.
cd hello
source bin/activate
python test.py
deactivate

Friday, January 12, 2018

Getting Started with Java Module System

In this blog, I'm going to teach you how to get started with Java module system.
build.gradle
apply plugin: "java"

sourceCompatibility = 1.9

Now let's add module-info.java and put it in the root project.
module jigsaw {
}

Your project structure should look like this.
|-- build.gradle
`-- src
    `-- main
        `-- java
            |-- jigsaw
            |   `-- Main.java
            `-- module-info.java
Let's now build the JAR using Gradle.
gradle

Type the following command to run the application.
java --module-path build/libs -m jigsaw/jigsaw.Main

We can still run the JAR with a module system using a standard classpath.
java -cp build/libs/jigsaw.jar jigsaw.Main

In order to make it easy to distribute the application and to reduce the Java runtime size, we can create a custom Java runtime image using jlink.
jlink --module-path "build/libs;$JAVA_HOME/jmods" --add-modules jigsaw --output jigsaw

The command above creates a Java runtime image in the directory called jigsaw.
jigsaw          
|-- bin    
|-- conf   
|-- legal  
|-- lib    
`-- release

By using jlink, the jigsaw module that we just created will be part of the Java runtime image. You will not see the jigsaw JAR gets in the Java runtime image. If you run this command below, you will see the jigsaw module is now part of the image.
$ cd jigsaw
$ bin/java --list-modules
java.base@9.0.1
jigsaw

We can now run the jigsaw module by running the Java executable inside the Java runtime image without having to specify -m option.
$ cd jigsaw
$ bin/java -m jigsaw/jigsaw.Main