NanowarOfSteel / HelloWorld

1.25k stars 62 forks source link

HelloWorld.java

thumbnail

Welcome to the Nanowar Of Steel's first GitHub project, and first ever example of a source-code song. The aim of this project is to support and enforce "True metal programming", that basically means: "write code so badly that only you can sing it".

You can find the source-code video in the link below: https://www.youtube.com/watch?v=yup8gIXxWDU

Here is the Nanowar Of Steel official website.

Here is the Nanowar Of Steel Youtube Channel.

Some important notes:

  1. Beware of Joey De Caio, if set to null It may suddenly cancel the program execution with a NullPointerException.
  2. metal variable is very important! If it is set to "false", it may trigger the Joey De Caio variable mentioned above.
  3. tizio and foo are very close friends.
  4. pippo isn't tizio's friend, but he's been flirting with foo.
  5. In the comments "manage" should be renamed as "handle" (we all wish "manage" was the right word, but it's not).
  6. The constructor argument is used to control the number of executions. However, It may be completely useless if the execution is cancelled by Joey De Caio.
  7. "Write once, run anywhere" means literally anywhere (e.g. Hellfest).
  8. songRefrain() identifies itself as a void method.

Below you will find a super detailed guide to the program, of course written with ChatGPT.


HelloWorldMainLauncherClass

This project contains a simple Java application that demonstrates the basic usage of loops, conditionals, and handling null-pointer exceptions. The main functionality includes printing "Hello World!" multiple times based on a user-defined parameter and intentionally introducing a bug to highlight error handling.

Overview

The HelloWorldMainLauncherClass performs the following tasks:

  1. Initialization: Accepts an integer parameter (foo) that controls how many times "Hello World!" will be printed.
  2. Song Refrain: Prints "Hello World!" twice—first using a for loop and then using a while loop.
  3. Exception Handling: Contains a method falseMethod() that introduces a NullPointerException by calling length() on a null string to simulate error handling.

Features

Prerequisites

Installation

To set up and run this project locally, follow these steps:

  1. Clone the repository:

    git clone https://github.com/NanowarOfSteel/HelloWorld.git
  2. Navigate into the project sources directory:

    cd HelloWorld/src/main/java
  3. Compile the Java file:

    javac it/nanowar/ofsteel/helloworld/HelloWorldMainLauncherClass.java
  4. Run the program:

    javac it/nanowar/ofsteel/helloworld/HelloWorldMainLauncherClass.java

Alternatively, with Java 11 you can compile and run the project in one single step:

  1. Compile and run the Java file:
    java it/nanowar/ofsteel/helloworld/HelloWorldMainLauncherClass.java

Usage

Once you run the program, you will see the following output:

Hello World Programmer Start
Hello World!
Hello World!
Hello World!
Hello World!
Program Finished!
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "joeyDeCaio" is null
    at HelloWorldMainLauncherClass.falseMethod(HelloWorldMainLauncherClass.java:35)
    at HelloWorldMainLauncherClass.main(HelloWorldMainLauncherClass.java:27)

Configuration

The number of times "Hello World!" is printed can be controlled by changing the parameter passed to the constructor of HelloWorldMainLauncherClass. For example:

HelloWorldMainLauncherClass tizio = new HelloWorldMainLauncherClass(3);

This will print "Hello World!" three times.

Code Explanation

  1. Constructor: The constructor takes an integer foo that determines how many times the message will be printed.

    public HelloWorldMainLauncherClass(int foo) {
       super();
       this.foo = foo;
    }
  2. songRefrain() Method: The method contains two loops—for and while—both of which print "Hello World!" the number of times specified by foo.

    public void songRefrain() {
       for (int i = 0; i < foo; i++) {
           System.out.println("Hello World!");
       }
    
       int pippo = 0;
       while (pippo < foo) {
           System.out.println("Hello World!");
           pippo++;
       }
    }
  3. falseMethod() Method: This method deliberately introduces a bug by attempting to call length() on a null string.

    public void falseMethod() {
       boolean metal = false;
       if (metal == false) {
           String joeyDeCaio = null;
           joeyDeCaio.length();
       }
    }

    Notes:

JBang Guide

This repository contains a jbang catalog that allows you to run the HelloWorld program with the command jbang hello@nanowarofsteel/hello.

Table of Contents

JBang Prerequisites

To use the jbang catalog, you need to have jbang installed. You can install jbang here.

JBang Usage

To run the HelloWorld program with jbang, you can use the following command directly without cloning the repository:

jbang hello@nanowarofsteel/HelloWorld

If you have cloned the repository, you can run the program with the following command:

jbang hello

Docker Guide

This repository contains a Dockerfile that automates the process of building and running a Java application using Maven. The Docker image is built in two stages: the first stage builds the application with Maven, and the second stage runs the application with a minimal OpenJDK 17 image.

Table of Contents

Prerequisites

Before building and running the Docker image, make sure you have the following installed:

Build and Run Instructions

1. Clone the repository

First, clone the repository containing the project and the Dockerfile to your local machine:

git clone https://github.com/NanowarOfSteel/HelloWorld.git
cd HelloWorld

2. Build the Docker image

You can build the Docker image using the following command. Make sure you're in the root directory of the project, then point out the location of the Dockerfile with the -f option:

docker build -t my-java-app -f src/main/docker/helloworld.dockerfile .

This command will:

  1. Use an official Maven image to build the application.
  2. Package the application into a JAR file.
  3. Use a slim OpenJDK 17 image to run the resulting application (kinda).

Usage

After the build is complete, you can run the Docker container using:

docker run --rm my-java-app

Cleaning Up

If you didn't use the option --rm when instanciating the container, you will have to manually remove the container and image once you're done:

docker rm <container_id>  # Remove the container
docker rmi my-java-app    # Remove the image

Notes

Performance considerations

In order to achieve better performance experience this code live. Due to resource allocation issues there are only limited possibilities:

Further information about upcomming resources can be found here: https://www.nanowar.it/live/

Troubleshooting


Docker compose guide

Prerequisites

Build and Run Instructions

1. Clone the repository

Do as described in Docker Build and Run Instructions

2. Build and Run

With Docker Compose building and running are done with one command (run in the project root):

docker compose up

This command will build and run the project serving it on port 8080.

For further explanations see the comments in file: docker-compose.yaml

By running docker compose up the output will be captured by the execution.

You can pass the execution context to the docker daemon by running

docker compose up -d

Cleaning Up

If you ran docker compose without the -d flag stop the execution by pressing ctrl+c

If you ran docker compose -d you can stop the execution by running

docker compose down

within the directory where the file docker-compose.yaml is located

Run

docker compose rm

to remove the stopped container.

Then do as describe in Cleaning Up for Docker except for the remove the stopped container part

Troubleshooting (only for Mac or Linux)

If you get this error: Error response from daemon: driver failed programming external connectivity on endpoint helloworld-app-1 (1769c92017f2d2da48adc989bd2321855e3152f6a11932c18ed36e7df443f0ae): failed to bind port 0.0.0.0:8080/tcp: Error starting userland proxy: listen tcp4 0.0.0.0:8080: bind: address already in use'

It means the port 8080 is already binded (maybe by the previous docker execution without cleaning up).

To solve it find the process running on port 8080 by running

lsof -i tcp:8080

Note the PID value of the output and kill the process by running

kill <PID>

License

This project is licensed under the IronAvantgarde Publishing & Napalm Records License - see the Napalm Records Website for more details.

Contributing

Feel free to fork this repository, submit issues, or open pull requests if you'd like to improve this code. Contributions are always welcome!

Contacts

Do you want some buggy, personalized and singable code by Nanowar Of Steel? Please contact us here.