Java – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Wed, 14 Sep 2022 03:46:53 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 How to Set JAVA_HOME environment variable on macOS https://tecadmin.net/how-to-set-java_home-environment-variable-on-macos/ https://tecadmin.net/how-to-set-java_home-environment-variable-on-macos/#respond Tue, 13 Sep 2022 12:26:53 +0000 https://tecadmin.net/?p=31670 This tutorial will help you to set the JAVA_HOME environment variable on the latest older macOS operating systems. The /usr/libexec/java_home is the command line utility that returns the Path of the Java home directory from the current user’s settings. You can use this utility to set up the JAVA_HOME environment variable on your macOS. In [...]

The post How to Set JAVA_HOME environment variable on macOS appeared first on TecAdmin.

]]>
This tutorial will help you to set the JAVA_HOME environment variable on the latest older macOS operating systems. The /usr/libexec/java_home is the command line utility that returns the Path of the Java home directory from the current user’s settings. You can use this utility to set up the JAVA_HOME environment variable on your macOS.

In an Operating System (OS) JAVA_HOME environment variable must be set to point to the directory containing the JVM. In this tutorial, we will help you to correctly set the JAVA_HOME environment variable on macOS.

Check Pre-Installed Java Versions

You can find details of all installed Java versions on macOS with the following command.

/usr/libexec/java_home -V 
Ouput:
Matching Java Virtual Machines (5): 18.0.1, x86_64: "OpenJDK 18.0.1" /Library/Java/JavaVirtualMachines/temurin-18.jdk/Contents/Home 16.0.1, x86_64: "AdoptOpenJDK 16" /Library/Java/JavaVirtualMachines/adoptopenjdk-16.jdk/Contents/Home 15.0.1, x86_64: "AdoptOpenJDK 15" /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home 11.0.9.1, x86_64: "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home 1.8.0_275, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home /Library/Java/JavaVirtualMachines/temurin-18.jdk/Contents/Home

Set JAVA_HOME on macOS 10.15 and newer

The macOS Catalina (version 10.15) and the newer macOS versions usee the Zsh as the default shell. Zsh executes ~/.zshrc script during a new session starts. You can add your code in this file to the JAVA_HOME environment variable. Once the user is logged or opens a new shell the variable will automatically be set by the script.

Use one of the below commands to set JAVA_HOME as per the required Java version:

  • Use default Java version:
    echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc 
    
  • Set the specific Java version by specifying it as:
    echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.8)" >> ~/.zshrc 
    

    To use a different Java version, simply replace 1.8 with the version you required like 11, 15, 17, or 18.

Set JAVA_HOME on macOS 10.14 Mojave and older

The macOS Mojave (version 10.14) and the previous macOS versions uses the Bash as the default shell. Bash runs ~/.bash_profile script everytime it started. You can easily set the JAVA_HOME environment variable using this file.

  • Set the default Java version:
    echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
    
  • Set the specific Java version:
    echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.8)" >> ~/.bash_profile
    

    To use a different Java version, simply replace 1.8 with the version you required like 11, 15, 17, or 18.

Conclusion

The JAVA_HOME is an environment variable that points to the file system location where the JDK or JRE is installed. Many Java application uses this variable to find the location of Java installed on the system.

The post How to Set JAVA_HOME environment variable on macOS appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-set-java_home-environment-variable-on-macos/feed/ 0
What are the Access Modifiers in Java https://tecadmin.net/java-access-modifiers/ https://tecadmin.net/java-access-modifiers/#respond Fri, 05 Aug 2022 01:01:35 +0000 https://tecadmin.net/?p=30996 An access modifier is a special kind of variable declaration that controls how and where other code can use the declared class, variable, or method. This blog will explain what access modifiers are and the three different levels of visibility for classes, methods, and variables with some practical examples. Understanding the scope of your code [...]

The post What are the Access Modifiers in Java appeared first on TecAdmin.

]]>
An access modifier is a special kind of variable declaration that controls how and where other code can use the declared class, variable, or method. This blog will explain what access modifiers are and the three different levels of visibility for classes, methods, and variables with some practical examples.

Understanding the scope of your code is essential for writing readable and maintainable programs. Access modifiers are one way to achieve this goal. They tell other users of your code how you want them to interact with specific methods or variables. If you’re new to Java programming, these concepts may seem confusing at first glance; however, once you learn about them once or twice, they become much clearer. Keep reading to learn more about access modifiers in Java.

Access Modifiers in Java

Access modifiers are special keywords that let you specify how other code can interact with specific methods or variables within classes. Like many programming concepts, access modifiers are simple in practice but can seem confusing at first. Once you understand them, they can help make your code more readable and maintainable.

There are four different types of access modifiers in java:

List of Access Modifiers in Java

A Short Explanation:

  • Private: A private member is accessible within the class scope.
  • Default: A member with no access modifier is also known as package-private. Which is only accessible within classes in the same package.
  • Protected: A protected member is accessible within all classes in the same package and the subclasses of the other packages.
  • Public: A public member is accessible to anywhere.

Please see below infographic image to better understand the access level of different modifiers in Java.

What is Access Modifiers in Java
Access Modifiers and Their Scope in Java

In the above representation, the fields with “Yes” are the allowed scope of modifiers. The “No” fields don’t allow accessibility of the member.

Private Access Modifier

A private access member is accessible within the class it is declared. A compile-time error will occur when another class tried to access it.

Let’s understand with an example: Created a class named “One”, and declare a private variable “i” with a default value. Now create a method showData() with default access in the same class that prints the variable value.

Now create another class “TecAdmin” with the main() method. Create an object of class One and try to print the variable “i”. As the variable “i” is declared private, the java compiler will through an error. Comment the above line and then call the showData method. This will print the value of “i” successfully as the method is also declared in the same class.

class One{
   private int i = 10;
   void showData(){
        System.out.println(i);
   }
}

class TecAdmin{

   public static void main(String args[]){
      One obj = new One();
      System.out.println(obj.i);  //Compilation error
      // Comment above line to test the next line code
      obj.showData();   //Successfully run
   }
}

Conclusion

The main difference between each visibility level is who can access them: – public – everyone – private – only the class that contains the variable/method – protected – the class that contains the variable/method and any classes derived from it The visibility of the class, method, or variable determines if and how other methods or classes can interact with it. This is called encapsulation, and it’s one of the cornerstones of object-oriented programming. And now that you know what access modifiers are and how they work, you can use them in your own code to make it more readable and maintainable.

The post What are the Access Modifiers in Java appeared first on TecAdmin.

]]>
https://tecadmin.net/java-access-modifiers/feed/ 0
How to Install JAVA on Ubuntu 22.04 https://tecadmin.net/how-to-install-java-on-ubuntu-22-04/ https://tecadmin.net/how-to-install-java-on-ubuntu-22-04/#comments Sun, 15 May 2022 16:01:55 +0000 https://tecadmin.net/?p=6117 Java is the most popular object-oriented, robust, platform-independent programming language. A large number of applications developed on Java required a system has Java installed. OpenJDK is an open-source implementation of Java available freely for production use. This guide will help you to install Java (OpenJDK 17, 11, and 8) LTS releases. Also, help you to [...]

The post How to Install JAVA on Ubuntu 22.04 appeared first on TecAdmin.

]]>
Java is the most popular object-oriented, robust, platform-independent programming language. A large number of applications developed on Java required a system has Java installed.

OpenJDK is an open-source implementation of Java available freely for production use.

This guide will help you to install Java (OpenJDK 17, 11, and 8) LTS releases. Also, help you to install Oracle Java 17 LTS) on your Ubuntu system. You will also find the instruction’s to switch between multiple installed Java versions.

Installing Java on Ubuntu

Java 17 is the latest LTS release available for installation. JDK packages provide the full Java development libraries, helpful for the development systems. To run a Java application, you just needed a Java runtime environment (JRE).

1. Install Java (OpenJDK) from Default Repositories

Default Ubuntu 22.04 repositories contain the OpenJDK 18, 17, 11, and 8 packages. By default, the apt package manager will install Java 11 but you can also install a specific Java version.

First of all, update the Apt cache:

sudo apt update 

Now, choose what Java version you want to install and execute one of the following commands to install that specific version.

  • Install OpenJDK 11
    sudo apt install openjdk-11-jdk 
    
  • Install OpenJDK 8
    sudo apt install openjdk-8-jdk 
    
  • Install OpenJDK 17
    sudo apt install openjdk-17-jdk 
    

You can also install OpenJDK 18 (openjdk-18-jdk) package, but that is a non-LTS release.

Once the installation is complete, you can verify it by checking the Java version with the below command.

java -version

openjdk version "11.0.15" 2022-04-19
OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)

2. Install Oracle Java 17 on Ubuntu

As of today, Oracle Java 18 and Java 17 are available to download and install without getting a commercial license. Java 18 is a non-LTS version and Java 17 is an LTS version. So we prefer to install Java 17 on Ubuntu systems.

You can visit official download page to download Debian file on your system. Also, you can use the below command to download the Debian file using the command line.

wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.deb 

If the above command does not work or you found a file not found error, please go to the official download page. Check for the current version and download it to your system.

Once you download the Debian file, install it on your system using command below:

sudo apt install ./jdk-17_linux-x64_bin.deb 

The above command installs Oracle Java 17 on your system. All the files will be placed under /usr/lib/jvm directory. Now set an alternative reference to newly installed Java on your system.

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 100 

Oracle Java 17 has been installed on your Ubuntu system. If you have installed multiple Java versions on a single system, go to the next step to change the default Java version with update-alternatives CLI on a Ubuntu system.

Change Default Java Version on Ubuntu

Most Unix/Linux-based systems allow the installation of multiple Java versions on one system. If you also have multiple Java versions installed on your system. You can change to the default java version as per your requirements.

The update-alternatives provides you option to maintain symbolic links for the default commands. To change default Java version run command on terminal:

update-alternatives --config java 

This will show the list of all java binaries installed on your system. Enter a number to select the default Java version and press enter. See the below screenshot:

There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  3            /usr/lib/jvm/jdk-17/bin/java                     100       manual mode

Press  to keep the current choice[*], or type selection number: 3

The above command will change the default Java version on your system by changing the link references to the java binary. Now, again run command java -version to view the default Java version.

java -version 

java version "17.0.3.1" 2022-04-22 LTS
Java(TM) SE Runtime Environment (build 17.0.3.1+2-LTS-6)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.3.1+2-LTS-6, mixed mode, sharing)

Conclusion

In this tutorial, you have learned about the installation of multiple Java on the Ubuntu 22.04 systems. Also found a solution to change the default Java version via the command line.

The post How to Install JAVA on Ubuntu 22.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-install-java-on-ubuntu-22-04/feed/ 2
Java HashMap – How to Get Value from Key https://tecadmin.net/java-hashmap-get-value-from-key/ https://tecadmin.net/java-hashmap-get-value-from-key/#respond Tue, 05 Apr 2022 02:31:56 +0000 https://tecadmin.net/?p=28839 Write a Java program to get value from the hashmap corresponding to a key. The HashMap class is available under the java.util package. It is pretty similar to HashTable, but the HashMap is unsynchronized and also allows to stole one null key. In this tutorial, you will learn Java examples to get value from a [...]

The post Java HashMap – How to Get Value from Key appeared first on TecAdmin.

]]>
Write a Java program to get value from the hashmap corresponding to a key.

The HashMap class is available under the java.util package. It is pretty similar to HashTable, but the HashMap is unsynchronized and also allows to stole one null key.

In this tutorial, you will learn Java examples to get value from a HashMap based on a defined key.

Get Values from Java HashMap

The Entry interface provides a number of methods to access key values from a HashMap. The Entry.getValue() method returns the value based on the provided key.

Let’s check with an example. Here we initialize a HashMap and then iterate through all key-pair using for loop. Create a file HashMapExample1.java in your system and add the below content.

import java.util.HashMap;
import java.util.Map.Entry;

class HashMapExample1 {
  public static void main(String[] args) {

    // Define a hashmap
    HashMap<Integer, String> cities = new HashMap<>();

    // Adding key pair to hashmap  
    cities.put(101, "Delhi");
    cities.put(102, "New York");
    cities.put(103, "Peris");
    cities.put(104, "Denmark");

    // Define value to search key for 
    String value = "Peris";

    // Iterate through hashmap using for loop
    for(Entry<Integer, String> entry: cities.entrySet()) {
      if(entry.getValue() == value) {
        System.out.println("The Key for '" + value + "' is " + entry.getKey());
        break;
      }
    }
  }
}

Save the file and close it.

Now, compile the Java program and run. You will see the results below.

Output:
The Key for 'Peris' is 103

Get All Key Values in HashMap

You can also print all the key values from a Java HashMap using the println() method. Run the below example on your system:

import java.util.HashMap;

class HashmapExample2 {
  public static void main(String[] args) {

    // Define a hashmap
    HashMap<Integer, String> cities = new HashMap<>();

    // Adding key pair to hashmap  
    cities.put(101, "Delhi");
    cities.put(102, "New York");
    cities.put(103, "Peris");
    cities.put(104, "Denmark");

    // Print all hashmap key pairs
    System.out.println("HashMap: " + cities);
  }
}

Now, compile and run above Java program. You should see the results as below:

Output:
HashMap: {101=Delhi, 102=New York, 103=Peris, 104=Denmark}

Wrap Up

In this faq, you have learned an example to get the HashMap key based on a value in the Java programming language.

The post Java HashMap – How to Get Value from Key appeared first on TecAdmin.

]]>
https://tecadmin.net/java-hashmap-get-value-from-key/feed/ 0
Java HashMap – How to Get Key from Value https://tecadmin.net/java-hashmap-get-key-from-value/ https://tecadmin.net/java-hashmap-get-key-from-value/#respond Sat, 02 Apr 2022 03:55:34 +0000 https://tecadmin.net/?p=28829 Write a Java program to get keys from the hashmap using the value. The HashMap class is available under the java.util package. It is pretty similar to HashTable, but the HashMap is unsynchronized and also allows to stole one null key. In this tutorial, you will learn Java examples to get keys from a HashMap [...]

The post Java HashMap – How to Get Key from Value appeared first on TecAdmin.

]]>
Write a Java program to get keys from the hashmap using the value.

The HashMap class is available under the java.util package. It is pretty similar to HashTable, but the HashMap is unsynchronized and also allows to stole one null key.

In this tutorial, you will learn Java examples to get keys from a HashMap based on a defined value.

Get Key for a Value in HashMap

The Entry interface provides a number of methods to access key values from a HashMap. The Entry.getValue() method returns the value based on the provided key.

Here is an example Java program to initialize a HashMap and then iterate through all key-pair using for loop.

Let’s create a file HashMapExample1.java in your system and add the below content.

import java.util.HashMap;
import java.util.Map.Entry;

class HashMapExample1 {
  public static void main(String[] args) {

    // Define a hashmap
    HashMap<Integer, String> cities = new HashMap<>();

    // Adding key pair to hashmap  
    cities.put(101, "Delhi");
    cities.put(102, "New York");
    cities.put(103, "Peris");
    cities.put(104, "Denmark");

    // Define value to search key for 
    String value = "Peris";

    // Iterate through hashmap using for loop
    for(Entry<Integer, String> entry: cities.entrySet()) {
      if(entry.getValue() == value) {
        System.out.println("The Key for '" + value + "' is " + entry.getKey());
        break;
      }
    }
  }
}

Save the file and close it.

Now, compile the Java program and run. You will see the results below.

Output:
The Key for 'Peris' is 103

Get All Key Values in HashMap

Here is another example showing to get all key values from a Java HashMap.

import java.util.HashMap;

class HashmapExample2 {
  public static void main(String[] args) {

    // Define a hashmap
    HashMap<Integer, String> cities = new HashMap<>();

    // Adding key pair to hashmap  
    cities.put(101, "Delhi");
    cities.put(102, "New York");
    cities.put(103, "Peris");
    cities.put(104, "Denmark");

    // Print all hashmap key pairs
    System.out.println("HashMap: " + cities);
  }
}

Now, compile and run above Java program. You should see the results as below:

Output:
HashMap: {101=Delhi, 102=New York, 103=Peris, 104=Denmark}

Wrap Up

In this faq, you have learned an example to get the HashMap key based on a value in the Java programming language.

The post Java HashMap – How to Get Key from Value appeared first on TecAdmin.

]]>
https://tecadmin.net/java-hashmap-get-key-from-value/feed/ 0
How To Install Java on Debian 11 https://tecadmin.net/how-to-install-java-on-debian-11/ https://tecadmin.net/how-to-install-java-on-debian-11/#respond Mon, 30 Aug 2021 11:05:46 +0000 https://tecadmin.net/?p=27508 Java is one of the well-liked computer programming languages to develop PC or mobile apps and is available on various platforms. There are many applications that require Java on your system to function. Apart from that, there are billions of mobile devices that run Java and around 9 million developers who prefer Java to create [...]

The post How To Install Java on Debian 11 appeared first on TecAdmin.

]]>
Java is one of the well-liked computer programming languages to develop PC or mobile apps and is available on various platforms. There are many applications that require Java on your system to function. Apart from that, there are billions of mobile devices that run Java and around 9 million developers who prefer Java to create applications.

If you are a developer and a Linux user then Java is an important component to have on your system, it will not only help you to create applications but also assist in running thousands of apps and games that require Java. This write-up is a guide to install Java on the latest release of Debian known as Bullseye.

Prerequisites

  1. A running Debian 11 system with sudo privileged account access.
  2. Compelte the initial server setup to prepare your system with the few recommendations

Step 1 – Installing Java on Debian 11

The default Debian 11 repositories contain the latest stable version OpenJDK 11 and the latest version ie OpenJDK 17. You can install any one of them from the official repositories.

Java Runtime Environment (JRE) will only assist in running applications developed in Java. To create a Java application you may need Java Development Kit or JDK. To install both, use:

  • Install Latest Stable Java (OpenJDK 11) on Debian 11 – The following command will install Java Runtime Environment.
    sudo apt install default-jre default-jdk 
    

    You can ignore JDK or JRE package based on your requirements.

  • Install Latest Java (OpenJDK 17) on Debian 11 – Use the following command to install the current latest version OpenJDK 17, execute the following command:
    sudo apt install openjdk-17-jre openjdk-17-jdk 
    

You can also have installed both versions on your system.

Once the download and installation is done, you can check the version:

java -version 
Output:
openjdk version "17-ea" 2021-09-14 OpenJDK Runtime Environment (build 17-ea+19-Debian-1) OpenJDK 64-Bit Server VM (build 17-ea+19-Debian-1, mixed mode, sharing)

To check the JDK version use:

javac -version 
Output:
javac 17-ea

Step 2 – Switching Between Multiple Java Versions

You can have multiple versions of Java on your system, they can easily be managed by using the command:

sudo update-alternatives --config java 

You will get a list of installed Java versions on your system where each version is associated with a number, and any version can be set as the default Java environment of your system. You can choose the Java version which is associated with a number, type number, and press Enter.

Switch Java on Debian 11

You have successfully updated the default JRE version. If you also have JDK installed, do the same for “javac”:

sudo update-alternatives --config javac 

Enter a number to set the required version as default.

Step 3 – Configure Environment Variable

Many Java-based applications use the JAVA_HOME environment variable to get the location of Java saved on your PC. So, to avoid any errors it is suggested to set the JAVA_HOME variable.

First, find the java binary path with the following command:

sudo update-alternatives --list java 

After that, to set the variable edit the /etc/environment file in any text editor:

sudo nano /etc/environment 

Set the JAVA_HOME environment variable with the value of binary path from the previous command. Write the path by simply skipping “bin/java”:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64/" 

Save changes and close file. Next, reload the file:

source /etc/environment  

To check the variable use:

echo $JAVA_HOME 
Output:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64/"

In the output, you will get the path.

Conclusion

Java is a cross-platform language that is widely used not only to develop PC but mobile applications as well. Java flawlessly works on any operating system whether it is Windows PC or Linux. Many applications require Java to function on a system, for that you need to have Java installed on your PC.

This write-up is a guide on how to install Java on Debian 11 Bullseye and how to manage it. Finally, we also learned how to set up the JAVA_HOME environment variable in Debian 11.

The post How To Install Java on Debian 11 appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-install-java-on-debian-11/feed/ 0
How to Install Tomcat 10 on Ubuntu 20.04 https://tecadmin.net/how-to-install-tomcat-10-on-ubuntu-20-04/ https://tecadmin.net/how-to-install-tomcat-10-on-ubuntu-20-04/#comments Wed, 17 Mar 2021 15:15:16 +0000 https://tecadmin.net/?p=24944 Apache Tomcat is an open-source web server with a servlet container for publishing Java-based web applications. Tomcat is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation. As of today, Tomcat 10 is the latest stable version available for installation on development and production environments. To know [...]

The post How to Install Tomcat 10 on Ubuntu 20.04 appeared first on TecAdmin.

]]>
Apache Tomcat is an open-source web server with a servlet container for publishing Java-based web applications. Tomcat is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation. As of today, Tomcat 10 is the latest stable version available for installation on development and production environments. To know more about the Apache Tomcat visit apache official site http://tomcat.apache.org/.

This tutorial will help you to how to install Apache Tomcat 10 on Ubuntu 20.04 LTS Linux systems.

Prerequisites

A running Ubuntu 20.04 system with shell access of root or sudo privileged account access.

For the newly installed systems recommended to complete initial server setup instructions.

Step 1 – Install Java

You must have JRE (Java runtime environment) installed on your system. Tomcat 10 is required to have JRE 8 or higher version installed on your system. Use the following command to install OpenJDK to fulfil the requirements.

sudo apt update 
sudo apt install default-jdk -y 

Check the current active Java version:

java -version 

openjdk 11.0.13 2021-10-19
OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

Step 2 – Create Tomcat User

We recommended running a Tomcat server with a dedicated user account. Create a new user, which is recommended for security purposes mainly for production deployments.

To create a new account, type:

sudo useradd -m -d /opt/tomcat -U -s /bin/false tomcat 

The above command will create a user and group with the name “tomcat” in your system.

Step 3 – Install Tomcat 10

The Apache Tomcat development team releases the latest version of Tomcat from time to time. So it will be good check download latest Tomcat version from the official download server. Use the below command to download Tomcat 10.

wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.23/bin/apache-tomcat-10.0.23.tar.gz 

Once the download is completed, extracted the downloaded archive and copy all content to the tomcat home directory.

sudo tar xzvf apache-tomcat-10*tar.gz -C /opt/tomcat --strip-components=1 

Next, set the proper file permissions.

sudo chown -R tomcat:tomcat /opt/tomcat/ 
sudo chmod -R u+x /opt/tomcat/bin 

You have now the latest Tomcat application on your system.

Step 4 – Create Tomcat User

Now, configure your tomcat with user accounts to secure access of admin/manager pages. To do this, edit conf/tomcat-users.xml file in your editor and paste the following code inside <tomcat-users> </tomcat-users> tags. We recommend changing the password in the below configuration with high secured password.

sudo nano /opt/tomcat/conf/tomcat-users.xml 

Add the following values. Make sure to change the password for admin and manager access.

<!-- user manager can access only manager section -->
<role rolename="manager-gui" />
<user username="manager" password="_SECRET_PASSWORD_" roles="manager-gui" />

<!-- user admin can access manager and admin section both -->
<role rolename="admin-gui" />
<user username="admin" password="_SECRET_PASSWORD_" roles="manager-gui,admin-gui" />

Save file and close.

Step 5 – Enable Remote Tomcat Access

The default Tomcat manager and host-manager applications are accessible for localhost only. To allow access to these pages from the remote system, you need to modify the following configuration files.

You can either allow a specific remote system or allow all. Edit the context.xml file for manager and host manager application:

sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml

Comment out the section added for IP address restriction to allow connections from anywhere.

<Context antiResourceLocking="false" privileged="true" >
  <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
                   sameSiteCookies="strict" />
  <!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
  ...
</Context>

Similarly edit context.xml for host manager application in text editor:

sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml

Comment out the same section to allow connections from anywhere.

<Context antiResourceLocking="false" privileged="true" >
  <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
                   sameSiteCookies="strict" />
  <!--<Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
  ...
</Context>

Save all files and close it.

Step 6 – Create a Tomcat Systemd Unit File

Tomcat provides bash scripts to start, stop service. But, to make it simple, create a startup script to manage Tomcat as systemd service. Let’s create a tomcat.service file with the following content:

sudo nano /etc/systemd/system/tomcat.service 
[Unit]
Description=Tomcat
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
Environment="CATALINA_BASE=/opt/tomcat"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

Reload the systemd daemon service to load newly create files.

sudo systemctl daemon-reload 

Now, start the Tomcat application for the first time.

sudo systemctl start tomcat.service 

Next, enable the tomcat service to auto-start for subsequent system boots. This is more important for the production deployments.

sudo systemctl enable tomcat.service 

As of now, the tomcat application is running on your system. You can verify the service status by executing the command as below. Make sure the status is showing “active (running)“.

sudo systemctl status tomcat.service 
● tomcat.service - Tomcat
     Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-03-17 10:56:39 IST; 3h 45min ago
    Process: 481049 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
   Main PID: 481056 (java)
      Tasks: 29 (limit: 4539)
     Memory: 264.2M
     CGroup: /system.slice/tomcat.service
             └─481056 /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Djava.util.logging.config.file>

Mar 17 10:56:39 tecadmin-ubuntu2004 systemd[1]: Starting Tomcat...
Mar 17 10:56:39 tecadmin-ubuntu2004 startup.sh[481049]: Tomcat started.
Mar 17 10:56:39 tecadmin-ubuntu2004 systemd[1]: Started Tomcat.

Step 7 – Access the Tomcat Web Interface

The default Tomcat server runs on port 8080. As you have configured Tomcat on your system, you can access web interface from your system. You can access tomcat interfaces by entering your server’s IP address or a domain name pointed to that server, followed by port 8080 in your browser:

Change tecadmin.local with your server ip or domain or localhost.

http://tecadmin.local:8080/

You will see the page like below:

Installing Tomcat 10

Tomcat Manager App is a web application packaged with the Tomcat server application. The Manager interface provides us with the basic functionality we need to manage our deployed web applications.

Click Manager App button home page or directly type /manager in browser url of main Tomcat server to access it.

http://tecadmin.local:8080/manager/

Tomcat 10 Manager Dashboard

Tomcat Host Manager App is another web application packaged with Tomcat server application. Which is used to creates/removes Virtual Hosts within the Tomcat service. A Virtual Host allows you to define multiple hostnames on a single server.

Click Host Manager button home page or directly type /host-manager url in main Tomcat server to access it.

http://tecadmin.local:8080/host-manager/

Tomcat 10 Host Manager Page

Conclusion

You have a running Tomcat server on Ubuntu system. You may need to create a Virtual host or configure a SSL certificate in Tomcat.

The post How to Install Tomcat 10 on Ubuntu 20.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-install-tomcat-10-on-ubuntu-20-04/feed/ 5
How to Check Java Version https://tecadmin.net/check-java-version/ https://tecadmin.net/check-java-version/#respond Sun, 06 Dec 2020 09:27:19 +0000 https://tecadmin.net/?p=23816 Java is a cross-platform, object-oriented programming language developed by the Sun Microsystems in the year 1995. Today, java is used by the billion’s of devices, games, media players, audio vide applications etc. As of today Java SE 15 is the latest version available for the installation. JDK 16 will be the next version expected to [...]

The post How to Check Java Version appeared first on TecAdmin.

]]>
Java is a cross-platform, object-oriented programming language developed by the Sun Microsystems in the year 1995. Today, java is used by the billion’s of devices, games, media players, audio vide applications etc.

As of today Java SE 15 is the latest version available for the installation. JDK 16 will be the next version expected to release in March, 2021. Both are the short term releases supports till next release only. After that JDK 17 will the next Long-Term Support release, will be supported for approx. eight years.

Java installation tutorials:

This tutorial described you to how to check Java version installed on a system.

Check Java Version

Java comes in two types of installations. You can either install Java Development Kit (JDK) or install Java Runtime Environment (JRE) only.

The JDK installation also includes the JRE. JDK is uses to compile java programs is basically installed on development systems. Where JRE is uses to run java programs compiled by the JDK. The JRE is required to install or production servers, where we only run the applications.

  • Check Java Runtime Version – You can use -version command line argument prints version details on error stream, also you can use --version prints java version details on standard output. Open a terminal (CTR+ALT+T) and type:
    java --version 
    
    java 11 2018-09-25
    Java(TM) SE Runtime Environment 18.9 (build 11+28)
    Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11+28, mixed mode)
    

    The above output shows that you have installed Java SE 11 on your system.

  • Check Java Compiler Version – javac is the compiler used to create bytecode from Java source code. Java compiler is installed with Java Development Kit (JDK). Use below command to view java compiler version.
    javac --version  
    
    javac 11
    

Conclusion

In this tutorial, you have learned about finding hte installed JDK or JRE versions on any system.

The post How to Check Java Version appeared first on TecAdmin.

]]>
https://tecadmin.net/check-java-version/feed/ 0
How to Allow Remote Access to Tomcat Manager https://tecadmin.net/allow-tomcat-manager-access-from-remote-host/ https://tecadmin.net/allow-tomcat-manager-access-from-remote-host/#respond Wed, 27 May 2020 14:40:15 +0000 https://tecadmin.net/?p=21659 The default Tomcat server does not allow remote access to the Manager and HostManager applications. This tutorial will teach you how to configure Tomcat so that remote hosts can access the Manager and HostManager applications. Tomcat have a context file for each deployed web application under the conf/Catalina/localhost directory. It has the file with the [...]

The post How to Allow Remote Access to Tomcat Manager appeared first on TecAdmin.

]]>
The default Tomcat server does not allow remote access to the Manager and HostManager applications. This tutorial will teach you how to configure Tomcat so that remote hosts can access the Manager and HostManager applications.

Tomcat have a context file for each deployed web application under the conf/Catalina/localhost directory. It has the file with the same name as the web app like manager.xml or host-manager.xml.

So, if the file is not present, you need to create a file conf/Catalina/localhost/manager.xml and specify the rule to allow remote hosts.

sudo nano ${CATLINA_HOME}/conf/Catalina/localhost/manager.xml 

Add the following content.

<Context privileged="true" antiResourceLocking="false" 
         docBase="{catalina.home}/webapps/manager">
    <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" />
</Context>

You also need to create an XML file for the host-manager web app to allow access for remote hosts.

sudo nano ${CATLINA_HOME}conf/Catalina/localhost/host-manager.xml 

Add the following content.

<Context privileged="true" antiResourceLocking="false" 
         docBase="{catalina.home}/webapps/host-manager">
    <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" />
</Context>

After you create the XML files, you must restart the Apache server in order to read the new files. Now, when you access the manager or host-manager web app, you will be prompted for authentication.

The post How to Allow Remote Access to Tomcat Manager appeared first on TecAdmin.

]]>
https://tecadmin.net/allow-tomcat-manager-access-from-remote-host/feed/ 0
How to Install Eclipse IDE on Ubuntu 20.04 https://tecadmin.net/install-eclipse-ide-ubuntu-20-04/ https://tecadmin.net/install-eclipse-ide-ubuntu-20-04/#comments Fri, 01 May 2020 03:09:07 +0000 https://tecadmin.net/?p=21280 Eclipse is an integrated development environment (IDE) used in computer programming. Eclipse really is the best IDE for Java applications development. Most of the Java programmers preferred to use Eclipse for the Development environment. It contains a base workspace and an extensible plug-in system for customizing the environment. This tutorial will help you to install [...]

The post How to Install Eclipse IDE on Ubuntu 20.04 appeared first on TecAdmin.

]]>
Eclipse is an integrated development environment (IDE) used in computer programming. Eclipse really is the best IDE for Java applications development. Most of the Java programmers preferred to use Eclipse for the Development environment. It contains a base workspace and an extensible plug-in system for customizing the environment.

This tutorial will help you to install Eclipse IDE on Ubuntu 20.04 using snap tool.

Prerequisites

Before continuing for Eclipse installation on Ubuntu 20.04:

  • Login to Ubuntu as sudo privileged user
  • For the newly installed systems, required to complete initial server setup
  • Step 1 – Install Java

    Eclipse required Java runtime environment to be installed on your system. If you already have Java installed, skip this step. To install Java type:

    sudo apt update
    sudo apt install openjdk-11-jdk
    

    The JDK package also contains JRE. It will also required to compile a Java program on your system. On the other hand, Use next tutorial, to install specific Java version.

    Step 2 – Install Eclipse

    Ubuntu 20.04 preferred to use snap for package installation instead of using apt package manager. To install Eclipse IDE on your machine type:

    sudo snap install eclipse --classic
    

    It will download the eclipse IDE snap package and install on your machine. Once the installation completed successfully, you will see the following output.

    installing eclipse ide ubuntu 20.04

    Step 3 – Start Eclipse IDE

    Now, The Eclipse IDE has been installed on your Ubuntu 20.04 system. Search for the Eclipse launcher in and click on it.

    Start eclipse on Ubuntu

    During the launch, Eclipse will ask you to set a Workplace. This will be default location for your Eclipse to save files of your application’s build on Eclipse.

    Check the checkbox “Use this as the default and do not ask again” to make this location as default with next launch. click launch button to start eclipse:

    Eclipse set workplace

    Eclipse IDE running on your Ubuntu 20.04 system.

    To disable to welcome screen on next launch, Remove check of “Always show the Welcome at start up” n bottom-right of the IDE.

    Eclipse on Ubuntu 20.04

    Conclusion

    You have installed Eclipse IDE on your Ubuntu 20.04 LTS system. Let’s start working with your application.

    The post How to Install Eclipse IDE on Ubuntu 20.04 appeared first on TecAdmin.

    ]]> https://tecadmin.net/install-eclipse-ide-ubuntu-20-04/feed/ 1