YOUR FIRST JAVA PROJECT

java-logo


The goal is to create the first Java Project
, using Visual Studio Code, to create what will be the structure of the programs that we will write as we go along. We will call the project CourseJava. I illustrate with a video the steps to be taken in the editor.

I bring you a picture of the folder structure that the editor creates when a Java project is added.

Struttura
  • src source files, i.e., code files that we write and having the extension .java
  • bin there are the files class, which are the files that the Java compiler has transformed into bytecode
  • Finally, the folder lib contains any imported libraries. In this case there are none

Now we are going to create the main package, all the other packages of the various programs that we will write as we go along will be contained in it.

The package it.corso.java will be the main one from which all others will be derived. Similarly to the creation of the Hello.java we create a file CorsoJava.java which will contain the main from which we will launch all the applications we write. We delete the files we don’t need (just delete the .java file and automatically the editor also deletes the corresponding .class).

Struttura

RUN A JAVA FILE

Within the class CorsoJava we write in output a simple string, “Ciao!” . Let’s see how to run the file in the editor.

WHAT JAR FILES ARE AND HOW THEY ARE CREATED

A software can consist of many .class, depending on complexity. Installation, in these cases can be very complex. A file JAR
is a compressed file that contains within it the compiled files and any additional libraries. It is a ZIP file containing the application files. Some advantages of using JAR:

  • Compression: the software and additional libraries are enclosed in a single compressed file, decreasing the overall size of the program.
  • Signature: the JAR contains a META-INF which contains within it the file MANIFEST.MF. This file contains a set of basic program information (Class-Path, Main-Class, …).
  • Portability: the same JAR can run on different operating systems that contain the JVM required.

THE MANIFEST

The file MANIFEST.MF contains a set of basic program information (Class-Path, Main-Class etc.). Other information can also be added to this file. Some attributes to know about the MANIFEST file.

  • Manifest-Version: defines the version of the Manifest file (e.g. 1.0, or 2.1.3, etc.).
  • Created-By: defines the version and vendor of the JDK used to create the file.
  • Class-Path: defines the relative path of the required libraries (which must be present in the JAR) for running the software.
  • Main-Class: if present, indicates the class that contains the main method, which is essential for software execution. This attribute is used for stand-alone applications (i.e., run on your pc) that can be run via the command ” java -jar x.jar“.

Note: In Visual studio code we will use the following command java -jar –enable-preview CorsoJava.jar to execute a JAR

CREATING A JAR FILE IN VISUAL STUDIO CODE

We first install the following extension that allows us to look inside the JAR.

Estensione

CREATION OF A JAR FILE

EXPLORE THE JAR

LINKS TO PREVIOUS POSTS

THE JAVA LANGUAGE

LINK TO CODE ON GITHUB

GITHUB