How to compile and run Java Projects with java and javac commands

Compiling

-d is output directory for new generated class files.

How to compile single Java source file

javac -d ./target/classes src/main/java/com/extuni/enum_demo4/App.java

How to compile multiple Java source files

javac -d ./target/classes 
./src/main/java/com/extuni/enum_demo4/App.java 
./src/main/java/com/extuni/enum_demo4/Greeting.java 
./src/main/java/com/extuni/enum_demo4/Helper.java

How to compile multiple Java source files using wildcard

javac -d ./target/classes ./src/main/**/*.java

How to specify dependency jar file when compiling

Trying to compile ./src folder completely will throw exception due to junit.jar file dependency. So, it should be specified using -cp option which means –classpath as below.

javac -d ./target/classes -cp /Users/kenanhancer/.m2/repository/junit/junit/4.11/junit-4.11.jar ./src/**/*.java
Continue reading

How to create and run Java project with Maven

You can find more details in https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

1. Creating a Java Project with Maven

mvn archetype:generate 
    -DgroupId={project-package-name}
    -DartifactId={project-name}
    -DarchetypeArtifactId={maven-template-name}
    -DinteractiveMode=false
    -DarchetypeVersion=1.4
for example
mvn archetype:generate 
    -DgroupId=com.extuni.enum_demo4 
    -DartifactId=enum_demo4 
    -DarchetypeArtifactId=maven-archetype-quickstart 
    -DinteractiveMode=false 
    -DarchetypeVersion=1.4
Maven Directory Layout
Continue reading

Java String

What is String in Java?

Java String is a sequence of characters.

Java String variable contains a collection of characters surrounded by double quotes.

An array of characters works same as Java String.

Java Strings are used for storing text.

In Java, Strings are immutable or final or constant which means each time when we are creating or editing or performing any method to change string, one string created in "String Constant Pool"

Immutable strings eliminate race conditions in multi-threaded applications.

How to create a String in Java?

There are two ways to create a String object:

  1. By string literal
  2. By new keyword

1- String literal

String str1 = "Hello World!";
String str2 = "Hello World!";

Each time you create a string literal, the JVM checks the "string constant pool" first. If the string already exists in the pool, a reference to the pooled instance is returned. If the string doesn't exist in the pool, a new string instance is created and placed in the pool.

In other words, we don't create any String object using new keyword above. The JVM does that task for us, it create a String object. If the String object exist in the memory, it doesn't create a new String object rather it assigns the same old object to the new instance, that means even though we have two String instances above(str1 and str2) compiler only created one String object and assigned the same to both the instances.

For example there are 5 String instances that have same value, it means that in memory there is only one String object having the value and all the 5 String instances would be pointing to the same String object.

What if we want to have two different String object with the same String. For that we would need to create Strings using new keyword

2- Using new keyword

String str1 = new String("Hello World!");
String str2 = new String("Hello World!");

In such case, the JVM will create a new String object in normal (non-pool) heap memory.

Notice that JVM handles allocation of memory space for string variables efficiently, whenever we create a String variable and assign a String value with String Literal(double quotes) , JVM check value of String variable in String pool, if found, it returns reference of String object, if not found then it creates a new String object in String pool and return reference of it.

I found some images from google to represent. So, there are three images below.

Continue reading

How to install Java with Jabba

Runtime version managers make developers life easier. So, I tested different Java version managers. But, Jabba is the easiest one for me.

You can read previously posted How to install java articles.

Java Specification Versions:

Here are the major releases:

  1. Java 1.0 (January 1996) – The original version.
  2. Java 1.1 (February 1997) – Introduced several significant features, including the JavaBeans API.
  3. Java 2 (J2SE 1.2) (December 1998) – Introduced the Swing application framework.
  4. J2SE 1.3 (May 2000) – Introduced the HotSpot JVM.
  5. J2SE 1.4 (February 2002) – Introduced regular expressions, image I/O API, and more.
  6. Java SE 5 (J2SE 1.5) (September 2004) – Introduced generics, metadata annotations, enumerated types, and more.
  7. Java SE 6 (December 2006) – Introduced scripting support, JVM improvements, and more.
  8. Java SE 7 (July 2011) – Introduced the try-with-resources statement, the diamond operator, and more.
  9. Java SE 8 (March 2014) – Introduced lambdas, streams, and the java.time package.
  10. Java SE 9 (September 2017) – Introduced the module system.
  11. Java SE 10 (March 2018) – Introduced local-variable type inference (var).
  12. Java SE 11 (September 2018) – Introduced String::lines, isBlank, and other methods, plus the java.net.http HttpClient.
  13. Java SE 12 (March 2019) – Introduced switch expressions (preview) and more.
  14. Java SE 13 (September 2019) – Introduced text blocks (preview) and more enhancements.
  15. Java SE 14 (March 2020) – Introduced pattern matching for instanceof (preview) and other improvements.
  16. Java SE 15 (September 2020) – Continued the introduction and enhancement of several features.
  17. Java SE 16 (March 2021) – Introduced sealed classes (preview) and other enhancements.
  18. Java SE 17 (September 2021) – This version is an LTS (Long Term Support) release, which means it will receive updates for a longer period than the interim releases.
  19. Java SE 18 (March 2022)
  20. Java SE 19 (September 2022)
  21. Java SE 20 (March 2023)
  22. Java SE 21 (September 2023)

Java has moved to a time-driven release model, with new versions being released every six months. Not all of these are long-term support (LTS) versions. As of Java 17, the LTS versions have been Java 8, Java 11, and Java 17.

Installing Jabba

Click Jabba link to see more detailed information about Jabba

$ curl -sL https://github.com/shyiko/jabba/raw/master/install.sh | bash && . ~/.jabba/jabba.sh

Usage

# list available JDK's
$ jabba ls-remote

# you can use any valid semver range to narrow down the list
$ jabba ls-remote zulu@~1.8.60
$ jabba ls-remote "*@>=1.6.45 <1.9" --latest=minor

# install Oracle JDK
$ jabba install 1.8

# install Oracle Server JRE
$ jabba install sjre@1.8

# install Adopt OpenJDK (Hotspot)
$ jabba install adopt@1.8-0

# install Adopt OpenJDK (Eclipse OpenJ9)
$ jabba install adopt-openj9@1.9-0

# install Zulu OpenJDK
$ jabba install zulu@1.8
$ jabba install zulu@~1.8.144 # same as "zulu@>=1.8.144 <1.9"

# install IBM SDK, Java Technology Edition
$ jabba install ibm@1.8

# install GraalVM CE
$ jabba install graalvm@1.0-0

# install OpenJDK
$ jabba install openjdk@1.10-0

# install OpenJDK with Shenandoah GC
$ jabba install openjdk-shenandoah@1.10-0

# install from custom URL
# (supported qualifiers: zip (since 0.3.0), tgz, tgx (since 0.10.0), dmg, bin, exe)
$ jabba install 1.8.0-custom=tgz+http://example.com/distribution.tar.gz
$ jabba install 1.8.0-custom=tgx+http://example.com/distribution.tar.xz
$ jabba install 1.8.0-custom=zip+file:///opt/distribution.zip

# uninstall JDK
$ jabba uninstall zulu@1.6.77

# link system JDK
$ jabba link system@1.8.72 /Library/Java/JavaVirtualMachines/jdk1.8.0_72.jdk

# list all installed JDK's
$ jabba ls

# switch to a different version of JDK (it must be already `install`ed)
$ jabba use adopt@1.8
$ jabba use zulu@~1.6.97

$ echo "1.8" > .jabbarc
# switch to the JDK specified in .jabbarc (since 0.5.0)
$ jabba use

# set default java version on shell (since 0.2.0)
# this version will automatically be "jabba use"d every time you open up a new terminal
$ jabba alias default 1.8
Continue reading

How to install Java with brew and jenv

jenv is a tool that helps you switch between different java versions in your development machine. But, Jenv doesn't install Java for you.

Use your platform appropriate package manager to install Java. On macOS, brew is recommended.

Install Java with Homebrew

First, ensure that you have Homebrew installed in your system. Homebrew is a package manager for Mac OS. So if you want to learn more details about it, visit my blog below.

I also posted how to install Java JDK manually in the following post.

Actually I already mentioned how to install Java JDK in the above post, but I will shortly show one more time

$ brew tap adoptopenjdk/openjdk
$ brew search jdk

==> Formulae
openjdk                                                                              openjdk@11

==> Casks
adoptopenjdk
adoptopenjdk12
adoptopenjdk13-openj9
adoptopenjdk14-openj9-jre-large
adoptopenjdk9
adoptopenjdk10
adoptopenjdk12-jre
adoptopenjdk13-openj9-jre
adoptopenjdk14-openj9-large
jdk-mission-control
adoptopenjdk11
adoptopenjdk12-openj9
adoptopenjdk13-openj9-jre-large
adoptopenjdk8
oracle-jdk
adoptopenjdk11-jre
adoptopenjdk12-openj9-jre
adoptopenjdk13-openj9-large
adoptopenjdk8-jre
oracle-jdk-javadoc
adoptopenjdk11-openj9
adoptopenjdk12-openj9-jre-large
adoptopenjdk14
adoptopenjdk8-openj9
sapmachine-jdk
adoptopenjdk11-openj9-jre
adoptopenjdk12-openj9-large
adoptopenjdk14-jre
adoptopenjdk8-openj9-jre
adoptopenjdk11-openj9-jre-large
adoptopenjdk13
adoptopenjdk14-openj9
adoptopenjdk8-openj9-jre-large
adoptopenjdk11-openj9-large
adoptopenjdk13-jre
adoptopenjdk14-openj9-jre
adoptopenjdk8-openj9-large
$ brew cask install adoptopenjdk
$ java --version

openjdk 14.0.1 2020-04-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.1+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.1+7, mixed mode, sharing)
Continue reading

How to install Maven

Downloading and Installing Maven

if you want to download Maven manually, download it from http://maven.apache.org/download.cgi

But you can download Maven programmatically as the following. So, run below script in your terminal.

wget https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz

tar -xzvf apache-maven*bin.tar.gz

# Fix the permissions:
# chown -R root:wheel Downloads/apache-maven*

mkdir /usr/local/apache-maven

mv apache-maven* /usr/local/apache-maven

rm apache-maven*

Setting environment variables in zsh

If you are using zsh, then run the following script in your terminal.

nano ~/.zshrc

export M2_HOME=/usr/local/apache-maven/apache-maven-3.6.3
export M2=$M2_HOME/bin
export PATH=$M2:$PATH

source ~/.zshrc

Setting environment variables in bash

nano ~/.bashrc

# add these three lines at the end of file and CTRL+X and Y and click enter :)

export M2_HOME=/usr/local/apache-maven/apache-maven-3.6.3
export M2=$M2_HOME/bin
export PATH=$M2:$PATH

source ~/.bashrc

Check Maven Version

# Verify if Maven is running
mvn -version

How to install Java

Java Specification Versions:

Here are the major releases:

  1. Java 1.0 (January 1996) – The original version.
  2. Java 1.1 (February 1997) – Introduced several significant features, including the JavaBeans API.
  3. Java 2 (J2SE 1.2) (December 1998) – Introduced the Swing application framework.
  4. J2SE 1.3 (May 2000) – Introduced the HotSpot JVM.
  5. J2SE 1.4 (February 2002) – Introduced regular expressions, image I/O API, and more.
  6. Java SE 5 (J2SE 1.5) (September 2004) – Introduced generics, metadata annotations, enumerated types, and more.
  7. Java SE 6 (December 2006) – Introduced scripting support, JVM improvements, and more.
  8. Java SE 7 (July 2011) – Introduced the try-with-resources statement, the diamond operator, and more.
  9. Java SE 8 (March 2014) – Introduced lambdas, streams, and the java.time package.
  10. Java SE 9 (September 2017) – Introduced the module system.
  11. Java SE 10 (March 2018) – Introduced local-variable type inference (var).
  12. Java SE 11 (September 2018) – Introduced String::linesisBlank, and other methods, plus the java.net.http HttpClient.
  13. Java SE 12 (March 2019) – Introduced switch expressions (preview) and more.
  14. Java SE 13 (September 2019) – Introduced text blocks (preview) and more enhancements.
  15. Java SE 14 (March 2020) – Introduced pattern matching for instanceof (preview) and other improvements.
  16. Java SE 15 (September 2020) – Continued the introduction and enhancement of several features.
  17. Java SE 16 (March 2021) – Introduced sealed classes (preview) and other enhancements.
  18. Java SE 17 (September 2021) – This version is an LTS (Long Term Support) release, which means it will receive updates for a longer period than the interim releases.
  19. Java SE 18 (March 2022)
  20. Java SE 19 (September 2022)
  21. Java SE 20 (March 2023)
  22. Java SE 21 (September 2023)

Java has moved to a time-driven release model, with new versions being released every six months. Not all of these are long-term support (LTS) versions. As of Java 17, the LTS versions have been Java 8, Java 11, and Java 17.

Downloading and Installing Java from OpenJDK

if you want to download OpenJDK manually, download it from https://jdk.java.net/

But you can download JDK programmatically as the following. Java SE 7, 8, 9, 10, 11, 12, 13 are included below script. So, run below script in your terminal.

# Java SE 7
wget https://download.java.net/openjdk/jdk7u75/ri/openjdk-7u75-b13-linux-x64-18_dec_2014.tar.gz

# Java SE 8
wget https://download.java.net/openjdk/jdk8u40/ri/openjdk-8u40-b25-linux-x64-10_feb_2015.tar.gz

# Java SE 9
wget https://download.java.net/java/GA/jdk9/9.0.4/binaries/openjdk-9.0.4_linux-x64_bin.tar.gz

# Java SE 10
wget https://download.java.net/java/GA/jdk10/10.0.2/19aef61b38124481863b1413dce1855f/13/openjdk-10.0.2_linux-x64_bin.tar.gz

# Java SE 11
wget https://download.java.net/java/GA/jdk11/9/GPL/openjdk-11.0.2_linux-x64_bin.tar.gz

# Java SE 12
wget https://download.java.net/java/GA/jdk12.0.2/e482c34c86bd4bf8b56c0b35558996b9/10/GPL/openjdk-12.0.2_linux-x64_bin.tar.gz

# Java SE 13
wget https://download.java.net/java/GA/jdk13/5b8a42f3905b406298b72d750b6919f6/33/GPL/openjdk-13_linux-x64_bin.tar.gz


mkdir /usr/lib/jvm


# Extract all downloaded jdk files
tar -xvzf openjdk-7u75-b13-linux-x64-18_dec_2014.tar.gz -C /usr/lib/jvm

tar -xvzf openjdk-8u40-b25-linux-x64-10_feb_2015.tar.gz -C /usr/lib/jvm

tar -xvzf openjdk-9.0.4_linux-x64_bin.tar.gz -C /usr/lib/jvm

tar -xvzf openjdk-10.0.2_linux-x64_bin.tar.gz -C /usr/lib/jvm

tar -xvzf openjdk-11.0.2_linux-x64_bin.tar.gz -C /usr/lib/jvm

tar -xvzf openjdk-12.0.2_linux-x64_bin.tar.gz -C /usr/lib/jvm

tar -xvzf openjdk-13_linux-x64_bin.tar.gz -C /usr/lib/jvm



# Remove downloaded jdk files
rm openjdk*


# Install Java and Java Compiler to Environment
update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-se-7u75-ri/bin/java 1

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-se-7u75-ri/bin/javac 1

update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-se-8u40-ri/bin/java 2

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-se-8u40-ri/bin/javac 2

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-9.0.4/bin/java 3

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-9.0.4/bin/javac 3

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-10.0.2/bin/java 4

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-10.0.2/bin/javac 4

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-11.0.2/bin/java 5

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-11.0.2/bin/javac 5

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-12.0.2/bin/java 6

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-12.0.2/bin/javac 6

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-13/bin/java 7

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-13/bin/javac 7



# Verify Java and Java Compiler Installation
# check if java command is pointing to correct path in system
update-alternatives --display java
update-alternatives --display javac

# List all environment variables
update-alternatives --get-selections
# or
update-alternatives --get-selections | grep java
# or
update-alternatives --get-selections | grep ^java

Check Java Version

# Verify if Java is running
java -version

# Verify if Java Compiler is running
javac -version