Applications Basics

De Marijan Stajic | Wiki
Aller à la navigation Aller à la recherche

Java Deployment


Java banner.png


Java is one of the most popular languages used for building desktop and mobile applications. It is free, open-source, and has a huge community that follows and supports the project.

Initially, Java was named Oak, inspired by an tree that stood outside the office of the company that created Java. Due to a trademark conflict, the name was changed to Java, which is slang for coffee in American English. Coffee was chosen because it is the favorite drink of many developers.

Many applications may not be using the latest version of Java. Indeed, there are several versions of Java, and it is not uncommon to see software that is stuck on an old version.

Install (Linux)

You can find the latest version of Java on the Oracle website. To install it on Linux using the Shell, follow these instructions:

1. First, use the wget command followed by the link to the x64 Compressed Archive :

marijan$ wget https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.tar.gz

2. Then, unarchive the file in the directory where you want to install Java :

marijan$ tar -xzf jdk-22_linux-x64_bin.tar.gz -C [destination_directory]

3. You can verify if Java is correctly installed by using the following command :

marijan$ jdk-22.0.1/bin/java -version

java version "22.0.1" 2024-04-16
Java(TM) SE Runtime Environment (build 22.0.1+8-16)
Java HotSpot(TM) 64-Bit Server VM (build 22.0.1+8-16, mixed mode, sharing)

4. Finally, you just need to define the Java environment path to be able to run commands :

marijan$ export PATH=$PATH:/[destination_directory]/jdk-22/bin

Java Development Kit (JDK)

The Java Development Kit (JDK) is a set of tools that help to develop, build, and run Java applications on a system. When we install Java, we are not just installing the Java runtime environment, but the entire kit, which includes the necessary tools and libraries to develop and execute Java applications.

  • Develop : The JDK includes tools for finding errors and debugging (jdb). It also provides a tool for generating code documentation (javadoc).
  • Build : It enables code compilation (javac) and allows you to archive code (jar).
  • Run : To run a Java application on any system, you use the Java Runtime Environment (JRE). For loading the application, you use the basic tool (java).

These tools are available in the following path :

marijan# ls jdk-22.0.1/bin

jar  jarsigner  java  javac  javadoc  javap  jcmd  jconsole  jdb  jdeprscan  jdeps  jfr  jhsdb  jimage  jinfo  jlink  jmap  jmod  jpackage  jps  jrunscript  jshell  jstack  jstat  jstatd  jwebserver  keytool  rmiregistry  serialver

Build and Packaging

To run Java code on any system, you need to compile the code first :

marijan$ cat MyClass.java

public class MyClass {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

marijan$ javac MyClass.java
marijan$ java MyClass

Hello, World!

Then, if you have an application with multiple files and dependencies, you can create an archive using Java Archive (JAR) :

Application Basics Java JAR.png


marijan$ jar cf MyApp.jar MyClass.class Services.Class ...

To run the archive, you must specify the following argument :

marijan$ java -jar MyApp.jar

Whether you need to package a website with files like images or HTML, you can create a Web Archive (WAR) :

Application Basics Java WAR correction.png


marijan$ jar cf MyApp.war MyClass.class Services.Class ...

To run the archive, you must specify the following argument :

marijan$ java -jar MyApp.war

Automation tools (Apache ANT)

To save time for a developer during program coding, there are tools created to automate the build process such as compilation, documentation generation, and archiving.

One of the most widely used tools for Java development is Apache ANT. To set up the tool, you need to create a build.xml file that specifies the tasks you want to automate.

marijan$ cat build.xml

<?xml version="1.0"?>
<project name="Ant" default="main" basedir=".">
    <!-- Compiles the java code -->
    <target name="compile">
        <javac srcdir="/app/src" destdir="/app/build">
        </java>
    </target>
    <!-- Creates Javadoc -->
    <target name="docs" depends="compile">
        <javadoc packagenames="src" sourcepath="/app/src" destdir="/app/docs"
        ..
    </target>
    <!-- Creates the deployable jar file -->
    <target name="jar" dpeends="compile">
        <jar basedir="/app/build" destfile="/app/dist/MyClass.jar" >
        ..
    </target
</project>

To execute the tools, you just have to run the following command :

marijan$ ant 

This command will execute the entire script. However, if you need, you can specify only one part of the script :

marijan$ ant compile 

Here, it will only run the compilation (javac) because the name of the target in the script is compile.

NodeJS Deployment


Nodejs Banner 2.png


Previously, websites were built only with HTML and CSS, resulting in static and unappealing pages. Nowadays, JavaScript enables the creation of intelligent and interactive websites and applications. JavaScript offers multiple frameworks such as ReactJS, VueJS, and more. All of these run on the client or user session and are independent of the application server.

In the past, server-side code was primarily written in languages like Java, Python, or Ruby. Today, JavaScript is also implemented on the server side with NodeJS.

NodeJS is open-source, free, and compatible with many operating systems. It is one of the most used languages for server-side development.

Install (Linux)

You can find the latest version of NodeJS on the Node.js website. To install it on Linux using the Shell, follow these instructions:

1. If you use Debian, you can download it simply by using apt. However, the package is also available on GitHub.

marijan$ apt install nodejs

2. To verify if it's correctly installed, you can run the following commands to check the versions of NodeJS :

marijan$ node -v
v12.22.12

3. Finally, to execute a .js file with NodeJS, you just have to run :

marijan$ cat add.js
// Returns addition of two numbers
let add = function (a, b) {
    return a+b;
};

const a = 10, b = 5;

console.log("Addition :"+ add(a,b))

marijan$ node add.js
Addition : 15

Package Manager (NPM)

Node.js has a large library of packages developed by the core team and the community. These packages can serve various purposes, such as creating web servers, interacting with databases, or implementing security features. All of them are accessible from a public repository, which is the NPM website. Indeed, there are thousands of packages available for use in Node.js, covering a wide range of functionalities. Additionally, you can share your own projects with others by publishing them as packages on the NPM registry.

You can integrate NPM directly into your system by installing the package on your system :

marijan$ apt install npm

Once installed, you can find shared packages on the NPM public repository using the following command :

marijan$ npm search file

NAME                      | DESCRIPTION          | AUTHOR          | DATE       | VERSION  | KEYWORDS
file                      | Higher level path…   | =aconbere       | 2014-02-21 | 0.2.2    |
File                      | HTML5 FileAPI…       | =coolaj86 =narf | 2014-10-24 | 0.10.2   | html5 jsdom file-api file
file-entry-cache          | Super simple cache…  | =jaredwray      | 2023-12-18 | 8.0.0    | file cache task cache files file cache key par key value cache
eslint-plugin-n           | Additional ESLint's… | =weiran.zsd…    | 2024-05-14 | 17.7.0   | eslint eslintplugin eslint-plugin node nodejs ecmascript shebang file path import require
fs-extra                  | fs-extra contains…   | =jprichardson…  | 2023-11-28 | 11.2.0   | fs file file system copy directory extra mkdirp mkdir mkdirs recursive json read write extra delete remove touch create text output move promise

It displays all packages containing the name you specified, followed by the description, author and more.

To install a package, simply execute the following command:

marijan$ npm install file

+ file@0.2.2
added 1 package from 1 contributor and audited 1 package in 1.072s

When you install a package from NPM, it will be automatically located under /usr/share/lib.

Application Bascics NodeJS Architecture.png.png


  • LICENSE : This file contains the license of the package.
  • README.md : This provides information about the package as provided by the developer.
  • package.json : This file contains metadata for the package including version, author, and repository details.
  • lib : This directory contains the package's code.

To establish a dependency on a package, you need to define the requirement within your code :

marijan$ cat app.js

..
var file = require("file")
..

Node.js will automatically search in the directory where the app.js is located to see if the package is available. If it's not found there, Node.js will search the entire system to locate it.

You need to specify the dependencies along with their versions in the package.json file of your application. This ensures that Node.js knows exactly which packages and versions your application relies on.

Python Deployment


Python Banner2.png

Python is a free, open-source, cross-platform compatible, and interpreted language. It's important to note that there are two main versions of Python available: Python 2 and Python 3. Python 2 was initially released in 2000 and reached its end of life in 2010. Python 3 is the current primary version, used extensively today. If your program is still using Python 2, it's essential to upgrade to Python 3 to leverage the latest features and ensure compatibility with modern libraries and tools.

Install (Linux)

1. If you use Debian, you can download it simply by using apt. If you write Python without specifying a version (i.e., Python 2 or Python 3), it will install the version that is set as the default on your system or the latest version available from the package manager or repository you're using.

marijan$ apt install python

2. To verify if it's correctly installed, you can run the following commands to check the versions of Python:

marijan$ python3.9 -v
Python 3.9.2

3. Finally, to execute a .py file with Python, you just have to run :

marijan$ cat main.py
def print_message():
    print("Hello World")

if __name__ == '__main__':
    print_message()

marijan$ python3.9 main.py
Hello World

Python Package Manager (PIP)

As its name suggests, PIP stands for Python Package Manager. There are two different versions of PIP, one for Python 2 and one for Python 3.

1. To install it, there are two separate packages :

marijan$ apt install pip
..

marijan$ apt install pip2
..

2. To verify if it's correctly installed, you can run the following command :

marijan$ pip2 -V

marijan$ pip3 -V

3. Then, to install a package, you just have to execute the following command with the name of the package:

marijan$ pip install flask

4. You can find the localisation and other informations about the packet by running the following command :

marijan$ pip show flask

Name: flask
Version: 3.0.3
Summary: A simple framework for building complex web applications.
Home-page: None
Author: None
Author-email: None
License: None
Location: /usr/local/lib/python3.9/dist-packages
Requires: Werkzeug, blinker, importlib-metadata, Jinja2, itsdangerous, click

Here is the structure about how Python is organise :

Applications Basics PIP Structure.png


5. To include a package in an application using Python, you need to import the necessary modules from the installed package :

marijan$ cat main.py

from flask import Flask
..

6. If you have a text file listing the requirements for the software, you can run a pip command with the -r parameter followed by the path to the .txt file to install all of them.

marijan$ cat requirements.txt

Flask
Jinja2
MarkupSafe
Werkzeug
requests
gunicorn

marijan$ pip install -r requirements.txt
</pre