From 0bc6c9ec584f442e06c39b179966d94c8e24a2a2 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Thu, 11 Nov 2021 19:54:04 +0100 Subject: [PATCH 01/68] Update the IntelliJ basic Java project package structure --- .../.idea/runConfigurations/Main.xml | 4 ++-- java/basic_java_project_intellij/README.md | 2 +- .../java/intellij/{java => general}/projects/basic/Main.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) rename java/basic_java_project_intellij/src/nl/mauritssilvis/challenges/java/intellij/{java => general}/projects/basic/Main.java (73%) diff --git a/java/basic_java_project_intellij/.idea/runConfigurations/Main.xml b/java/basic_java_project_intellij/.idea/runConfigurations/Main.xml index 0b32f9f..5995127 100644 --- a/java/basic_java_project_intellij/.idea/runConfigurations/Main.xml +++ b/java/basic_java_project_intellij/.idea/runConfigurations/Main.xml @@ -1,10 +1,10 @@ - ``` -##### 1.2.1.2 Copyright +##### Copyright Automatic copyright messages including the current year and an [SPDX license identifier](https://spdx.dev/ids/) for the [GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.en.html) are configured in the file [GNU_GPL_v3.xml](.idea/copyright/GNU_GPL_v3.xml): @@ -64,12 +64,12 @@ Automatic copyright messages including the current year and an [SPDX license ide ``` -##### 1.2.1.3 Inspection profiles +##### Inspection profiles To learn about possible code improvements and optimizations, a code inspection profile in which all Java-17-compatible IntelliJ IDEA inspections are turned on is provided in [All.xml](.idea/inspectionProfiles/All.xml). A profile in which only the default inspections are selected is stored in [Defaults.xml](.idea/inspectionProfiles/Default.xml). -##### 1.2.1.4 Run configurations +##### Run configurations This project comes with one run configuration for executing the `main` method of the `Main` class. diff --git a/java/basic_maven_project_intellij/README.md b/java/basic_maven_project_intellij/README.md index e74a83e..6c12fa2 100644 --- a/java/basic_maven_project_intellij/README.md +++ b/java/basic_maven_project_intellij/README.md @@ -38,7 +38,7 @@ Additional details regarding the IntelliJ IDEA run configurations of the current #### 1.2.1 Configuration -##### 1.2.1.1 Run configurations +##### Run configurations This project comes with three run configurations. This first run configuration is called `Main` and directly executes the `main` method of the `Main` class. @@ -53,7 +53,7 @@ The Maven configuration of the current project is detailed in what follows. #### 1.3.1 Configuration -##### 1.3.1.1 Project coordinates +##### Project coordinates In the project object model of the current project, [pom.xml](pom.xml), the so-called project coordinates are set to: @@ -65,7 +65,7 @@ In the project object model of the current project, [pom.xml](pom.xml), the so-c ``` -##### 1.3.1.2 Encoding +##### Encoding The encoding of the source files is set to UTF-8 using: @@ -77,7 +77,7 @@ The encoding of the source files is set to UTF-8 using: ``` -##### 1.3.1.3 Maven compiler plugin +##### Maven compiler plugin Finally, the [Maven compiler plugin](https://maven.apache.org/plugins/maven-compiler-plugin/) is configured as: diff --git a/java/executable_jar_maven_intellij/README.md b/java/executable_jar_maven_intellij/README.md index d80ced5..5c833b6 100644 --- a/java/executable_jar_maven_intellij/README.md +++ b/java/executable_jar_maven_intellij/README.md @@ -38,7 +38,7 @@ Additional details regarding the IntelliJ IDEA run configurations of the current #### 1.2.1 Configuration -##### 1.2.1.1 Run configurations +##### Run configurations This project comes with three run configurations. This first run configuration is called `Main` and directly executes the `main` method of the `Main` class. @@ -53,7 +53,7 @@ Additional details regarding the Maven configuration of the current project are #### 1.3.1 Configuration -##### 1.3.1.1 Maven JAR plugin +##### Maven JAR plugin To ensure that the JAR created by Maven is executable, the project object model of the current project, [pom.xml](pom.xml), contains the following configuration for the [Maven JAR plugin](https://maven.apache.org/plugins/maven-jar-plugin/): From a6fb63b6a0ddb7a332c4be78e9f3effcdca4fe9b Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Thu, 11 Nov 2021 20:05:27 +0100 Subject: [PATCH 04/68] Extend the description of the status of Java projects --- java/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/java/README.md b/java/README.md index ecc3414..e66e6f9 100644 --- a/java/README.md +++ b/java/README.md @@ -11,17 +11,17 @@ I also provide some [background information](#2-background) on [Java](#21-java) ## 1. Status -Currently, Java solutions are available to challenges in the following categories. +Currently, Java solutions are available to the following challenges, categorized according to the primary tool used: the [command line](#11-command-line-projects) or [IntelliJ IDEA](#12-intellij-idea-projects). -### 1.1 Command line +### 1.1 Command-line projects -I solved the challenging in this section using the command line. +I solved the challenges in this section using the command line. #### 1.1.1 General ##### Projects -### 1.2 IntelliJ IDEA +### 1.2 IntelliJ IDEA projects I solved the following challenges using [IntelliJ IDEA](#22-intellij-idea). From e5e803c7aa695cd5516913e3387e94eadd3496b0 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Thu, 11 Nov 2021 20:05:27 +0100 Subject: [PATCH 05/68] Introduce a (placeholder) section on Bash --- java/README.md | 12 +++++++----- java/basic_java_project_intellij/README.md | 2 +- java/basic_maven_project_intellij/README.md | 4 ++-- java/executable_jar_maven_intellij/README.md | 4 ++-- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/java/README.md b/java/README.md index e66e6f9..3fd461e 100644 --- a/java/README.md +++ b/java/README.md @@ -7,11 +7,11 @@ With this part of the [Challenges](https://github.com/mauritssilvis/challenges) project, I provide solutions to common programming challenges in Java. Under [Status](#1-status), I give an overview of the challenges that I solved using Java so far. -I also provide some [background information](#2-background) on [Java](#21-java) and the tools I used, namely, [IntelliJ IDEA](#22-intellij-idea) and [Maven](#23-maven). +I also provide some [background information](#2-background) on [Java](#21-java) and the tools I used, namely, [Bash](#22-bash), [IntelliJ IDEA](#23-intellij-idea) and [Maven](#24-maven). ## 1. Status -Currently, Java solutions are available to the following challenges, categorized according to the primary tool used: the [command line](#11-command-line-projects) or [IntelliJ IDEA](#12-intellij-idea-projects). +Currently, Java solutions are available to the following challenges, categorized according to the tools I used. ### 1.1 Command-line projects @@ -23,7 +23,7 @@ I solved the challenges in this section using the command line. ### 1.2 IntelliJ IDEA projects -I solved the following challenges using [IntelliJ IDEA](#22-intellij-idea). +I solved the following challenges using [IntelliJ IDEA](#23-intellij-idea). #### 1.2.1 General @@ -43,7 +43,7 @@ I solved the following challenges using [IntelliJ IDEA](#22-intellij-idea). ## 2. Background -In this section, I provide some background information on [Java](#21-java) and the tools I used to solve the above-mentioned programming challenges, namely, [IntelliJ IDEA](#22-intellij-idea) and [Maven](#23-maven). +In this section, I provide some background information on [Java](#21-java) and the tools I used to solve the above-mentioned programming challenges, namely, [Bash](#22-bash), [IntelliJ IDEA](#23-intellij-idea) and [Maven](#24-maven). ### 2.1 Java @@ -56,7 +56,9 @@ This bytecode can then be executed on a platform of choice by a platform-specifi To be up-to-date with recent developments, most of the above-mentioned projects are configured to make use of the latest long-term support (LTS) version of Java (currently Java 17). To compile and build these projects, the [Java Development Kit 17](https://jdk.java.net/17/) is required. -### 2.2 IntelliJ IDEA +### 2.2 Bash + +### 2.3 IntelliJ IDEA [IntelliJ IDEA](https://www.jetbrains.com/idea/) is an integrated development environment (IDE) developed by [JetBrains](https://www.jetbrains.com/). This IDE supports Java and several other Java virtual machine languages, and can be extended with plugins to retrieve support for other programming languages. diff --git a/java/basic_java_project_intellij/README.md b/java/basic_java_project_intellij/README.md index cebb664..731b952 100644 --- a/java/basic_java_project_intellij/README.md +++ b/java/basic_java_project_intellij/README.md @@ -28,7 +28,7 @@ public class Main { ### 1.2 IntelliJ IDEA -This project is set up using [IntelliJ IDEA](https://www.jetbrains.com/idea/) (see, e.g., the [IntelliJ IDEA section](../#22-intellij-idea) of [Challenges > Java](..)). +This project is set up using [IntelliJ IDEA](https://www.jetbrains.com/idea/) (see, e.g., the [IntelliJ IDEA section](../#23-intellij-idea) of [Challenges > Java](..)). The used IntelliJ IDEA configuration is described in what follows. diff --git a/java/basic_maven_project_intellij/README.md b/java/basic_maven_project_intellij/README.md index 6c12fa2..ba4cdad 100644 --- a/java/basic_maven_project_intellij/README.md +++ b/java/basic_maven_project_intellij/README.md @@ -31,7 +31,7 @@ public class Main { ### 1.2 IntelliJ IDEA -This project is set up using [IntelliJ IDEA](https://www.jetbrains.com/idea/) (see, e.g., the [IntelliJ IDEA section](../#22-intellij-idea) of [Challenges > Java](..)). +This project is set up using [IntelliJ IDEA](https://www.jetbrains.com/idea/) (see, e.g., the [IntelliJ IDEA section](../#23-intellij-idea) of [Challenges > Java](..)). The basic configuration is described in the [IntelliJ IDEA section](../basic_java_project_intellij#12-intellij-idea) of [Setting up a basic Java project using IntelliJ IDEA](../basic_java_project_intellij). Additional details regarding the IntelliJ IDEA run configurations of the current project are given in what follows. @@ -47,7 +47,7 @@ Note that execution of the JAR will [fail](#24-maven-jar-plugin-issues). ### 1.3 Maven -This Java project is managed and built using [Maven](https://maven.apache.org/) (see, e.g., the [Maven section](../#23-maven) of [Challenges > Java](..)). +This Java project is managed and built using [Maven](https://maven.apache.org/) (see, e.g., the [Maven section](../#24-maven) of [Challenges > Java](..)). The Maven configuration of the current project is detailed in what follows. diff --git a/java/executable_jar_maven_intellij/README.md b/java/executable_jar_maven_intellij/README.md index 5c833b6..f424e05 100644 --- a/java/executable_jar_maven_intellij/README.md +++ b/java/executable_jar_maven_intellij/README.md @@ -31,7 +31,7 @@ public class Main { ### 1.2 IntelliJ IDEA -This project is set up using [IntelliJ IDEA](https://www.jetbrains.com/idea/) (see, e.g., the [IntelliJ IDEA section](../#22-intellij-idea) of [Challenges > Java](..)). +This project is set up using [IntelliJ IDEA](https://www.jetbrains.com/idea/) (see, e.g., the [IntelliJ IDEA section](../#23-intellij-idea) of [Challenges > Java](..)). The basic configuration is described in the [IntelliJ IDEA section](../basic_java_project_intellij#12-intellij-idea) of [Setting up a basic Java project using IntelliJ IDEA](../basic_java_project_intellij). Additional details regarding the IntelliJ IDEA run configurations of the current project are given in what follows. @@ -46,7 +46,7 @@ The other two run configurations, respectively, create a JAR and execute it. ### 1.3 Maven -This Java project is managed and built using [Maven](https://maven.apache.org/) (see, e.g., the [Maven section](../#23-maven) of [Challenges > Java](..)). +This Java project is managed and built using [Maven](https://maven.apache.org/) (see, e.g., the [Maven section](../#24-maven) of [Challenges > Java](..)). The used configuration extends the [Maven configuration](../basic_maven_project_intellij#13-maven) of [Setting up a basic Maven project using IntelliJ IDEA](../basic_maven_project_intellij). Additional details regarding the Maven configuration of the current project are given in what follows. From 367201f71a4aeabb3c0ed50976f94b16fa8e6995 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Thu, 11 Nov 2021 20:05:27 +0100 Subject: [PATCH 06/68] Introduce a (placeholder) section on Bash (2) --- java/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/README.md b/java/README.md index 3fd461e..095c292 100644 --- a/java/README.md +++ b/java/README.md @@ -67,7 +67,7 @@ IntelliJ IDEA comes with a [detailed online documentation](https://www.jetbrains IntelliJ IDEA makes it easy to configure code styles, automatic copyright notices, code inspection profiles, run configurations and more. The basic IntelliJ IDEA configuration used in the above-mentioned projects is described in the [IntelliJ IDEA section](basic_maven_project_intellij#12-intellij-idea) of [Setting up a basic Maven project using IntelliJ IDEA](basic_maven_project_intellij). -### 2.3 Maven +### 2.4 Maven [Maven](https://maven.apache.org/) is a build tool that can be used to manage and build projects written in Java and other programming languages. The [project object model](https://maven.apache.org/guides/introduction/introduction-to-the-pom.html), a file named `pom.xml`, is central to the use of Maven. From dce2e188cc14fdc1b84e0b78a077bfd5d3ff22f2 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Thu, 11 Nov 2021 21:29:59 +0100 Subject: [PATCH 07/68] Add the basic Java project source file --- .../java/cli/general/projects/basic/Main.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 java/basic_java_project_cli/src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java diff --git a/java/basic_java_project_cli/src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java b/java/basic_java_project_cli/src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java new file mode 100644 index 0000000..c7e0f9f --- /dev/null +++ b/java/basic_java_project_cli/src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java @@ -0,0 +1,12 @@ +/* + * Copyright © 2021 Maurits H. Silvis + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package nl.mauritssilvis.challenges.java.cli.general.projects.basic; + +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} From a4b20cd6b9d425113b5130240ada494565b34f6e Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Thu, 11 Nov 2021 21:30:21 +0100 Subject: [PATCH 08/68] Start setting up the basic Java project documentation --- java/README.md | 4 ++- java/basic_java_project_cli/README.md | 40 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 java/basic_java_project_cli/README.md diff --git a/java/README.md b/java/README.md index 095c292..01a584d 100644 --- a/java/README.md +++ b/java/README.md @@ -15,12 +15,14 @@ Currently, Java solutions are available to the following challenges, categorized ### 1.1 Command-line projects -I solved the challenges in this section using the command line. +I solved the challenges in this section using the [command line](#22-bash). #### 1.1.1 General ##### Projects +* [Setting up a basic Java project using the command line](basic_java_project_cli) + ### 1.2 IntelliJ IDEA projects I solved the following challenges using [IntelliJ IDEA](#23-intellij-idea). diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md new file mode 100644 index 0000000..8518c57 --- /dev/null +++ b/java/basic_java_project_cli/README.md @@ -0,0 +1,40 @@ +# Challenges > Java > Setting up a basic Java project using the command line + +> A basic Java project, set up using the command line + +## Introduction + +With this part of the [Challenges > Java](..) project, I provide the code and settings for a basic Java project, set up using the command line. + +## 1. Background + +### 1.1 Java + +#### 1.1.1 Code + +For demonstrative purposes, the current project contains only a single Java file, [Main.java](src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java), which comprises a Java class with the corresponding name `Main`. +This class contains a `main` method outputting a well-known message: + +```java +package nl.mauritssilvis.challenges.java.cli.general.projects.basic; + +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} +``` + +#### 1.1.2 Folder structure + +#### 1.1.3 Compile + +#### 1.1.4 Run + +## 2. Issues and solutions + +## License + +Copyright © 2021 Maurits H. Silvis + +This source code package is subject to the terms and conditions defined in the GNU General Public License v3.0, which can be found in the file [LICENSE.md](../../LICENSE.md), or later. From 714cea47820aa11ece92115882ca640bc81795b5 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Thu, 11 Nov 2021 21:59:37 +0100 Subject: [PATCH 09/68] Use the acronym JDK in the Java challenges documentation --- java/basic_maven_project_intellij/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/basic_maven_project_intellij/README.md b/java/basic_maven_project_intellij/README.md index ba4cdad..08c2a36 100644 --- a/java/basic_maven_project_intellij/README.md +++ b/java/basic_maven_project_intellij/README.md @@ -100,7 +100,7 @@ Finally, the [Maven compiler plugin](https://maven.apache.org/plugins/maven-comp ``` Here, the latest version of the [Maven compiler plugin](https://maven.apache.org/plugins/maven-compiler-plugin/) (currently 3.8.1) is selected. -Moreover, to be up-to-date with recent developments, the latest long-term support (LTS) version of Java (currently Java 17) is selected, which requires the [Java Development Kit 17](https://jdk.java.net/17/). +Moreover, to be up-to-date with recent developments, the latest long-term support (LTS) version of Java (currently Java 17) is selected, which requires the corresponding version of the Java Development Kit (JDK), [JDK 17](https://jdk.java.net/17/). #### 1.3.2 Build From 00c680e0e755fb6cc65038538359f68243c7846c Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Thu, 11 Nov 2021 22:57:57 +0100 Subject: [PATCH 10/68] Remove dollar signs from shell code blocks --- java/basic_maven_project_intellij/README.md | 2 +- java/executable_jar_maven_intellij/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/java/basic_maven_project_intellij/README.md b/java/basic_maven_project_intellij/README.md index 08c2a36..cab1d5a 100644 --- a/java/basic_maven_project_intellij/README.md +++ b/java/basic_maven_project_intellij/README.md @@ -107,7 +107,7 @@ Moreover, to be up-to-date with recent developments, the latest long-term suppor The project can be built using the command: ```shell -$ mvn clean install +mvn clean install ``` Execution of this command will compile the above-mentioned Java class file, package it as a JAR and make this JAR available in your local Maven repository. diff --git a/java/executable_jar_maven_intellij/README.md b/java/executable_jar_maven_intellij/README.md index f424e05..03d9668 100644 --- a/java/executable_jar_maven_intellij/README.md +++ b/java/executable_jar_maven_intellij/README.md @@ -91,7 +91,7 @@ nl.mauritssilvis.challenges.java.intellij.maven.jars.executable.standalone.Main The executable JAR can be built using the command: ```shell -$ mvn clean install +mvn clean install ``` ## 2. Issues and solutions From ea30ed4c4f242bebb8de253704ec12c7fb2492af Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 12 Nov 2021 00:11:33 +0100 Subject: [PATCH 11/68] Ensure the build directory is always present --- .../mauritssilvis/challenges/java/cli/general/projects/basic/.dir | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 java/basic_java_project_cli/out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/.dir diff --git a/java/basic_java_project_cli/out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/.dir b/java/basic_java_project_cli/out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/.dir new file mode 100644 index 0000000..e69de29 From 887c2dbcab696349396ba4cd4508b2c238640b0f Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 12 Nov 2021 00:13:50 +0100 Subject: [PATCH 12/68] Add a .gitignore file to the basic Java project --- java/basic_java_project_cli/.gitignore | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 java/basic_java_project_cli/.gitignore diff --git a/java/basic_java_project_cli/.gitignore b/java/basic_java_project_cli/.gitignore new file mode 100644 index 0000000..656b445 --- /dev/null +++ b/java/basic_java_project_cli/.gitignore @@ -0,0 +1,26 @@ +# From https://gitignore.io + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* From b9fb489a0876b972d0f4091c95b9764fece6a0aa Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 12 Nov 2021 00:37:26 +0100 Subject: [PATCH 13/68] Document possible Java compilation and execution problems incl. solutions --- java/basic_java_project_cli/problems.md | 236 ++++++++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 java/basic_java_project_cli/problems.md diff --git a/java/basic_java_project_cli/problems.md b/java/basic_java_project_cli/problems.md new file mode 100644 index 0000000..277bec6 --- /dev/null +++ b/java/basic_java_project_cli/problems.md @@ -0,0 +1,236 @@ +# Background + +## Directory structure + +basic_java_project_cli +├─ out +│ └─ ... +├─ src +│ └─ ... +├─ .gitignore +└─ README.md + +## Java compilation + +### Compiling the Java source file using the Java compiler + +``` +cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic +javac Main.java +``` + +( + ``` + javac src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java + ``` +) + +``` +javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +``` + +## Java execution + +### Executing the compiled Java class from the proper folder + +``` +cd out +java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +``` + +``` +cd out +java nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + +``` +java -cp out nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +``` + +``` +java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + +### Directly executing the Java source file using Java from any folder + +``` +cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic +java Main.java +``` + +``` +java src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +``` + +Hello world! + +# Problems + +## Java compilation problems + +### file not found + +Using dots instead of slashes in the Java source file path: + +``` +javac src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java +``` + +``` +error: file not found: src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java +Usage: javac +use --help for a list of possible options +``` + +Use normal paths, not using dots to separate folder names: + +``` +javac src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +``` + +### Class names are only accepted if annotation processing is explicitly requested + +Using a class-name-like path: + +``` +cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic +javac Main +``` + +``` +error: Class names, 'Main', are only accepted if annotation processing is explicitly requested +1 error +``` + +``` +javac src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + +``` +error: Class names, 'src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main', are only accepted if annotation processing is explicitly requested +1 error +``` + +Do not use class-name-like paths. + +### error: invalid flag + +Using a class-name-like path with a .class ending: +/ +Running the Java compiler on a class file: +/ +Any invalid flag: + +( + ``` + javac out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class + ``` + + ``` + error: invalid flag: out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class + Usage: javac + use --help for a list of possible options + ``` +) + +``` +cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic +javac Main.class +``` + +``` +error: invalid flag: Main.class +Usage: javac +use --help for a list of possible options +``` + +Do not run the Java compiler on class files. + +## Java execution problems + +### Problem: Executing the class file including its extension + +``` +cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic +java Main.class +``` + +( + ``` + Error: Could not find or load main class Main.class + Caused by: java.lang.ClassNotFoundException: Main.class + ``` + + ``` + cd out + java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.class + ``` + + ``` + Error: Could not find or load main class nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class + Caused by: java.lang.ClassNotFoundException: nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class + ``` + + ``` + java out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.class + ``` + + ``` + Error: Could not find or load main class out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class + Caused by: java.lang.ClassNotFoundException: out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class + ``` +) + +Solutions: Do not include the .class extension and execute from the proper folder (either with slashes or with dots): + +``` +cd out +java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +``` + +``` +cd out +java nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + +### Problem: Executing a class from the wrong folder + +``` +cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic +java Main +``` + +``` +Error: Could not find or load main class Main +Caused by: java.lang.NoClassDefFoundError: nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main (wrong name: Main) + +``` + +``` +java out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +``` + +``` +Error: Could not find or load main class out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +Caused by: java.lang.NoClassDefFoundError: nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main (wrong name: out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main) +``` + +Solutions: Execute from the proper folder: + +``` +cd out +java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +``` + +``` +cd out +java nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + +``` +java -cp out nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +``` + +``` +java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` From f61d7045fa8470defe49d9454d70037b76c18ce0 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Mon, 15 Nov 2021 21:04:43 +0100 Subject: [PATCH 14/68] Fill in the basic directory structure --- java/basic_java_project_cli/problems.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/java/basic_java_project_cli/problems.md b/java/basic_java_project_cli/problems.md index 277bec6..1e4ce44 100644 --- a/java/basic_java_project_cli/problems.md +++ b/java/basic_java_project_cli/problems.md @@ -4,9 +4,27 @@ basic_java_project_cli ├─ out -│ └─ ... +│ └─ nl +│ └─ mauritssilvis +│ └─ challenges +│ └─ java +│ └─ cli +│ └─ general +│ └─ projects +│ └─ basic +│ └─ Main.class +│ ├─ src -│ └─ ... +│ └─ nl +│ └─ mauritssilvis +│ └─ challenges +│ └─ java +│ └─ cli +│ └─ general +│ └─ projects +│ └─ basic +│ └─ Main.java +│ ├─ .gitignore └─ README.md From 09012ecabd621989d7f576240020a584ca9d417e Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Mon, 15 Nov 2021 21:36:38 +0100 Subject: [PATCH 15/68] Describe the basic Java project directory structure --- java/basic_java_project_cli/README.md | 35 ++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 8518c57..6b39841 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -25,7 +25,40 @@ public class Main { } ``` -#### 1.1.2 Folder structure +#### 1.1.2 Directory structure + +The directory structure of this project is as follows: + +```text +basic_java_project_cli +├─ out +│ └─ nl +│ └─ mauritssilvis +│ └─ challenges +│ └─ java +│ └─ cli +│ └─ general +│ └─ projects +│ └─ basic +│ └─ Main.class +│ +├─ src +│ └─ nl +│ └─ mauritssilvis +│ └─ challenges +│ └─ java +│ └─ cli +│ └─ general +│ └─ projects +│ └─ basic +│ └─ Main.java +│ +├─ .gitignore +└─ README.md +``` + +Here, the `src` folder contains the source file, Main.java, stored in a directory structure that matches the package `nl.mauritssilvis.challenges.java.cli.general.projects.basic` of the class `Main`. +The `out` folder, which is created upon compilation (see the Compile section), has the same structure as the `src` folder, but contains the compiled bytecode instead of source code. #### 1.1.3 Compile From a56c7e1ace01f09fce8b6dd965e5c9a8eb0c3d05 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Mon, 15 Nov 2021 21:59:56 +0100 Subject: [PATCH 16/68] Describe compilation of the basic Java project --- java/basic_java_project_cli/README.md | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 6b39841..e7154ea 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -57,12 +57,31 @@ basic_java_project_cli └─ README.md ``` -Here, the `src` folder contains the source file, Main.java, stored in a directory structure that matches the package `nl.mauritssilvis.challenges.java.cli.general.projects.basic` of the class `Main`. -The `out` folder, which is created upon compilation (see the Compile section), has the same structure as the `src` folder, but contains the compiled bytecode instead of source code. +Here, the `src` folder contains the source file, `Main.java`, stored in a directory structure that matches the package `nl.mauritssilvis.challenges.java.cli.general.projects.basic` of the class `Main`. +The `out` folder, which is created upon compilation (see the [Compilation section](#113-compilation)), has the same structure as the `src` folder, but is there to contain the compiled bytecode instead of source code. -#### 1.1.3 Compile +#### 1.1.3 Compilation -#### 1.1.4 Run +The most basic way to compile the Java code in `Main.java` consists in navigating to the directory of this source file and calling the Java compiler: + +```shell +cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic +javac Main.java +``` + +These commands will compile the Java code of `Main.java` into bytecode, which is stored in the class file `Main.class` in the directory where the source code is located. + +The class file, `Main.class`, cannot be executed from the folder in which it is stored. +Moreover, Java source and class files are generally stored in different locations. +Therefore, it is more convenient to compile the source file with the following command, which specifies the output directory: + +```shell +javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +``` + +#### 1.1.4 Execution + +#### 1.1.5 Direct execution ## 2. Issues and solutions From e2cc8c33bc83e468622012ea068bf13c17f61f06 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 17 Nov 2021 20:35:16 +0100 Subject: [PATCH 17/68] Polish basic Java project readme --- java/basic_java_project_cli/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index e7154ea..b24c42e 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -57,23 +57,23 @@ basic_java_project_cli └─ README.md ``` -Here, the `src` folder contains the source file, `Main.java`, stored in a directory structure that matches the package `nl.mauritssilvis.challenges.java.cli.general.projects.basic` of the class `Main`. +Here, the `src` folder contains the source file, `Main.java`, stored in a directory structure that matches the package of the class `Main`, i.e., `nl.mauritssilvis.challenges.java.cli.general.projects.basic`. The `out` folder, which is created upon compilation (see the [Compilation section](#113-compilation)), has the same structure as the `src` folder, but is there to contain the compiled bytecode instead of source code. #### 1.1.3 Compilation -The most basic way to compile the Java code in `Main.java` consists in navigating to the directory of this source file and calling the Java compiler: +The most basic way to compile the Java source code in `Main.java` consists in navigating to the directory of this file and calling the Java compiler: ```shell cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic javac Main.java ``` -These commands will compile the Java code of `Main.java` into bytecode, which is stored in the class file `Main.class` in the directory where the source code is located. +These commands will compile the Java code of `Main.java` into bytecode, which is then stored in the class file `Main.class` in the directory where the source code is located. The class file, `Main.class`, cannot be executed from the folder in which it is stored. Moreover, Java source and class files are generally stored in different locations. -Therefore, it is more convenient to compile the source file with the following command, which specifies the output directory: +Therefore, it is more convenient to compile the source file from the project root folder with a directive that specifies the output directory: ```shell javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java From f6aed2e0916a0b1fa85999ca6c853445604823c6 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 17 Nov 2021 21:43:46 +0100 Subject: [PATCH 18/68] Describe execution of a Java class --- java/basic_java_project_cli/README.md | 40 ++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index b24c42e..947b2be 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -58,7 +58,7 @@ basic_java_project_cli ``` Here, the `src` folder contains the source file, `Main.java`, stored in a directory structure that matches the package of the class `Main`, i.e., `nl.mauritssilvis.challenges.java.cli.general.projects.basic`. -The `out` folder, which is created upon compilation (see the [Compilation section](#113-compilation)), has the same structure as the `src` folder, but is there to contain the compiled bytecode instead of source code. +The `out` folder, which is created upon [compilation](#113-compilation), has the same structure as the `src` folder, but is there to contain the compiled bytecode instead of source code. #### 1.1.3 Compilation @@ -81,6 +81,44 @@ javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Mai #### 1.1.4 Execution +After [compilation](#113-compilation), the Java class file, `Main.class`, that is buried in the `out` folder can be executed in several ways. + +A convenient way to execute this class is given by: + +```shell +cd out +java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +``` + +Equivalently, one could execute the following commands: + +```shell +cd out +java nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + +Rather than navigating to a folder containing class files manually, it's more common to execute a Java class under specification of the class path. +For the current project, this looks as follows: + +```shell +java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + +All above commands will result in the following output: + +```text +Hello world! +``` + +Note that Java executes classes by their class name, not by their file name. +Therefore, the above commands should not include the `.class` extension of the Java class file name `Main.class`. + +Moreover, for classes that are part of a package, the supplied class name should be the fully qualified name, which includes both the package and the class name. +The folder from which such a class is executed has to contain a directory structure corresponding to the package name. +Therefore, the Java class `Main.class` cannot be executed from any other folder than the `out` folder. +In particular, it cannot be executed from its containing folder. +As the above shows, class names can, however, be specified using both slashes and dots. + #### 1.1.5 Direct execution ## 2. Issues and solutions From d64fcb987457bdd25b489236f9a26529512aaf42 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 17 Nov 2021 22:14:55 +0100 Subject: [PATCH 19/68] Polish the description of execution of a Java class --- java/basic_java_project_cli/README.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 947b2be..801ad5a 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -98,6 +98,7 @@ java nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main ``` Rather than navigating to a folder containing class files manually, it's more common to execute a Java class under specification of the class path. + For the current project, this looks as follows: ```shell @@ -114,10 +115,17 @@ Note that Java executes classes by their class name, not by their file name. Therefore, the above commands should not include the `.class` extension of the Java class file name `Main.class`. Moreover, for classes that are part of a package, the supplied class name should be the fully qualified name, which includes both the package and the class name. -The folder from which such a class is executed has to contain a directory structure corresponding to the package name. -Therefore, the Java class `Main.class` cannot be executed from any other folder than the `out` folder. -In particular, it cannot be executed from its containing folder. -As the above shows, class names can, however, be specified using both slashes and dots. +For the current project, this fully qualified class name is: + +```text +nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + +The folder from which a class that is part of a package is executed has to contain a directory structure corresponding to the package name. +Therefore, the Java class file `Main.class` cannot be executed from any other folder than the `out` folder. +In particular, this class file cannot be executed from its containing folder. + +As the above shows, fully qualified class names can be specified using both slashes and dots. #### 1.1.5 Direct execution From b226fd0fee43fa3e8eb4b1c8af1f7e9057f47a5a Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 9 Mar 2022 17:38:41 +0100 Subject: [PATCH 20/68] Mark documentation sections that are to be enhanced --- java/README.md | 2 ++ java/basic_java_project_cli/README.md | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/java/README.md b/java/README.md index 01a584d..608624d 100644 --- a/java/README.md +++ b/java/README.md @@ -60,6 +60,8 @@ To compile and build these projects, the [Java Development Kit 17](https://jdk.j ### 2.2 Bash +TODO + ### 2.3 IntelliJ IDEA [IntelliJ IDEA](https://www.jetbrains.com/idea/) is an integrated development environment (IDE) developed by [JetBrains](https://www.jetbrains.com/). diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 801ad5a..315d74e 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -129,8 +129,12 @@ As the above shows, fully qualified class names can be specified using both slas #### 1.1.5 Direct execution +TODO + ## 2. Issues and solutions +TODO + ## License Copyright © 2021 Maurits H. Silvis From 00d2b042a3309d94f47fa120b2ac657c5080ed81 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 9 Mar 2022 17:39:19 +0100 Subject: [PATCH 21/68] Enhance the list of potential problems --- java/basic_java_project_cli/problems.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/java/basic_java_project_cli/problems.md b/java/basic_java_project_cli/problems.md index 1e4ce44..781129f 100644 --- a/java/basic_java_project_cli/problems.md +++ b/java/basic_java_project_cli/problems.md @@ -166,6 +166,8 @@ Do not run the Java compiler on class files. ## Java execution problems +### Problem: Error: Invalid or corrupt jarfile + ### Problem: Executing the class file including its extension ``` From 64bec0fb1408bfdcc43f16fffbbb948eb82e6310 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 11 Mar 2022 15:23:12 +0100 Subject: [PATCH 22/68] Update the Git ignore file --- java/basic_java_project_cli/.gitignore | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/java/basic_java_project_cli/.gitignore b/java/basic_java_project_cli/.gitignore index 656b445..5282a50 100644 --- a/java/basic_java_project_cli/.gitignore +++ b/java/basic_java_project_cli/.gitignore @@ -1,4 +1,6 @@ -# From https://gitignore.io + +# Created by https://gitignore.io/api/java +# Edit at https://gitignore.io?templates=java ### Java ### # Compiled class file @@ -24,3 +26,6 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +replay_pid* + +# End of https://gitignore.io/api/java From 43f4e6d2770d3232f24d8f4dc811cf2a017f5810 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 11 Mar 2022 15:38:05 +0100 Subject: [PATCH 23/68] Update copyright notices --- java/README.md | 2 +- java/basic_maven_project_intellij/README.md | 2 +- java/basic_maven_project_intellij/pom.xml | 2 +- java/executable_jar_maven_intellij/README.md | 2 +- java/executable_jar_maven_intellij/pom.xml | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/java/README.md b/java/README.md index 66218c8..8ee9e4a 100644 --- a/java/README.md +++ b/java/README.md @@ -82,6 +82,6 @@ A step-by-step guide for setting up a Maven project using IntelliJ IDEA can be f ## License -Copyright © 2021 Maurits H. Silvis +Copyright © 2021, 2022 Maurits H. Silvis This source code package is subject to the terms and conditions defined in the GNU General Public License v3.0, which can be found in the file [LICENSE.md](../LICENSE.md), or later. diff --git a/java/basic_maven_project_intellij/README.md b/java/basic_maven_project_intellij/README.md index c45fa3f..5da5b82 100644 --- a/java/basic_maven_project_intellij/README.md +++ b/java/basic_maven_project_intellij/README.md @@ -510,6 +510,6 @@ To solve this problem, configure the Maven JAR plugin according to the discussio ## License -Copyright © 2021 Maurits H. Silvis +Copyright © 2021, 2022 Maurits H. Silvis This source code package is subject to the terms and conditions defined in the GNU General Public License v3.0, which can be found in the file [LICENSE.md](../../LICENSE.md), or later. diff --git a/java/basic_maven_project_intellij/pom.xml b/java/basic_maven_project_intellij/pom.xml index 05bab88..9bf866b 100644 --- a/java/basic_maven_project_intellij/pom.xml +++ b/java/basic_maven_project_intellij/pom.xml @@ -1,7 +1,7 @@ diff --git a/java/executable_jar_maven_intellij/README.md b/java/executable_jar_maven_intellij/README.md index fff9586..98c556d 100644 --- a/java/executable_jar_maven_intellij/README.md +++ b/java/executable_jar_maven_intellij/README.md @@ -258,6 +258,6 @@ public class Main { ## License -Copyright © 2021 Maurits H. Silvis +Copyright © 2021, 2022 Maurits H. Silvis This source code package is subject to the terms and conditions defined in the GNU General Public License v3.0, which can be found in the file [LICENSE.md](../../LICENSE.md), or later. diff --git a/java/executable_jar_maven_intellij/pom.xml b/java/executable_jar_maven_intellij/pom.xml index 6c98099..ede5cd5 100644 --- a/java/executable_jar_maven_intellij/pom.xml +++ b/java/executable_jar_maven_intellij/pom.xml @@ -1,7 +1,7 @@ From dbd95603992b38e99a5658aad1df1ea506abf384 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 11 Mar 2022 15:43:55 +0100 Subject: [PATCH 24/68] Adapt the Java readme --- java/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/README.md b/java/README.md index 8ee9e4a..4b2aa2d 100644 --- a/java/README.md +++ b/java/README.md @@ -6,8 +6,8 @@ With this part of the [Challenges](https://github.com/mauritssilvis/challenges) project, I provide solutions to common programming challenges in Java. -Under [Status](#1-status), I give an overview of the challenges that I solved using Java so far. -I also provide some [background information](#2-background) on [Java](#21-java) and the tools I used, namely, [Bash](#22-bash), [IntelliJ IDEA](#23-intellij-idea) and [Maven](#24-maven). +In the [Status](#1-status) section, I give an overview of the challenges that I solved using Java so far. +I then provide some [background information](#2-background) on [Java](#21-java) and the tools I used, namely, [Bash](#22-bash), [IntelliJ IDEA](#23-intellij-idea) and [Maven](#24-maven). ## 1. Status From 4f2c59e77a4d51a0fea1e2d98d0b0a144674bd97 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 11 Mar 2022 16:02:13 +0100 Subject: [PATCH 25/68] Enhance the explanation of Java class execution --- java/basic_java_project_cli/README.md | 9 ++-- java/basic_java_project_cli/problems.md | 67 ------------------------- 2 files changed, 5 insertions(+), 71 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 315d74e..76a0a4d 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -71,7 +71,7 @@ javac Main.java These commands will compile the Java code of `Main.java` into bytecode, which is then stored in the class file `Main.class` in the directory where the source code is located. -The class file, `Main.class`, cannot be executed from the folder in which it is stored. +As we will see, the class file, `Main.class`, cannot be executed from the folder in which it is stored. Moreover, Java source and class files are generally stored in different locations. Therefore, it is more convenient to compile the source file from the project root folder with a directive that specifies the output directory: @@ -90,6 +90,8 @@ cd out java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main ``` +As is explained below, the `.class` extension of the class file should not be included. + Equivalently, one could execute the following commands: ```shell @@ -98,7 +100,6 @@ java nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main ``` Rather than navigating to a folder containing class files manually, it's more common to execute a Java class under specification of the class path. - For the current project, this looks as follows: ```shell @@ -121,12 +122,12 @@ For the current project, this fully qualified class name is: nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main ``` +As the above shows, fully qualified class names can be specified using both slashes and dots. + The folder from which a class that is part of a package is executed has to contain a directory structure corresponding to the package name. Therefore, the Java class file `Main.class` cannot be executed from any other folder than the `out` folder. In particular, this class file cannot be executed from its containing folder. -As the above shows, fully qualified class names can be specified using both slashes and dots. - #### 1.1.5 Direct execution TODO diff --git a/java/basic_java_project_cli/problems.md b/java/basic_java_project_cli/problems.md index 781129f..fc5ec71 100644 --- a/java/basic_java_project_cli/problems.md +++ b/java/basic_java_project_cli/problems.md @@ -1,74 +1,7 @@ # Background -## Directory structure - -basic_java_project_cli -├─ out -│ └─ nl -│ └─ mauritssilvis -│ └─ challenges -│ └─ java -│ └─ cli -│ └─ general -│ └─ projects -│ └─ basic -│ └─ Main.class -│ -├─ src -│ └─ nl -│ └─ mauritssilvis -│ └─ challenges -│ └─ java -│ └─ cli -│ └─ general -│ └─ projects -│ └─ basic -│ └─ Main.java -│ -├─ .gitignore -└─ README.md - -## Java compilation - -### Compiling the Java source file using the Java compiler - -``` -cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic -javac Main.java -``` - -( - ``` - javac src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java - ``` -) - -``` -javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java -``` - ## Java execution -### Executing the compiled Java class from the proper folder - -``` -cd out -java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main -``` - -``` -cd out -java nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main -``` - -``` -java -cp out nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main -``` - -``` -java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main -``` - ### Directly executing the Java source file using Java from any folder ``` From d3185349c54aa920b9c58f1df48e26d031c76985 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Mon, 14 Mar 2022 18:05:54 +0100 Subject: [PATCH 26/68] Enhance the explanation of Java class execution --- java/basic_java_project_cli/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 76a0a4d..898008d 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -99,7 +99,7 @@ cd out java nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main ``` -Rather than navigating to a folder containing class files manually, it's more common to execute a Java class under specification of the class path. +Rather than navigating to a folder containing class files manually, it is more common to execute a Java class under specification of the class path. For the current project, this looks as follows: ```shell From f7eb43e913b64a3534a0a07eba73aa93b60a8721 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Thu, 17 Mar 2022 20:12:43 +0100 Subject: [PATCH 27/68] Enhance the overview of solved challenges --- java/README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/java/README.md b/java/README.md index 4b2aa2d..8bbb094 100644 --- a/java/README.md +++ b/java/README.md @@ -6,10 +6,10 @@ With this part of the [Challenges](https://github.com/mauritssilvis/challenges) project, I provide solutions to common programming challenges in Java. -In the [Status](#1-status) section, I give an overview of the challenges that I solved using Java so far. +Below, I first give an [overview](#1-overview) of the challenges that I solved using Java so far. I then provide some [background information](#2-background) on [Java](#21-java) and the tools I used, namely, [Bash](#22-bash), [IntelliJ IDEA](#23-intellij-idea) and [Maven](#24-maven). -## 1. Status +## 1. Overview Currently, Java solutions are available to the following challenges, categorized according to the tools I used. @@ -19,6 +19,8 @@ I solved the challenges in this section using the [command line](#22-bash). #### 1.1.1 General +The challenges listed below were solved without a build tool. + ##### Projects * [Setting up a basic Java project using the command line](basic_java_project_cli) @@ -26,6 +28,7 @@ I solved the challenges in this section using the [command line](#22-bash). ### 1.2 IntelliJ IDEA projects I solved the following challenges using [IntelliJ IDEA](#23-intellij-idea). +For these challenges, I either did not make use of a build tool (general) or I made use of Maven. #### 1.2.1 General From 986970afdf952cfe5fc32e2d50b26d34414268fb Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Thu, 17 Mar 2022 21:20:01 +0100 Subject: [PATCH 28/68] Restructure the project readmes --- README.md | 9 ++++++--- java/basic_java_project_cli/README.md | 9 ++++++++- java/basic_java_project_intellij/README.md | 5 ++++- java/basic_maven_project_intellij/README.md | 7 +++++-- java/executable_jar_maven_intellij/README.md | 10 +++------- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index e8da73c..73c4471 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,14 @@ > A collection of programming challenges, solved using different programming languages -## About +## Introduction With this project, I provide solutions to common programming challenges in different programming languages. -## Aims +Below, I first describe the [aims](#1-aims) I have with the current project. +I then [list](#2-overview) the languages I used to solve programming challenges. + +## 1. Aims My aims with this project are to: @@ -22,7 +25,7 @@ Finally, I aim to: - Showcase this experience and knowledge. -## Status +## 2. Overview Currently, solutions to common programming challenges are available in: diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 898008d..1d952e4 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -6,10 +6,17 @@ With this part of the [Challenges > Java](..) project, I provide the code and settings for a basic Java project, set up using the command line. +Below, I give detailed [background information](#1-background) on the project's [code](#11-java). +In addition, I list several [issues](#2-issues-and-solutions) that can occur when setting up a Java project. +I also describe solutions to these issues. + ## 1. Background ### 1.1 Java +This project makes use of Java. +I describe this programming language in the [Java section](../#21-java) of [Challenges > Java](..). + #### 1.1.1 Code For demonstrative purposes, the current project contains only a single Java file, [Main.java](src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java), which comprises a Java class with the corresponding name `Main`. @@ -126,7 +133,7 @@ As the above shows, fully qualified class names can be specified using both slas The folder from which a class that is part of a package is executed has to contain a directory structure corresponding to the package name. Therefore, the Java class file `Main.class` cannot be executed from any other folder than the `out` folder. -In particular, this class file cannot be executed from its containing folder. +In particular, this class file cannot be executed from its containing parent folder. #### 1.1.5 Direct execution diff --git a/java/basic_java_project_intellij/README.md b/java/basic_java_project_intellij/README.md index 731b952..c0d7147 100644 --- a/java/basic_java_project_intellij/README.md +++ b/java/basic_java_project_intellij/README.md @@ -6,12 +6,15 @@ With this part of the [Challenges > Java](..) project, I provide the code and settings for a basic Java project, set up using IntelliJ IDEA. -Below, I give an [overview](#1-background) of the project's [code](#11-java) and [IntelliJ IDEA's configuration](#12-intellij-idea). +Below, I give [background information](#1-background) on the project's [code](#11-java) and [IntelliJ IDEA's configuration](#12-intellij-idea). ## 1. Background ### 1.1 Java +This project makes use of Java. +I describe this programming language in the [Java section](../#21-java) of [Challenges > Java](..). + #### 1.1.1 Code For demonstrative purposes, the current project contains only a single Java class called `Main`, which consists of a `main` method outputting a well-known message: diff --git a/java/basic_maven_project_intellij/README.md b/java/basic_maven_project_intellij/README.md index 5da5b82..a361bc7 100644 --- a/java/basic_maven_project_intellij/README.md +++ b/java/basic_maven_project_intellij/README.md @@ -6,8 +6,8 @@ With this part of the [Challenges > Java](..) project, I provide the code and settings for a basic Maven project, set up using IntelliJ IDEA. -Below, I give an [overview](#1-background) of the project's [code](#11-java), [IntelliJ IDEA's configuration](#12-intellij-idea) and the used [Maven configuration](#13-maven). -I also detail several [issues](#2-issues-and-solutions) that can occur when setting up a Maven project, and I provide possible solutions to these issues. +Below, I give detailed [background information](#1-background) on the project's [code](#11-java), [IntelliJ IDEA's configuration](#12-intellij-idea) and the used [Maven configuration](#13-maven). +I also describe several [issues](#2-issues-and-solutions) that can occur when setting up a Maven project, and I provide possible solutions to these issues. This project builds on [Setting up a basic Java project using IntelliJ IDEA](../basic_java_project_intellij). @@ -15,6 +15,9 @@ This project builds on [Setting up a basic Java project using IntelliJ IDEA](../ ### 1.1 Java +This project makes use of Java. +I describe this programming language in the [Java section](../#21-java) of [Challenges > Java](..). + #### 1.1.1 Code For demonstrative purposes, the current project contains only a single Java class called `Main`, which consists of a `main` method outputting a well-known message: diff --git a/java/executable_jar_maven_intellij/README.md b/java/executable_jar_maven_intellij/README.md index 98c556d..d7ff27e 100644 --- a/java/executable_jar_maven_intellij/README.md +++ b/java/executable_jar_maven_intellij/README.md @@ -6,7 +6,7 @@ With this part of the [Challenges > Java](..) project, I provide the code and settings for a Maven project that can create an executable JAR. -Below, I give an [overview](#1-background) of the project's [code](#11-java), [IntelliJ IDEA's configuration](#12-intellij-idea) and the used [Maven configuration](#13-maven). +Below, I give [background information](#1-background) on the project's [code](#11-java), [IntelliJ IDEA's configuration](#12-intellij-idea) and the used [Maven configuration](#13-maven). I also detail some [issues](#2-issues-and-solutions) that can occur when creating an executable JAR using Maven, and I provide possible solutions to these issues. This project builds on [Setting up a basic Java project using IntelliJ IDEA](../basic_java_project_intellij) and [Setting up a basic Maven project using IntelliJ IDEA](../basic_maven_project_intellij). @@ -31,9 +31,7 @@ public class Main { ### 1.2 IntelliJ IDEA -This project is set up using [IntelliJ IDEA](https://www.jetbrains.com/idea/) (see, e.g., the [IntelliJ IDEA section](../#23-intellij-idea) of [Challenges > Java](..)). - -The basic configuration is described in the [IntelliJ IDEA section](../basic_java_project_intellij#12-intellij-idea) of [Setting up a basic Java project using IntelliJ IDEA](../basic_java_project_intellij). +The basic configuration of this project is described in the [IntelliJ IDEA section](../basic_java_project_intellij#12-intellij-idea) of [Setting up a basic Java project using IntelliJ IDEA](../basic_java_project_intellij). Additional details regarding the IntelliJ IDEA run configurations of the current project are given in what follows. #### 1.2.1 Configuration @@ -46,9 +44,7 @@ The other two run configurations, respectively, create a JAR and execute it. ### 1.3 Maven -This Java project is managed and built using [Maven](https://maven.apache.org/) (see, e.g., the [Maven section](../#24-maven) of [Challenges > Java](..)). - -The used configuration extends the [Maven configuration](../basic_maven_project_intellij#13-maven) of [Setting up a basic Maven project using IntelliJ IDEA](../basic_maven_project_intellij). +The configuration used in this project extends the [Maven configuration](../basic_maven_project_intellij#13-maven) of [Setting up a basic Maven project using IntelliJ IDEA](../basic_maven_project_intellij). Additional details regarding the Maven configuration of the current project are given in what follows. #### 1.3.1 Configuration From daeb7b25c38c3d88d5a1350ce768552a1359dc4d Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Thu, 17 Mar 2022 22:08:38 +0100 Subject: [PATCH 29/68] Update the copyright notices --- java/basic_java_project_cli/README.md | 2 +- java/basic_java_project_intellij/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 1d952e4..c23520b 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -145,6 +145,6 @@ TODO ## License -Copyright © 2021 Maurits H. Silvis +Copyright © 2021, 2022 Maurits H. Silvis This source code package is subject to the terms and conditions defined in the GNU General Public License v3.0, which can be found in the file [LICENSE.md](../../LICENSE.md), or later. diff --git a/java/basic_java_project_intellij/README.md b/java/basic_java_project_intellij/README.md index c0d7147..8effe98 100644 --- a/java/basic_java_project_intellij/README.md +++ b/java/basic_java_project_intellij/README.md @@ -78,6 +78,6 @@ This project comes with one run configuration for executing the `main` method of ## License -Copyright © 2021 Maurits H. Silvis +Copyright © 2021, 2022 Maurits H. Silvis This source code package is subject to the terms and conditions defined in the GNU General Public License v3.0, which can be found in the file [LICENSE.md](../../LICENSE.md), or later. From 59388bbf610ee2b23b238ea4fd80eb1b0deeb017 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 18 Mar 2022 03:12:55 +0100 Subject: [PATCH 30/68] Clarify use of word language --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 73c4471..f14291e 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ With this project, I provide solutions to common programming challenges in different programming languages. Below, I first describe the [aims](#1-aims) I have with the current project. -I then [list](#2-overview) the languages I used to solve programming challenges. +I then [list](#2-overview) the programming languages I used to solve programming challenges. ## 1. Aims From f4d7bee5c5b4653bdf1707e4e92a41e033f1309e Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 13:04:18 +0200 Subject: [PATCH 31/68] Adapt the project scope --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f14291e..3358393 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ## Introduction -With this project, I provide solutions to common programming challenges in different programming languages. +With this project, I provide solutions to challenges one commonly encounters when working with different programming languages. Below, I first describe the [aims](#1-aims) I have with the current project. I then [list](#2-overview) the programming languages I used to solve programming challenges. @@ -13,7 +13,7 @@ I then [list](#2-overview) the programming languages I used to solve programming My aims with this project are to: -- Extend my knowledge of algorithms, data structures and design patterns. +- Extend my knowledge of different programming tools. - Apply this knowledge in solving practical programming problems. More generally, I strive to: From 9272e2e2bd03bd055612cf5aefc2e6c09908ec49 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 13:17:52 +0200 Subject: [PATCH 32/68] Properly refer to the basic IntelliJ IDEA configuration --- java/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/README.md b/java/README.md index 8bbb094..10ba329 100644 --- a/java/README.md +++ b/java/README.md @@ -72,7 +72,7 @@ This IDE supports Java and several other Java virtual machine languages, and can IntelliJ IDEA comes with a [detailed online documentation](https://www.jetbrains.com/help/idea/discover-intellij-idea.html) and its [community edition](https://www.jetbrains.com/idea/download/) can be downloaded and used for free. IntelliJ IDEA makes it easy to configure code styles, automatic copyright notices, code inspection profiles, run configurations and more. -The basic IntelliJ IDEA configuration used in the above-mentioned projects is described in the [IntelliJ IDEA section](basic_maven_project_intellij#12-intellij-idea) of [Setting up a basic Maven project using IntelliJ IDEA](basic_maven_project_intellij). +The basic IntelliJ IDEA configuration used in the above-mentioned projects is described in the [IntelliJ IDEA section](basic_java_project_intellij#12-intellij-idea) of [Setting up a basic Java project using IntelliJ IDEA](basic_java_project_intellij). ### 2.4 Maven From 001354fca6f1ab94c6cd4c6835261e220248b6ca Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 14:49:31 +0200 Subject: [PATCH 33/68] Enhance readme --- java/basic_java_project_cli/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index c23520b..9ebaaa7 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -80,7 +80,7 @@ These commands will compile the Java code of `Main.java` into bytecode, which is As we will see, the class file, `Main.class`, cannot be executed from the folder in which it is stored. Moreover, Java source and class files are generally stored in different locations. -Therefore, it is more convenient to compile the source file from the project root folder with a directive that specifies the output directory: +Therefore, it is more convenient to compile the source file from the project root folder with a `-d` option that specifies the output directory: ```shell javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java @@ -106,8 +106,8 @@ cd out java nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main ``` -Rather than navigating to a folder containing class files manually, it is more common to execute a Java class under specification of the class path. -For the current project, this looks as follows: +Rather than manually navigating to a folder containing class files, it is more common to execute a Java class relative to a so-called class path. +For the current project, the following command involving the `-cp` option executes the previously compiled class from the class path `out`: ```shell java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main From 37a2d33c9946272fe45411433be80af4c072e0e9 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 14:49:43 +0200 Subject: [PATCH 34/68] Provide details to known issues --- java/basic_java_project_cli/problems.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/java/basic_java_project_cli/problems.md b/java/basic_java_project_cli/problems.md index fc5ec71..95973ab 100644 --- a/java/basic_java_project_cli/problems.md +++ b/java/basic_java_project_cli/problems.md @@ -103,6 +103,8 @@ Do not run the Java compiler on class files. ### Problem: Executing the class file including its extension +Error: Could not find or load main class / Caused by: java.lang.ClassNotFoundException + ``` cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic java Main.class @@ -148,6 +150,8 @@ java nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main ### Problem: Executing a class from the wrong folder +Error: Could not find or load main class / Caused by: java.lang.NoClassDefFoundError + ``` cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic java Main From dd90b6524f26ca4b09765875c6e4ea5d08a0fc66 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 15:04:20 +0200 Subject: [PATCH 35/68] Describe direct execution of single Java source files --- java/basic_java_project_cli/README.md | 20 +++++++++++++++++++- java/basic_java_project_cli/problems.md | 17 ----------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 9ebaaa7..0a3442e 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -137,7 +137,25 @@ In particular, this class file cannot be executed from its containing parent fol #### 1.1.5 Direct execution -TODO +Since Java 11, programs consisting of only a single source file can be executed directly, without the need for an explicit compilation step. +For the current project, the following command directly executes our source file from its containing folder: + +```shell +cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic +java Main.java +``` + +Alternatively, direct execution from the main project folder can be started using: + +```bash +java src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +``` + +As expected, the output of the above commands is: + +```text +Hello world! +``` ## 2. Issues and solutions diff --git a/java/basic_java_project_cli/problems.md b/java/basic_java_project_cli/problems.md index 95973ab..ce40573 100644 --- a/java/basic_java_project_cli/problems.md +++ b/java/basic_java_project_cli/problems.md @@ -1,20 +1,3 @@ -# Background - -## Java execution - -### Directly executing the Java source file using Java from any folder - -``` -cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic -java Main.java -``` - -``` -java src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java -``` - -Hello world! - # Problems ## Java compilation problems From d4292817b7c34b51b42e4588037a44897053e48d Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 15:19:33 +0200 Subject: [PATCH 36/68] Start setting up the basic Java project issue section --- java/basic_java_project_cli/README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 0a3442e..39f5d2f 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -159,7 +159,13 @@ Hello world! ## 2. Issues and solutions -TODO +While setting up a Java project, several issues may occur. +I list several such issues, here, including possible solutions. +In particular, I discuss problems related to [compilation](#21-compilation-issues) and [execution](#22-execution-issues) of Java programs. + +### 2.1 Compilation issues + +### 2.2 Execution issues ## License From a78964eae264bb8030a9019cffd762b91da8381a Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 15:20:08 +0200 Subject: [PATCH 37/68] Adapt the introduction of issues --- java/basic_maven_project_intellij/README.md | 4 ++-- java/executable_jar_maven_intellij/README.md | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/java/basic_maven_project_intellij/README.md b/java/basic_maven_project_intellij/README.md index a361bc7..5348b78 100644 --- a/java/basic_maven_project_intellij/README.md +++ b/java/basic_maven_project_intellij/README.md @@ -118,8 +118,8 @@ Note that this JAR will [not be executable](#24-maven-jar-plugin-issues). ## 2. Issues and solutions -While setting up and building a Maven project, several problems may occur. -I list several of such problems, here, including possible solutions. +While setting up and building a Maven project, several issues may occur. +I list several such issues, below, including possible solutions. Specifically, I discuss problems related to the [project object model](#21-project-object-model-issues), the [Maven resources plugin](#22-maven-resources-plugin-issues), the [Maven compiler plugin](#23-maven-compiler-plugin-issues) and the [Maven JAR plugin](#24-maven-jar-plugin-issues). ### 2.1 Project object model issues diff --git a/java/executable_jar_maven_intellij/README.md b/java/executable_jar_maven_intellij/README.md index d7ff27e..effe575 100644 --- a/java/executable_jar_maven_intellij/README.md +++ b/java/executable_jar_maven_intellij/README.md @@ -92,11 +92,11 @@ mvn clean install ## 2. Issues and solutions -While setting up and building a Maven project, several problems may occur. -I partly documented these problems in the [Issues and solutions section](../basic_maven_project_intellij#2-issues-and-solutions) of [Setting up a basic Maven project using IntelliJ IDEA](../basic_maven_project_intellij). +While setting up and building a Maven project, several issues may occur. +I partly documented these issues in the [Issues and solutions section](../basic_maven_project_intellij#2-issues-and-solutions) of [Setting up a basic Maven project using IntelliJ IDEA](../basic_maven_project_intellij). Additional problems may occur when trying to execute a JAR created using Maven. -I describe the problems related to the [Maven JAR plugin](#21-maven-jar-plugin-issues), including possible solutions. +I describe the issues related to the [Maven JAR plugin](#21-maven-jar-plugin-issues), including possible solutions, below. ### 2.1 Maven JAR plugin issues From 1e635dc828e83346899b245510fd2b2d460f0a42 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 16:16:12 +0200 Subject: [PATCH 38/68] Describe the first compiler issue --- java/basic_java_project_cli/README.md | 22 ++++++++++++++++++++++ java/basic_java_project_cli/problems.md | 20 -------------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 39f5d2f..317cacb 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -165,6 +165,28 @@ In particular, I discuss problems related to [compilation](#21-compilation-issue ### 2.1 Compilation issues +#### 2.1.1 file not found + +Since class names can be specified using both slashes and dots, one may be tempted to also use dots in paths to Java source files: + +```shell +javac src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java +``` + +Such a command will, however, result in an error of the form: + +```text +error: file not found: src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java +Usage: javac +use --help for a list of possible options +``` + +To solve this problem, do not use dots instead of slashes where a file path to a source file is expected: + +```shell +javac src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +``` + ### 2.2 Execution issues ## License diff --git a/java/basic_java_project_cli/problems.md b/java/basic_java_project_cli/problems.md index ce40573..90abfc3 100644 --- a/java/basic_java_project_cli/problems.md +++ b/java/basic_java_project_cli/problems.md @@ -2,26 +2,6 @@ ## Java compilation problems -### file not found - -Using dots instead of slashes in the Java source file path: - -``` -javac src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java -``` - -``` -error: file not found: src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java -Usage: javac -use --help for a list of possible options -``` - -Use normal paths, not using dots to separate folder names: - -``` -javac src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java -``` - ### Class names are only accepted if annotation processing is explicitly requested Using a class-name-like path: From a3f4a4f38369f387a9a00862c42a0251444ae379 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 17:05:43 +0200 Subject: [PATCH 39/68] Describe another compiler issue --- java/basic_java_project_cli/README.md | 57 +++++++++++++++++++++++-- java/basic_java_project_cli/problems.md | 25 ----------- 2 files changed, 54 insertions(+), 28 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 317cacb..553b37f 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -167,13 +167,13 @@ In particular, I discuss problems related to [compilation](#21-compilation-issue #### 2.1.1 file not found -Since class names can be specified using both slashes and dots, one may be tempted to also use dots in paths to Java source files: +Since class names can be specified using both slashes and dots, one may be tempted to also use dots in paths to Java source files in calls to the Java compiler: ```shell javac src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java ``` -Such a command will, however, result in an error of the form: +Such commands will, however, result in an error of the form: ```text error: file not found: src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java @@ -184,7 +184,58 @@ use --help for a list of possible options To solve this problem, do not use dots instead of slashes where a file path to a source file is expected: ```shell -javac src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +``` + +#### 2.1.2 Class names are only accepted if annotation processing is explicitly requested + +When working with Java, you may encounter errors of the following form: + +```text +error: Class names, 'Main', are only accepted if annotation processing is explicitly requested +1 error +``` + +```text +error: Class names, 'nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main', are only accepted if annotation processing is explicitly requested +1 error +``` + +```text +error: Class names, 'src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main', are only accepted if annotation processing is explicitly requested +1 error +``` + +Such errors respectively result from using the Java compiler in commands of the form: + +```shell +cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic +javac Main +``` + +```shell +cd src +javac nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + +```shell +javac src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + +Here, the Java compiler is supplied with class-name-like paths. + +To solve the above problem, do not supply the Java compiler with a class-name-like path when a file path to a source file is expected. +In addition, ensure the `.java` file extension is present in paths to source files: + +```shell +javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +``` + +Alternatively, ensure that you called the right tool. +You may have wanted to call the Java interpreter instead of the Java compiler: + +```shell +java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main ``` ### 2.2 Execution issues diff --git a/java/basic_java_project_cli/problems.md b/java/basic_java_project_cli/problems.md index 90abfc3..9367476 100644 --- a/java/basic_java_project_cli/problems.md +++ b/java/basic_java_project_cli/problems.md @@ -2,31 +2,6 @@ ## Java compilation problems -### Class names are only accepted if annotation processing is explicitly requested - -Using a class-name-like path: - -``` -cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic -javac Main -``` - -``` -error: Class names, 'Main', are only accepted if annotation processing is explicitly requested -1 error -``` - -``` -javac src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main -``` - -``` -error: Class names, 'src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main', are only accepted if annotation processing is explicitly requested -1 error -``` - -Do not use class-name-like paths. - ### error: invalid flag Using a class-name-like path with a .class ending: From 639b63dc9791f8c458b8cce13f1b2f1bc2cd0ccb Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 17:22:41 +0200 Subject: [PATCH 40/68] Describe another compiler issue --- java/basic_java_project_cli/README.md | 55 ++++++++++++++++++++++++- java/basic_java_project_cli/problems.md | 35 ---------------- 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 553b37f..ae2bde0 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -181,7 +181,7 @@ Usage: javac use --help for a list of possible options ``` -To solve this problem, do not use dots instead of slashes where a file path to a source file is expected: +To solve this problem, do not use dots instead of slashes when a file path to a source file is expected: ```shell javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java @@ -238,6 +238,59 @@ You may have wanted to call the Java interpreter instead of the Java compiler: java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main ``` +#### 2.1.3 invalid flag + +When working with Java, you may encounter errors of the following form: + +```text +error: invalid flag: Main.class +Usage: javac +use --help for a list of possible options +``` + +```text +error: invalid flag: nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class +Usage: javac +use --help for a list of possible options +``` + +```text +error: invalid flag: src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class +Usage: javac +use --help for a list of possible options +``` + +Such errors respectively results from using the Java compiler with commands of the form: + +```shell +cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic +javac Main.class +``` + +```shell +cd src +javac nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class +``` + +```shell +javac src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class +``` + +To solve issues of the above form, do not provide the Java compiler with a class-name-like path when a file path to a source file is expected. +In addition, ensure the `.java` file extension (instead of `.class`) is present in paths to source files: + +```shell +javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +``` + +Alternatively, ensure that you called the right tool. +You may have wanted to call the Java interpreter instead of the Java compiler. +In that case, note that the `.class` extension of the Java class file name has to be omitted to refer to the class: + +```shell +java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + ### 2.2 Execution issues ## License diff --git a/java/basic_java_project_cli/problems.md b/java/basic_java_project_cli/problems.md index 9367476..21fb669 100644 --- a/java/basic_java_project_cli/problems.md +++ b/java/basic_java_project_cli/problems.md @@ -1,40 +1,5 @@ # Problems -## Java compilation problems - -### error: invalid flag - -Using a class-name-like path with a .class ending: -/ -Running the Java compiler on a class file: -/ -Any invalid flag: - -( - ``` - javac out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class - ``` - - ``` - error: invalid flag: out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class - Usage: javac - use --help for a list of possible options - ``` -) - -``` -cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic -javac Main.class -``` - -``` -error: invalid flag: Main.class -Usage: javac -use --help for a list of possible options -``` - -Do not run the Java compiler on class files. - ## Java execution problems ### Problem: Error: Invalid or corrupt jarfile From 0a331a24bffeaacef6a3a5ad98318e201bc7fac9 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 17:47:40 +0200 Subject: [PATCH 41/68] Enhance compiler issue descriptions --- java/basic_java_project_cli/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index ae2bde0..8e619fd 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -181,7 +181,7 @@ Usage: javac use --help for a list of possible options ``` -To solve this problem, do not use dots instead of slashes when a file path to a source file is expected: +To solve this problem, use slashes when a file path to a source file is expected: ```shell javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java @@ -260,7 +260,7 @@ Usage: javac use --help for a list of possible options ``` -Such errors respectively results from using the Java compiler with commands of the form: +Such errors respectively result from using the Java compiler with commands of the form: ```shell cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic From d959a64f205e7c8765c2285b2a88e0e9d014bb93 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 18:08:08 +0200 Subject: [PATCH 42/68] Describe the first execution issue --- java/basic_java_project_cli/README.md | 49 +++++++++++++++++++++++++ java/basic_java_project_cli/problems.md | 47 ------------------------ 2 files changed, 49 insertions(+), 47 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 8e619fd..2de19d0 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -293,6 +293,55 @@ java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main ### 2.2 Execution issues +#### 2.2.1 Could not find or load main class / java.lang.ClassNotFoundException + +When trying to execute Java programs, you may encounter errors of the following form: + +```text +Error: Could not find or load main class Main.class +Caused by: java.lang.ClassNotFoundException: Main.class +``` + +```text +Error: Could not find or load main class nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class +Caused by: java.lang.ClassNotFoundException: nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class +``` + +```text +Error: Could not find or load main class out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class +Caused by: java.lang.ClassNotFoundException: out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class +``` + +Such errors result from calling the Java interpreter using commands of the form: + +```shell +cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic +java Main.class +``` + +```shell +cd out +java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.class +``` + +```shell +java out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.class +``` + +To solve the above problem, do not include the `.class` extension when referring to Java classes and ensure execution takes place from the proper path. +For the current project, these two conditions are fulfilled for the following command: + +```shell +cd out +java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +``` + +As noted before, a more common way of writing this above command is given by: + +```shell +java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + ## License Copyright © 2021, 2022 Maurits H. Silvis diff --git a/java/basic_java_project_cli/problems.md b/java/basic_java_project_cli/problems.md index 21fb669..030141b 100644 --- a/java/basic_java_project_cli/problems.md +++ b/java/basic_java_project_cli/problems.md @@ -4,53 +4,6 @@ ### Problem: Error: Invalid or corrupt jarfile -### Problem: Executing the class file including its extension - -Error: Could not find or load main class / Caused by: java.lang.ClassNotFoundException - -``` -cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic -java Main.class -``` - -( - ``` - Error: Could not find or load main class Main.class - Caused by: java.lang.ClassNotFoundException: Main.class - ``` - - ``` - cd out - java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.class - ``` - - ``` - Error: Could not find or load main class nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class - Caused by: java.lang.ClassNotFoundException: nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class - ``` - - ``` - java out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.class - ``` - - ``` - Error: Could not find or load main class out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class - Caused by: java.lang.ClassNotFoundException: out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class - ``` -) - -Solutions: Do not include the .class extension and execute from the proper folder (either with slashes or with dots): - -``` -cd out -java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main -``` - -``` -cd out -java nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main -``` - ### Problem: Executing a class from the wrong folder Error: Could not find or load main class / Caused by: java.lang.NoClassDefFoundError From a229f36b65a6e24614f1238a78c0a039859483a3 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 18:25:28 +0200 Subject: [PATCH 43/68] Describe the second execution issue --- java/basic_java_project_cli/README.md | 41 ++++++++++++++++++++++- java/basic_java_project_cli/problems.md | 44 ------------------------- 2 files changed, 40 insertions(+), 45 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 2de19d0..581c971 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -336,7 +336,46 @@ cd out java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main ``` -As noted before, a more common way of writing this above command is given by: +As noted before, a more common way of writing this command is given by: + +```shell +java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + +#### 2.2.2 Could not find or load main class / java.lang.NoClassDefFoundError + +When trying to execute Java programs, you may encounter errors of the following form: + +```text +Error: Could not find or load main class Main +Caused by: java.lang.NoClassDefFoundError: nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main (wrong name: Main) +``` + +```text +Error: Could not find or load main class out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +Caused by: java.lang.NoClassDefFoundError: nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main (wrong name: out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main) +``` + +Such errors result from calling the Java interpreter using commands of the form: + +```shell +cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic +java Main +``` + +```shell +java out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +``` + +To solve the above problem, ensure that classes that belong to a package are executed from a folder that contains a directory structure corresponding to the package name. +For the current project, the following command could be used: + +```shell +cd out +java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +``` + +As noted before, a more common way of writing this command is given by: ```shell java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main diff --git a/java/basic_java_project_cli/problems.md b/java/basic_java_project_cli/problems.md index 030141b..395fa0f 100644 --- a/java/basic_java_project_cli/problems.md +++ b/java/basic_java_project_cli/problems.md @@ -3,47 +3,3 @@ ## Java execution problems ### Problem: Error: Invalid or corrupt jarfile - -### Problem: Executing a class from the wrong folder - -Error: Could not find or load main class / Caused by: java.lang.NoClassDefFoundError - -``` -cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic -java Main -``` - -``` -Error: Could not find or load main class Main -Caused by: java.lang.NoClassDefFoundError: nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main (wrong name: Main) - -``` - -``` -java out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main -``` - -``` -Error: Could not find or load main class out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main -Caused by: java.lang.NoClassDefFoundError: nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main (wrong name: out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main) -``` - -Solutions: Execute from the proper folder: - -``` -cd out -java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main -``` - -``` -cd out -java nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main -``` - -``` -java -cp out nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main -``` - -``` -java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main -``` From 61200b890ed9d37271ee998ea6fc9f82c38e8174 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 19:51:38 +0200 Subject: [PATCH 44/68] Polish the basic Java project readme --- java/basic_java_project_cli/README.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 581c971..77f7836 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -122,30 +122,32 @@ Hello world! Note that Java executes classes by their class name, not by their file name. Therefore, the above commands should not include the `.class` extension of the Java class file name `Main.class`. -Moreover, for classes that are part of a package, the supplied class name should be the fully qualified name, which includes both the package and the class name. +Moreover, for classes that are part of a package, the supplied class name should be the so-called fully qualified name, which includes both the package and the class name. For the current project, this fully qualified class name is: ```text nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main ``` -As the above shows, fully qualified class names can be specified using both slashes and dots. +As the above shows, fully qualified class names can be provided to the Java interpreter using both slashes and dots. -The folder from which a class that is part of a package is executed has to contain a directory structure corresponding to the package name. +Class files of Java classes that are part of a package have to be stored in a directory structure corresponding to the package name. +Otherwise, these classes cannot be executed. +More specifically, the folder from which a class is executed should contain a directory structure corresponding to the package name of that class. Therefore, the Java class file `Main.class` cannot be executed from any other folder than the `out` folder. In particular, this class file cannot be executed from its containing parent folder. #### 1.1.5 Direct execution Since Java 11, programs consisting of only a single source file can be executed directly, without the need for an explicit compilation step. -For the current project, the following command directly executes our source file from its containing folder: +For the current project, the following command directly executes the source file `Main.java` from its containing folder: ```shell cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic java Main.java ``` -Alternatively, direct execution from the main project folder can be started using: +Alternatively, direct execution from the main project folder is possible: ```bash java src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java @@ -167,7 +169,7 @@ In particular, I discuss problems related to [compilation](#21-compilation-issue #### 2.1.1 file not found -Since class names can be specified using both slashes and dots, one may be tempted to also use dots in paths to Java source files in calls to the Java compiler: +Since the Java interpreter accepts class names with slashes and dots, one may be tempted to use dots in paths to Java source files in calls to the Java compiler: ```shell javac src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java @@ -328,7 +330,8 @@ java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.class java out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.class ``` -To solve the above problem, do not include the `.class` extension when referring to Java classes and ensure execution takes place from the proper path. +To solve the above problem, do not include the `.class` extension when referring to Java classes. +In addition, ensure execution takes place from the proper path. For the current project, these two conditions are fulfilled for the following command: ```shell From c54b058bbc726850d8057ed7fb5e1421a24f549e Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 20:12:30 +0200 Subject: [PATCH 45/68] Improve the description of compilation issues --- java/basic_java_project_cli/README.md | 51 ++++++++------------------- 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 77f7836..edb09eb 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -172,13 +172,14 @@ In particular, I discuss problems related to [compilation](#21-compilation-issue Since the Java interpreter accepts class names with slashes and dots, one may be tempted to use dots in paths to Java source files in calls to the Java compiler: ```shell -javac src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java +cd src +javac nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java ``` Such commands will, however, result in an error of the form: ```text -error: file not found: src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java +error: file not found: nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java Usage: javac use --help for a list of possible options ``` @@ -189,6 +190,14 @@ To solve this problem, use slashes when a file path to a source file is expected javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java ``` +Alternatively, ensure that you called the right tool. +You may have wanted to call the Java interpreter instead of the Java compiler. +In that case, ensure you drop any file extensions and you execute the class from the proper location: + +```shell +java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +``` + #### 2.1.2 Class names are only accepted if annotation processing is explicitly requested When working with Java, you may encounter errors of the following form: @@ -203,11 +212,6 @@ error: Class names, 'nl.mauritssilvis.challenges.java.cli.general.projects.basic 1 error ``` -```text -error: Class names, 'src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main', are only accepted if annotation processing is explicitly requested -1 error -``` - Such errors respectively result from using the Java compiler in commands of the form: ```shell @@ -220,10 +224,6 @@ cd src javac nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main ``` -```shell -javac src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main -``` - Here, the Java compiler is supplied with class-name-like paths. To solve the above problem, do not supply the Java compiler with a class-name-like path when a file path to a source file is expected. @@ -242,7 +242,7 @@ java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main #### 2.1.3 invalid flag -When working with Java, you may encounter errors of the following form: +When working with Java, you may encounter an error of the following form: ```text error: invalid flag: Main.class @@ -250,36 +250,15 @@ Usage: javac use --help for a list of possible options ``` -```text -error: invalid flag: nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class -Usage: javac -use --help for a list of possible options -``` - -```text -error: invalid flag: src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class -Usage: javac -use --help for a list of possible options -``` - -Such errors respectively result from using the Java compiler with commands of the form: +Such an error may result from calling the Java compiler on a Java class file: ```shell cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic javac Main.class ``` -```shell -cd src -javac nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class -``` - -```shell -javac src.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class -``` - -To solve issues of the above form, do not provide the Java compiler with a class-name-like path when a file path to a source file is expected. -In addition, ensure the `.java` file extension (instead of `.class`) is present in paths to source files: +To solve the above problem, do not provide the Java compiler with the path of a class file when a path path to a source file is expected. +In addition, ensure the `.java` file extension (instead of `.class`) is used in paths to source files: ```shell javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java From 94f0cefb3df1d81f1049212d329a42e584dbea5e Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 20:19:26 +0200 Subject: [PATCH 46/68] Improve the description of compilation issues --- java/basic_java_project_cli/README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index edb09eb..f48fda4 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -192,7 +192,7 @@ javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Mai Alternatively, ensure that you called the right tool. You may have wanted to call the Java interpreter instead of the Java compiler. -In that case, ensure you drop any file extensions and you execute the class from the proper location: +In that case, ensure you drop any file extensions, you use the fully qualified class name and you execute the class from the proper location: ```shell java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main @@ -224,9 +224,9 @@ cd src javac nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main ``` -Here, the Java compiler is supplied with class-name-like paths. +Here, the Java compiler is supplied with class names. -To solve the above problem, do not supply the Java compiler with a class-name-like path when a file path to a source file is expected. +To solve the above problem, do not supply the Java compiler with a class name when a path to a source file is expected. In addition, ensure the `.java` file extension is present in paths to source files: ```shell @@ -234,7 +234,8 @@ javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Mai ``` Alternatively, ensure that you called the right tool. -You may have wanted to call the Java interpreter instead of the Java compiler: +You may have wanted to call the Java interpreter instead of the Java compiler +In that case, ensure you use the fully qualified class name and you execute the class from the proper location: ```shell java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main @@ -257,7 +258,7 @@ cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic javac Main.class ``` -To solve the above problem, do not provide the Java compiler with the path of a class file when a path path to a source file is expected. +To solve the above problem, do not provide the Java compiler with the path of a class file when a path to a source file is expected. In addition, ensure the `.java` file extension (instead of `.class`) is used in paths to source files: ```shell @@ -266,7 +267,7 @@ javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Mai Alternatively, ensure that you called the right tool. You may have wanted to call the Java interpreter instead of the Java compiler. -In that case, note that the `.class` extension of the Java class file name has to be omitted to refer to the class: +In that case, ensure you drop any file extensions, you use the fully qualified class name and you execute the class from the proper location: ```shell java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main From 37edbb0ee5b02bbf662d8cedd2b6bf910cfeab5d Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Wed, 4 May 2022 20:28:29 +0200 Subject: [PATCH 47/68] Simplify the description of execution issues --- java/basic_java_project_cli/README.md | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index f48fda4..b6c6ea4 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -284,32 +284,13 @@ Error: Could not find or load main class Main.class Caused by: java.lang.ClassNotFoundException: Main.class ``` -```text -Error: Could not find or load main class nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class -Caused by: java.lang.ClassNotFoundException: nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class -``` - -```text -Error: Could not find or load main class out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class -Caused by: java.lang.ClassNotFoundException: out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.class -``` - -Such errors result from calling the Java interpreter using commands of the form: +Such errors result from calling the Java interpreter on class files: ```shell cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic java Main.class ``` -```shell -cd out -java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.class -``` - -```shell -java out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.class -``` - To solve the above problem, do not include the `.class` extension when referring to Java classes. In addition, ensure execution takes place from the proper path. For the current project, these two conditions are fulfilled for the following command: From 00381c96d3ce6709cfc3310883f740309456bc2d Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Sat, 1 Apr 2023 18:00:05 +0200 Subject: [PATCH 48/68] Remove end-of-line whitespace --- java/basic_java_project_cli/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index b6c6ea4..20212e2 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -19,7 +19,7 @@ I describe this programming language in the [Java section](../#21-java) of [Chal #### 1.1.1 Code -For demonstrative purposes, the current project contains only a single Java file, [Main.java](src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java), which comprises a Java class with the corresponding name `Main`. +For demonstrative purposes, the current project contains only a single Java file, [Main.java](src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java), which comprises a Java class with the corresponding name `Main`. This class contains a `main` method outputting a well-known message: ```java From 579b5ce2c747892348d6c6ae7e1c7ece32001cb8 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Sat, 1 Apr 2023 23:13:19 +0200 Subject: [PATCH 49/68] Use absolute out-of-project references --- java/basic_java_project_cli/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/basic_java_project_cli/README.md b/java/basic_java_project_cli/README.md index 20212e2..a2eab90 100644 --- a/java/basic_java_project_cli/README.md +++ b/java/basic_java_project_cli/README.md @@ -4,7 +4,7 @@ ## Introduction -With this part of the [Challenges > Java](..) project, I provide the code and settings for a basic Java project, set up using the command line. +With this part of the [Challenges](https://github.com/mauritssilvis/challenges) > [Java](https://github.com/mauritssilvis/challenges/tree/main/java) project, I provide the code and settings for a basic Java project, set up using the command line. Below, I give detailed [background information](#1-background) on the project's [code](#11-java). In addition, I list several [issues](#2-issues-and-solutions) that can occur when setting up a Java project. @@ -15,7 +15,7 @@ I also describe solutions to these issues. ### 1.1 Java This project makes use of Java. -I describe this programming language in the [Java section](../#21-java) of [Challenges > Java](..). +I describe this programming language in the [Java section](https://github.com/mauritssilvis/challenges/tree/main/java#21-java) of the [Challenges](https://github.com/mauritssilvis/challenges) > [Java](https://github.com/mauritssilvis/challenges/tree/main/java) project. #### 1.1.1 Code From 63dae907e31bad626a7aff5af29a07bb641caeda Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Sun, 2 Apr 2023 12:24:22 +0200 Subject: [PATCH 50/68] Rename project folder --- .../{basic_java_project_cli => basic-java-project-cli}/.gitignore | 0 java/{basic_java_project_cli => basic-java-project-cli}/README.md | 0 .../mauritssilvis/challenges/java/cli/general/projects/basic/.dir | 0 .../problems.md | 0 .../challenges/java/cli/general/projects/basic/Main.java | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename java/{basic_java_project_cli => basic-java-project-cli}/.gitignore (100%) rename java/{basic_java_project_cli => basic-java-project-cli}/README.md (100%) rename java/{basic_java_project_cli => basic-java-project-cli}/out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/.dir (100%) rename java/{basic_java_project_cli => basic-java-project-cli}/problems.md (100%) rename java/{basic_java_project_cli => basic-java-project-cli}/src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java (100%) diff --git a/java/basic_java_project_cli/.gitignore b/java/basic-java-project-cli/.gitignore similarity index 100% rename from java/basic_java_project_cli/.gitignore rename to java/basic-java-project-cli/.gitignore diff --git a/java/basic_java_project_cli/README.md b/java/basic-java-project-cli/README.md similarity index 100% rename from java/basic_java_project_cli/README.md rename to java/basic-java-project-cli/README.md diff --git a/java/basic_java_project_cli/out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/.dir b/java/basic-java-project-cli/out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/.dir similarity index 100% rename from java/basic_java_project_cli/out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/.dir rename to java/basic-java-project-cli/out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/.dir diff --git a/java/basic_java_project_cli/problems.md b/java/basic-java-project-cli/problems.md similarity index 100% rename from java/basic_java_project_cli/problems.md rename to java/basic-java-project-cli/problems.md diff --git a/java/basic_java_project_cli/src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java b/java/basic-java-project-cli/src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java similarity index 100% rename from java/basic_java_project_cli/src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java rename to java/basic-java-project-cli/src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java From 6f5c08e962fb79ffe4c5bd980df9ddd0259fe2ee Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Sun, 2 Apr 2023 12:26:01 +0200 Subject: [PATCH 51/68] Fix references to the basic CLI Java project --- java/README.md | 2 +- java/basic-java-project-cli/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/java/README.md b/java/README.md index 8be334a..5c3d7f1 100644 --- a/java/README.md +++ b/java/README.md @@ -23,7 +23,7 @@ The challenges listed below were solved without a build tool. ##### Projects -* [Setting up a basic Java project using the command line](basic_java_project_cli) +* [Setting up a basic Java project using the command line](basic-java-project-cli) ### 1.2 IntelliJ IDEA projects diff --git a/java/basic-java-project-cli/README.md b/java/basic-java-project-cli/README.md index a2eab90..47d6f56 100644 --- a/java/basic-java-project-cli/README.md +++ b/java/basic-java-project-cli/README.md @@ -37,7 +37,7 @@ public class Main { The directory structure of this project is as follows: ```text -basic_java_project_cli +basic-java-project-cli ├─ out │ └─ nl │ └─ mauritssilvis From f1106adb5f56441d378e8d652412554e14df7044 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Sun, 2 Apr 2023 12:31:27 +0200 Subject: [PATCH 52/68] Shorten the project title --- java/basic-java-project-cli/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/basic-java-project-cli/README.md b/java/basic-java-project-cli/README.md index 47d6f56..23542fd 100644 --- a/java/basic-java-project-cli/README.md +++ b/java/basic-java-project-cli/README.md @@ -1,6 +1,6 @@ -# Challenges > Java > Setting up a basic Java project using the command line +# Setting up a basic Java project using the command line -> A basic Java project, set up using the command line +> A basic Java project set up using the command line ## Introduction From c2f1cd5c0924c6bf6a0f775d67d89902d229d1c4 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Sun, 2 Apr 2023 12:34:27 +0200 Subject: [PATCH 53/68] Update copyright notice --- java/basic-java-project-cli/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/basic-java-project-cli/README.md b/java/basic-java-project-cli/README.md index 23542fd..67d185a 100644 --- a/java/basic-java-project-cli/README.md +++ b/java/basic-java-project-cli/README.md @@ -347,6 +347,6 @@ java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main ## License -Copyright © 2021, 2022 Maurits H. Silvis +Copyright © 2021–2023 Maurits Silvis This source code package is subject to the terms and conditions defined in the GNU General Public License v3.0, which can be found in the file [LICENSE.md](../../LICENSE.md), or later. From 35de556dae30b8cd0f38a2a9d99afec3c803ff83 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Sun, 2 Apr 2023 12:36:50 +0200 Subject: [PATCH 54/68] Remove to-do file --- java/basic-java-project-cli/problems.md | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 java/basic-java-project-cli/problems.md diff --git a/java/basic-java-project-cli/problems.md b/java/basic-java-project-cli/problems.md deleted file mode 100644 index 395fa0f..0000000 --- a/java/basic-java-project-cli/problems.md +++ /dev/null @@ -1,5 +0,0 @@ -# Problems - -## Java execution problems - -### Problem: Error: Invalid or corrupt jarfile From e60dc3ff81c79dd0643369a97e07ae1ebcdf803f Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Sun, 2 Apr 2023 12:39:34 +0200 Subject: [PATCH 55/68] Remove newline --- java/basic-java-project-cli/.gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/java/basic-java-project-cli/.gitignore b/java/basic-java-project-cli/.gitignore index 5282a50..fbe5047 100644 --- a/java/basic-java-project-cli/.gitignore +++ b/java/basic-java-project-cli/.gitignore @@ -1,4 +1,3 @@ - # Created by https://gitignore.io/api/java # Edit at https://gitignore.io?templates=java From ea9ade896990a98ae283c69549644825fd42ddc7 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 2 Jun 2023 15:06:40 +0200 Subject: [PATCH 56/68] Rename folder without build tool --- java/basic-java-project-cli/README.md | 70 +++++++++---------- .../{general => manual}/projects/basic/.dir | 0 .../projects/basic/Main.java | 2 +- 3 files changed, 36 insertions(+), 36 deletions(-) rename java/basic-java-project-cli/out/nl/mauritssilvis/challenges/java/cli/{general => manual}/projects/basic/.dir (100%) rename java/basic-java-project-cli/src/nl/mauritssilvis/challenges/java/cli/{general => manual}/projects/basic/Main.java (75%) diff --git a/java/basic-java-project-cli/README.md b/java/basic-java-project-cli/README.md index 67d185a..c6175bd 100644 --- a/java/basic-java-project-cli/README.md +++ b/java/basic-java-project-cli/README.md @@ -19,11 +19,11 @@ I describe this programming language in the [Java section](https://github.com/ma #### 1.1.1 Code -For demonstrative purposes, the current project contains only a single Java file, [Main.java](src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java), which comprises a Java class with the corresponding name `Main`. +For demonstrative purposes, the current project contains only a single Java file, [Main.java](src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java), which comprises a Java class with the corresponding name `Main`. This class contains a `main` method outputting a well-known message: ```java -package nl.mauritssilvis.challenges.java.cli.general.projects.basic; +package nl.mauritssilvis.challenges.java.cli.manual.projects.basic; public class Main { public static void main(String[] args) { @@ -44,7 +44,7 @@ basic-java-project-cli │ └─ challenges │ └─ java │ └─ cli -│ └─ general +│ └─ manual │ └─ projects │ └─ basic │ └─ Main.class @@ -55,7 +55,7 @@ basic-java-project-cli │ └─ challenges │ └─ java │ └─ cli -│ └─ general +│ └─ manual │ └─ projects │ └─ basic │ └─ Main.java @@ -64,7 +64,7 @@ basic-java-project-cli └─ README.md ``` -Here, the `src` folder contains the source file, `Main.java`, stored in a directory structure that matches the package of the class `Main`, i.e., `nl.mauritssilvis.challenges.java.cli.general.projects.basic`. +Here, the `src` folder contains the source file, `Main.java`, stored in a directory structure that matches the package of the class `Main`, i.e., `nl.mauritssilvis.challenges.java.cli.manual.projects.basic`. The `out` folder, which is created upon [compilation](#113-compilation), has the same structure as the `src` folder, but is there to contain the compiled bytecode instead of source code. #### 1.1.3 Compilation @@ -72,7 +72,7 @@ The `out` folder, which is created upon [compilation](#113-compilation), has the The most basic way to compile the Java source code in `Main.java` consists in navigating to the directory of this file and calling the Java compiler: ```shell -cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic +cd src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic javac Main.java ``` @@ -83,7 +83,7 @@ Moreover, Java source and class files are generally stored in different location Therefore, it is more convenient to compile the source file from the project root folder with a `-d` option that specifies the output directory: ```shell -javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +javac -d out src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java ``` #### 1.1.4 Execution @@ -94,7 +94,7 @@ A convenient way to execute this class is given by: ```shell cd out -java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +java nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main ``` As is explained below, the `.class` extension of the class file should not be included. @@ -103,14 +103,14 @@ Equivalently, one could execute the following commands: ```shell cd out -java nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +java nl.mauritssilvis.challenges.java.cli.manual.projects.basic.Main ``` Rather than manually navigating to a folder containing class files, it is more common to execute a Java class relative to a so-called class path. For the current project, the following command involving the `-cp` option executes the previously compiled class from the class path `out`: ```shell -java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +java -cp out nl.mauritssilvis.challenges.java.cli.manual.projects.basic.Main ``` All above commands will result in the following output: @@ -126,7 +126,7 @@ Moreover, for classes that are part of a package, the supplied class name should For the current project, this fully qualified class name is: ```text -nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +nl.mauritssilvis.challenges.java.cli.manual.projects.basic.Main ``` As the above shows, fully qualified class names can be provided to the Java interpreter using both slashes and dots. @@ -143,14 +143,14 @@ Since Java 11, programs consisting of only a single source file can be executed For the current project, the following command directly executes the source file `Main.java` from its containing folder: ```shell -cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic +cd src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic java Main.java ``` Alternatively, direct execution from the main project folder is possible: ```bash -java src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +java src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java ``` As expected, the output of the above commands is: @@ -173,13 +173,13 @@ Since the Java interpreter accepts class names with slashes and dots, one may be ```shell cd src -javac nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java +javac nl.mauritssilvis.challenges.java.cli.manual.projects.basic.Main.java ``` Such commands will, however, result in an error of the form: ```text -error: file not found: nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main.java +error: file not found: nl.mauritssilvis.challenges.java.cli.manual.projects.basic.Main.java Usage: javac use --help for a list of possible options ``` @@ -187,7 +187,7 @@ use --help for a list of possible options To solve this problem, use slashes when a file path to a source file is expected: ```shell -javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +javac -d out src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java ``` Alternatively, ensure that you called the right tool. @@ -195,7 +195,7 @@ You may have wanted to call the Java interpreter instead of the Java compiler. In that case, ensure you drop any file extensions, you use the fully qualified class name and you execute the class from the proper location: ```shell -java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +java -cp out nl.mauritssilvis.challenges.java.cli.manual.projects.basic.Main ``` #### 2.1.2 Class names are only accepted if annotation processing is explicitly requested @@ -208,20 +208,20 @@ error: Class names, 'Main', are only accepted if annotation processing is explic ``` ```text -error: Class names, 'nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main', are only accepted if annotation processing is explicitly requested +error: Class names, 'nl.mauritssilvis.challenges.java.cli.manual.projects.basic.Main', are only accepted if annotation processing is explicitly requested 1 error ``` Such errors respectively result from using the Java compiler in commands of the form: ```shell -cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic +cd src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic javac Main ``` ```shell cd src -javac nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +javac nl.mauritssilvis.challenges.java.cli.manual.projects.basic.Main ``` Here, the Java compiler is supplied with class names. @@ -230,7 +230,7 @@ To solve the above problem, do not supply the Java compiler with a class name wh In addition, ensure the `.java` file extension is present in paths to source files: ```shell -javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +javac -d out src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java ``` Alternatively, ensure that you called the right tool. @@ -238,7 +238,7 @@ You may have wanted to call the Java interpreter instead of the Java compiler In that case, ensure you use the fully qualified class name and you execute the class from the proper location: ```shell -java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +java -cp out nl.mauritssilvis.challenges.java.cli.manual.projects.basic.Main ``` #### 2.1.3 invalid flag @@ -254,7 +254,7 @@ use --help for a list of possible options Such an error may result from calling the Java compiler on a Java class file: ```shell -cd src/nl/mauritssilvis/challenges/java/cli/general/projects/basic +cd src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic javac Main.class ``` @@ -262,7 +262,7 @@ To solve the above problem, do not provide the Java compiler with the path of a In addition, ensure the `.java` file extension (instead of `.class`) is used in paths to source files: ```shell -javac -d out src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +javac -d out src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java ``` Alternatively, ensure that you called the right tool. @@ -270,7 +270,7 @@ You may have wanted to call the Java interpreter instead of the Java compiler. In that case, ensure you drop any file extensions, you use the fully qualified class name and you execute the class from the proper location: ```shell -java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +java -cp out nl.mauritssilvis.challenges.java.cli.manual.projects.basic.Main ``` ### 2.2 Execution issues @@ -287,7 +287,7 @@ Caused by: java.lang.ClassNotFoundException: Main.class Such errors result from calling the Java interpreter on class files: ```shell -cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic +cd out/nl/mauritssilvis/challenges/java/cli/manual/projects/basic java Main.class ``` @@ -297,13 +297,13 @@ For the current project, these two conditions are fulfilled for the following co ```shell cd out -java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +java nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main ``` As noted before, a more common way of writing this command is given by: ```shell -java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +java -cp out nl.mauritssilvis.challenges.java.cli.manual.projects.basic.Main ``` #### 2.2.2 Could not find or load main class / java.lang.NoClassDefFoundError @@ -312,23 +312,23 @@ When trying to execute Java programs, you may encounter errors of the following ```text Error: Could not find or load main class Main -Caused by: java.lang.NoClassDefFoundError: nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main (wrong name: Main) +Caused by: java.lang.NoClassDefFoundError: nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main (wrong name: Main) ``` ```text -Error: Could not find or load main class out.nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main -Caused by: java.lang.NoClassDefFoundError: nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main (wrong name: out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main) +Error: Could not find or load main class out.nl.mauritssilvis.challenges.java.cli.manual.projects.basic.Main +Caused by: java.lang.NoClassDefFoundError: nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main (wrong name: out/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main) ``` Such errors result from calling the Java interpreter using commands of the form: ```shell -cd out/nl/mauritssilvis/challenges/java/cli/general/projects/basic +cd out/nl/mauritssilvis/challenges/java/cli/manual/projects/basic java Main ``` ```shell -java out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +java out/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main ``` To solve the above problem, ensure that classes that belong to a package are executed from a folder that contains a directory structure corresponding to the package name. @@ -336,13 +336,13 @@ For the current project, the following command could be used: ```shell cd out -java nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main +java nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main ``` As noted before, a more common way of writing this command is given by: ```shell -java -cp out nl.mauritssilvis.challenges.java.cli.general.projects.basic.Main +java -cp out nl.mauritssilvis.challenges.java.cli.manual.projects.basic.Main ``` ## License diff --git a/java/basic-java-project-cli/out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/.dir b/java/basic-java-project-cli/out/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/.dir similarity index 100% rename from java/basic-java-project-cli/out/nl/mauritssilvis/challenges/java/cli/general/projects/basic/.dir rename to java/basic-java-project-cli/out/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/.dir diff --git a/java/basic-java-project-cli/src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java b/java/basic-java-project-cli/src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java similarity index 75% rename from java/basic-java-project-cli/src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java rename to java/basic-java-project-cli/src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java index 10aaa13..99304e3 100644 --- a/java/basic-java-project-cli/src/nl/mauritssilvis/challenges/java/cli/general/projects/basic/Main.java +++ b/java/basic-java-project-cli/src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ -package nl.mauritssilvis.challenges.java.cli.general.projects.basic; +package nl.mauritssilvis.challenges.java.cli.manual.projects.basic; public class Main { public static void main(String[] args) { From 2c07ab78e10759c9407cbdac7180a4dfecb9f74a Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 2 Jun 2023 15:17:31 +0200 Subject: [PATCH 57/68] Rename the basic CLI project --- java/README.md | 2 +- java/basic-java-project-cli/README.md | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/java/README.md b/java/README.md index 5c3d7f1..f501a4e 100644 --- a/java/README.md +++ b/java/README.md @@ -23,7 +23,7 @@ The challenges listed below were solved without a build tool. ##### Projects -* [Setting up a basic Java project using the command line](basic-java-project-cli) +* [Setting up a Java project using the command line](basic-java-project-cli) ### 1.2 IntelliJ IDEA projects diff --git a/java/basic-java-project-cli/README.md b/java/basic-java-project-cli/README.md index c6175bd..97d3dad 100644 --- a/java/basic-java-project-cli/README.md +++ b/java/basic-java-project-cli/README.md @@ -1,10 +1,10 @@ -# Setting up a basic Java project using the command line +# Setting up a Java project using the command line -> A basic Java project set up using the command line +> A Java project set up using the command line ## Introduction -With this part of the [Challenges](https://github.com/mauritssilvis/challenges) > [Java](https://github.com/mauritssilvis/challenges/tree/main/java) project, I provide the code and settings for a basic Java project, set up using the command line. +With this part of the [Challenges](https://github.com/mauritssilvis/challenges) > [Java](https://github.com/mauritssilvis/challenges/tree/main/java) project, I provide the code and settings for a Java project, set up using the command line. Below, I give detailed [background information](#1-background) on the project's [code](#11-java). In addition, I list several [issues](#2-issues-and-solutions) that can occur when setting up a Java project. From d28b38c81a119d8937a84b34bf03c05d9edd815c Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 2 Jun 2023 15:18:34 +0200 Subject: [PATCH 58/68] Rename the corresponding folder --- java/README.md | 2 +- java/{basic-java-project-cli => java-project-cli}/.gitignore | 0 java/{basic-java-project-cli => java-project-cli}/README.md | 2 +- .../challenges/java/cli/manual/projects/basic/.dir | 0 .../challenges/java/cli/manual/projects/basic/Main.java | 0 5 files changed, 2 insertions(+), 2 deletions(-) rename java/{basic-java-project-cli => java-project-cli}/.gitignore (100%) rename java/{basic-java-project-cli => java-project-cli}/README.md (99%) rename java/{basic-java-project-cli => java-project-cli}/out/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/.dir (100%) rename java/{basic-java-project-cli => java-project-cli}/src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java (100%) diff --git a/java/README.md b/java/README.md index f501a4e..9b7b93c 100644 --- a/java/README.md +++ b/java/README.md @@ -23,7 +23,7 @@ The challenges listed below were solved without a build tool. ##### Projects -* [Setting up a Java project using the command line](basic-java-project-cli) +* [Setting up a Java project using the command line](java-project-cli) ### 1.2 IntelliJ IDEA projects diff --git a/java/basic-java-project-cli/.gitignore b/java/java-project-cli/.gitignore similarity index 100% rename from java/basic-java-project-cli/.gitignore rename to java/java-project-cli/.gitignore diff --git a/java/basic-java-project-cli/README.md b/java/java-project-cli/README.md similarity index 99% rename from java/basic-java-project-cli/README.md rename to java/java-project-cli/README.md index 97d3dad..b7603ba 100644 --- a/java/basic-java-project-cli/README.md +++ b/java/java-project-cli/README.md @@ -37,7 +37,7 @@ public class Main { The directory structure of this project is as follows: ```text -basic-java-project-cli +java-project-cli ├─ out │ └─ nl │ └─ mauritssilvis diff --git a/java/basic-java-project-cli/out/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/.dir b/java/java-project-cli/out/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/.dir similarity index 100% rename from java/basic-java-project-cli/out/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/.dir rename to java/java-project-cli/out/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/.dir diff --git a/java/basic-java-project-cli/src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java b/java/java-project-cli/src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java similarity index 100% rename from java/basic-java-project-cli/src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java rename to java/java-project-cli/src/nl/mauritssilvis/challenges/java/cli/manual/projects/basic/Main.java From 5a1ee204ff55e0a2a8919cdcd8ccf9dfa4f1add6 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 2 Jun 2023 16:26:21 +0200 Subject: [PATCH 59/68] Condense the project overview --- java/README.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/java/README.md b/java/README.md index 9b7b93c..885cd9a 100644 --- a/java/README.md +++ b/java/README.md @@ -21,8 +21,6 @@ I solved the challenges in this section using the [command line](#22-bash). The challenges listed below were solved without a build tool. -##### Projects - * [Setting up a Java project using the command line](java-project-cli) ### 1.2 IntelliJ IDEA projects @@ -33,18 +31,11 @@ For others, I used [Maven](#122-maven). #### 1.2.1 General -##### Projects - * [Setting up a basic Java project using IntelliJ IDEA](basic-java-project-intellij) #### 1.2.2 Maven -##### Projects - * [Setting up a basic Maven project using IntelliJ IDEA](basic-maven-project-intellij) - -##### JARs - * [Creating an executable JAR using Maven](executable-jar-maven-intellij) ## 2. Background From 8b89cc60cdef1e785efca3bdec569bee5715df69 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 2 Jun 2023 16:29:09 +0200 Subject: [PATCH 60/68] Rename the no-build-tool section --- java/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/java/README.md b/java/README.md index 885cd9a..c16812b 100644 --- a/java/README.md +++ b/java/README.md @@ -17,7 +17,7 @@ Currently, Java solutions are available for the following challenges, categorize I solved the challenges in this section using the [command line](#22-bash). -#### 1.1.1 General +#### 1.1.1 Manual The challenges listed below were solved without a build tool. @@ -26,10 +26,10 @@ The challenges listed below were solved without a build tool. ### 1.2 IntelliJ IDEA projects I solved the following challenges using [IntelliJ IDEA](#23-intellij-idea). -For some challenges, I did not use a build tool ([general](#121-general)). +For some challenges, I did not use a build tool ([manual](#121-manual)). For others, I used [Maven](#122-maven). -#### 1.2.1 General +#### 1.2.1 Manual * [Setting up a basic Java project using IntelliJ IDEA](basic-java-project-intellij) From 01504e4b3091c62fba2a7c6067288acbad462035 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 2 Jun 2023 16:30:58 +0200 Subject: [PATCH 61/68] Use relative links --- java/java-project-cli/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/java-project-cli/README.md b/java/java-project-cli/README.md index b7603ba..702d39c 100644 --- a/java/java-project-cli/README.md +++ b/java/java-project-cli/README.md @@ -4,7 +4,7 @@ ## Introduction -With this part of the [Challenges](https://github.com/mauritssilvis/challenges) > [Java](https://github.com/mauritssilvis/challenges/tree/main/java) project, I provide the code and settings for a Java project, set up using the command line. +With this part of the [Challenges](../..) > [Java](..) project, I provide the code and settings for a Java project, set up using the command line. Below, I give detailed [background information](#1-background) on the project's [code](#11-java). In addition, I list several [issues](#2-issues-and-solutions) that can occur when setting up a Java project. @@ -15,7 +15,7 @@ I also describe solutions to these issues. ### 1.1 Java This project makes use of Java. -I describe this programming language in the [Java section](https://github.com/mauritssilvis/challenges/tree/main/java#21-java) of the [Challenges](https://github.com/mauritssilvis/challenges) > [Java](https://github.com/mauritssilvis/challenges/tree/main/java) project. +I describe this programming language in the [Java section](../#21-java) of the [Challenges](../..) > [Java](..) project. #### 1.1.1 Code From f0ec19b8584e12ab32ad4ace3eb4122d96d19b1c Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 2 Jun 2023 16:48:12 +0200 Subject: [PATCH 62/68] Document changes --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5563259..179bb9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to the [Challenges](https://github.com/mauritssilvis/challen The file format is based on [keep a changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [semantic versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- Add a Java project set up using the command line. + ## [0.2.0] - 2023-06-02 ### Changed From 098c410952fffe7f0642148b40ae732d43187d77 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 2 Jun 2023 16:50:21 +0200 Subject: [PATCH 63/68] Shorten the Java project overview --- java/README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/java/README.md b/java/README.md index 4e23b93..8fa302f 100644 --- a/java/README.md +++ b/java/README.md @@ -15,11 +15,7 @@ Currently, Java solutions are available for the following challenges, categorize ### 1.1 Command-line projects -I solved the challenges in this section using the [command line](#22-bash). - -#### 1.1.1 Manual - -The challenges listed below were solved without a build tool. +I solved the challenges in this section using the [command line](#22-bash) but without a build tool. * [Setting up a Java project using the command line](java-project-cli) From df19df0598f8f02ac4b6d4f40298c807b57439f7 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 2 Jun 2023 17:26:44 +0200 Subject: [PATCH 64/68] Describe Bash --- java/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/java/README.md b/java/README.md index 8fa302f..dd60503 100644 --- a/java/README.md +++ b/java/README.md @@ -52,7 +52,9 @@ These projects require the installation of the Java Development Kit (JDK) 20 or ### 2.2 Bash -TODO +[Bash](https://www.gnu.org/software/bash/) is a commonly-used command-line interface for Unix-like operating systems. +Bash can execute both user-provided commands and so-called shell scripts. +Windows users can obtain Bash by installing [Git for Windows](https://git-scm.com/download/win). ### 2.3 IntelliJ IDEA From ed7ed57351fce10794652c910f9a811f8c27f097 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 2 Jun 2023 17:30:24 +0200 Subject: [PATCH 65/68] Add a reference to Bash --- java/java-project-cli/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/java/java-project-cli/README.md b/java/java-project-cli/README.md index 702d39c..7f0a83a 100644 --- a/java/java-project-cli/README.md +++ b/java/java-project-cli/README.md @@ -16,6 +16,8 @@ I also describe solutions to these issues. This project makes use of Java. I describe this programming language in the [Java section](../#21-java) of the [Challenges](../..) > [Java](..) project. +The commands in this section can, for example, be executed using Bash. +I describe this command-line interface in the [Bash section](../#22-bash) of the [Challenges](../..) > [Java](..) project. #### 1.1.1 Code From b792120e94630b82f159b260245a8f9afe1828ce Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 2 Jun 2023 17:33:50 +0200 Subject: [PATCH 66/68] Move the reference to Bash --- java/java-project-cli/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/java/java-project-cli/README.md b/java/java-project-cli/README.md index 7f0a83a..663b628 100644 --- a/java/java-project-cli/README.md +++ b/java/java-project-cli/README.md @@ -10,14 +10,15 @@ Below, I give detailed [background information](#1-background) on the project's In addition, I list several [issues](#2-issues-and-solutions) that can occur when setting up a Java project. I also describe solutions to these issues. +Listed commands can, for example, be executed using Bash. +I describe this command-line interface in the [Bash section](../#22-bash) of the [Challenges](../..) > [Java](..) project. + ## 1. Background ### 1.1 Java This project makes use of Java. I describe this programming language in the [Java section](../#21-java) of the [Challenges](../..) > [Java](..) project. -The commands in this section can, for example, be executed using Bash. -I describe this command-line interface in the [Bash section](../#22-bash) of the [Challenges](../..) > [Java](..) project. #### 1.1.1 Code From db833326b2f2eba74cb83169bd253e318531c19a Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 2 Jun 2023 17:36:06 +0200 Subject: [PATCH 67/68] Replace double spaces --- java/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/README.md b/java/README.md index dd60503..0102de5 100644 --- a/java/README.md +++ b/java/README.md @@ -52,7 +52,7 @@ These projects require the installation of the Java Development Kit (JDK) 20 or ### 2.2 Bash -[Bash](https://www.gnu.org/software/bash/) is a commonly-used command-line interface for Unix-like operating systems. +[Bash](https://www.gnu.org/software/bash/) is a commonly-used command-line interface for Unix-like operating systems. Bash can execute both user-provided commands and so-called shell scripts. Windows users can obtain Bash by installing [Git for Windows](https://git-scm.com/download/win). From d8845a324236d05f1cb99865aca48864961024a9 Mon Sep 17 00:00:00 2001 From: Maurits Silvis Date: Fri, 2 Jun 2023 17:38:09 +0200 Subject: [PATCH 68/68] Set the latest release version --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 179bb9a..090cef8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to the [Challenges](https://github.com/mauritssilvis/challen The file format is based on [keep a changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [semantic versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] +## [0.3.0] - 2023-06-02 ### Added