Wednesday, May 13, 2020

How To Add Java Jdk To Path

How_To_Add_Java_Jdk_To_Path

In the previous tutorial, we have learned how to install java JDK both on windows or linux system. However there are a few settings that should be done to help us in running a Java program code. Now, we will learn how to add java JDK to path on Windows and Linux systems.

Java JDK Path Windows 10


According to the Oracle documentation, it is explained that we can actually run java without the need to set environment paths. However, if you don't set the java JDK path variable, then you need to specify the full address of the variable path every time yourun a java program, for example:

C:\Java\jdk1.8.0\bin\javac MyClass.java

In general, path is the term for addressing a file in the computer's directory. For example we have a learn_java.doc document. The path address can be stored in C:\MyDocument\learn_java.doc.

So, if we say "set the path in the Windows operating system", then that means we input a folder address so that Windows cmd can run stored applications from anywhere.

For example, the java.exe and javac.exe files that will be used to process Java languages are in the C:\Program Files\Java\jdk1.8.0_191\bin\folder. So every time we need to access this file, we must open the folder C:\Program Files\Java\jdk1.8.0_191\bin first.

By setting the path address, later java.exe and javac.exe files can be accessed from any address.

To better understand, let's open Windows cmd:

default_windows_cmd

By default, the current directory is C:\Users\Erlangga, that is to say, currently Windows cmd is in the Users\Erlangga folder on drive C. The position you find can be different because here "Erlangga" is my username.

All typed cmd commands will only affect the C:\Users\Erlangga folder. For example, type java.exe -version then press Enter:

cmd-windows-error-java.exe-not-found

The result is:

'java.exe' is not recognized as an internal or external command, operable program or batch file.

This error message appears because I tried to access the java.exe file in the C:\Users\Erlangga folder. Of course the java.exe file was not found because the location of java.exe is in C:\Program Files\Java\ jdk1.8.0_191\bin\java.exe.

By setting the system path in Windows, we can tell Windows cmd to search for the java.exe file in the C:\Program Files\Java\ jdk1.8.0_191\bin folder automatically.

Changing the path settings can be different depending on the version of Windows. The steps below are for Windows 10.

First open the System Properties settings window. The fastest way is to click the search icon next to the Start Menu (Windows Logo), then type "environment". The "Edit the system environment variables" option will appear.
access_menu_system_environtment

Another way is through the Control Panel, select System and Security, select the System menu, then on the left menu click "Advanced System Settings".

advanced_system_settings_control_panel

Then select the "Advanced" tab in the "System Properties" window. Then, at the bottom right, click the environment variable button:

environment_variable_setup_path

In the Environment Variable window, select the Path line at the bottom, then click the Edit button.

edit_environment_variable_path

The Edit Environment Variable window will now appear as follows:

new_edit_environment_variable_path

Click the New button at the top right. The cursor will switch to the lower left side, then type the address of the Java SDK path, which is C:\ Program Files\Java\jdk1.8.0_191\bin\. If you store Java JDK at another address, adjust the writing path.

add_jdk_path_environment_variable

After the path C:\Program Files\Java\jdk1.8.0_191\bin\ is in the table, click OK to end, then click the OK button several times to close all Environment Variable and System Properties windows.

Check using the java.exe -version command:

check_java_version_windows

Java JDK Path Linux (Ubuntu)

Open the file /etc/environment with any text editor. In this example, I use Nano:

$ sudo nano /etc/environment

Add the following line at the end to set the JAVA_HOME value:

JAVA_HOME="/usr/lib/jvm/jdk1.8.0_191/bin/java"

NOTE: Don’t forget to update it with the actual path to your Java installation.

Press CTRL+X to finish editing and save the changes. Next, make the changes by applying this command:

source /etc/environment

You can double-check if it’s active by entering:

echo $JAVA_HOME

check_java_jdk_path

That is, we have learned how to add java JDK to path in either Windows or Linux. This technique is not only useful for Java applications, but also for many other applications.

Tuesday, May 12, 2020

How To Install Java JDK and JRE

How_To_Install_Java JDK_JRE

In the previous tutorial, we have discussed what is JVM or Java Virtual Machine. Here, we will learn other java programming terms, namely JRE and JDK. Are the two still related to the JVM and why do we need to discuss both?

Understanding JRE (Java Runtime Environment)
JRE or Java Runtime Environment is an application package containing JVM (Java Virtual Machine) and some additional libraries needed to run Java applications.

So, to run a Java application on a computer or a system, a JRE application must be installed.

Java_Runtime_Environment

In the picture above, it appears that the JRE contains a JVM plus several libraries and other files. In the JVM there is a JIT (Just-In-Time) compiler, which is a technique used by Java to improve performance when running programs.

If we only want to run Java applications and do not plan to make Java program code, then JRE is the required application. Inside the JRE, there is a java command that can be used to run Java byte code.

Unfortunately, starting from version 11 and above, Oracle no longer releases JRE separately. We have to install a package with JDK (will be discussed shortly). If you intend to install JRE only, you can use an old version of JRE, for example JRE 10 or JRE 8.

Understanding JDK (Java Development Kit)
JDK or Java Development Kit is an application package containing JVM (Java Virtual Machine) + JRE (Java Runtime Environment) + various applications for the process of making Java program code.

Java_Development_Kit
One additional command in JDK is the javac command. Javac is used to process Java program code into byte code. How to use this command we will discuss in the next few tutorials.

Oracle JDK vs OpenJDK
There are currently 2 types of JDK: Oracle JDK and OpenJDK. The two versions are very similar with slight differences in the licenses.

As the name implies, OpenJDK is the open-source version of JDK released under the GNU General Public License (GNU GPL). Open JDK is still being developed by Oracle (formerly Sun Microsystem), along with various other companies, especially RedHat.

Meanwhile, Oracle JDK is a version of OpenJDK with added several features from Oracle released under commercial license. Last January 2019, Oracle drastically changed the contents of the Oracle JDK license, especially in terms of support for corporate users.

To get updates and support, companies use Oracle JDK must pay starting at $ 2.5 per user each month. But for individual use or development, we don't need to pay to use Oracle JDK.

There are still many who do not realize this difference. I'm sure the majority of Java books or tutorials for the Windows operating system still use Oracle JDK. Because, when we visit the Oracle web, this is the JDK version they have immediately offered.

For this reason, I will use Oracle JDK throughout the discussion on this blog.

Install Oracle JDK 8 On Windows

First you have to do before installing JDK is to download the JDK 8 software on the official website. Click here.

Then scroll down to find the version of the Java SE Development Kit 8u102 (JDK-8u102-windows-x64.exe), as shown in the picture below.

download_oracle_jdk8_windows

When finished, open the file then click next.

Select the feature (follow the following picture) then click next


Wait for the progress to finish.

Determine the location where the JDK folder will be stored, then click next.


The installation process will begin and wait for it to finish.

If the installation process is complete, please click close.

Install JDK 8 On Linux (Ubuntu)

OpenJDK

Open terminal. Update system with the command:

$ sudo apt-get update

Then, do the following to install default JDK:

$ sudo apt-get install default-jdk

If you want to install a different version of Java, you just need to replace the number 8 in the command according to the desired version number.

$ sudo apt-get install openjdk-8-jdk

OracleJDK

Before installing Java oracle, you must add an Oracle PPA to your repository. Type the command below:

$ sudo add-apt-repository ppa:webupd8team/java

Then, update with the command:

$ sudo apt-get update

Next, type command for the installation. Just like OpenJDK, replace the Java version of the oracle with the desired version number. Here I install Java oracle 8.

$ sudo apt-get install oracle-java8-installer

Wait for the installation to complete. If there is a configuration request like the image below, select OK by pressing Enter.


Then on the next window, navigate to the Yes option, then press Enter.

Wait for the installation to complete.

Check java version

$ java -version

check_java_version_ubuntu

Up to this point, we have successfully installed JDK both on the Windows or Linux operating system. In the next tutorial we will learn on how to add java JDK to path.

What Is Java Virtual Machine? Understanding JVM


Java_Virtual_Machine

Java language has its way to be run in a system. Unlike the flow of programming languages in general, to run Java code, the system must have a JVM (Java Virtual Machine) installed.

Understanding Java Virtual Machine

Java Virtual Machine is a special application installed to run Java programs on a computer or system.

Java_Virtual_Machine_Diagram
The definition above is "simple version", because the Java Virtual Machine (we just call it a JVM), in theory, it's not only able to run applications written in the Java language, but also several other programming languages based on "Java bytecode".

Generally a desktop programming language has the following workflow (we use the C language example):
  • Write the program code using a text editor, then save for example as hello_world.c.
  • Compile the hello_world.c file into an object file named hello_world.o.
  • Perform a linker process, where the hello_world.o file is processed and produces an executable file named hello_world.exe (if it is intended for Windows operating systems).
  • The hello_world.exe file is ready to run.
The workflow above is a bit problematic in terms of portability. I mean, the hello_world.exe application can only run on Windows operating systems. If we want to run the same application on a Linux or Mac computer, the program code must be recompiled for each operating system.

The Java language development team wants to overcome this problem so that the Java language code is compiled only once and can run on all operating systems. Following is the process flow of creating and running programs written in Java:
  • Write the program code using a text editor, then save for example as hello_world.java
  • Compile the hello_world.java file into a byte code-named hello_world.class
  • The hello_world.class file can be run on all computers as long as the computer has a Java Virtual Machine installed.
The byte code in step 2 is similar to object code in other programming languages, but specifically the byte code is used to refer to object code that belongs to the Java language.

Components of JVM
As compilation target and virtual machine, JVM try to mimic our computer in its architecture, such as by having memory management system and register.
JVM has 3 essential components:
  • Class Loader
  • Runtime Data Area
  • Execution Engine
The portable java bytecode are stored in our computer as files with *.class extension. Therefore the first component of JVM is responsible for loading all *.class files, hence why it’s called the class loader. It is also responsible for verifying whether the supplied bytecodes are valid and resolving all static references.

Runtime data area responsible for storing all classes, objects, methods, variables and its data. Basically this is the component that mimic our computer memory. But it also have PC registers as sub-component, which stores the address of current executing instruction (essentially similar to register in our CPU).

Lastly and perhaps the most important, execution engine responsible for executing the bytecodes. It contains an interpreter, just-in-time compiler (abbr. JIT compiler) and garbage collector.

You may wonder why JVM has both compiler and interpreter. On early version of JVM the execution engine only has interpreter to execute the bytecode. The JIT compiler was added later to increase performance. Basically the JVM can now dynamically increase performance by identifying methods that are used often in our program. JVM then ask the JIT compiler instead to compile those methods instead of re-interpreting it every time. This is why JVM performance benefits quite significantly from warm-up time.

Programming Languages Running on the JVM

Clojure
Designed by Rich Hickey, Clojure is a programming language similar to Scheme and Lisp that runs on the JVM. Clojure has quite a lot of interest among users of functional programming languages.

At first, Rich Hickey wanted the modern Lisp programming language for functional programming. He also wants a concurrent feature in that language. So at that time, Rich developed dotLisp, a Lisp project implemented on the .NET Framework. But finally, Rich developed Clojure which was implemented on the JVM. One well-known web framework designed using Clojure is Noir. The following is an example of the Clojure source code quoted from Wikipedia:

(let [i (atom 0)]
  (defn generate-unique-id
    "Returns a distinct numeric ID for each call."
    []
    (swap! i inc)))

Groovy
Groovy is a dynamic programming language similar to Python, Perl, Ruby, and Smalltalk that runs on Java. Groovy can interact with other Java libraries, and can create a blend of Java and Groovy.

Groovy has a famous web framework, Grails. Groovy was developed by James Strachan. Groovy is managed by a company called Pivotal. If you use Netbeans for the first time, you can see there are options for making an application with Groovy. Here is an example of the Groovy source code quoted from Wikipedia:

class AGroovyBean {
  String color
}

def myGroovyBean = new AGroovyBean()

myGroovyBean.setColor('baby blue')
assert myGroovyBean.getColor() == 'baby blue'

myGroovyBean.color = 'pewter'
assert myGroovyBean.color == 'pewter'

Jython
Jython is the JVM implementation of the Python programming language. It is designed to run on the Java platform. A Jython program can import and use any Java class. Just as Java, Jython program compiles to bytecode. One of the main advantages is that a user interface designed in Python can use GUI elements of AWT, Swing, or SWT Package. Some standard Python libraries cannot be accessed in Jython.

Jython was first developed by Jim Hugunin, Barry Warsaw, Samuele Pedroni, Brian Zimmer, and Frank Wierzbicki. The following is an example of Jython source code quoted from Jython Cookbook:

from javax.swing import JButton, JFrame

frame = JFrame('Hello, Jython!',
            defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
            size = (300, 300)
        )

def change_text(event):
    print 'Clicked!'

button = JButton('Click Me!', actionPerformed=change_text)
frame.add(button)
frame.visible = True

JRuby
JRuby was developed by Charles Oliver Nutter and Thomas Enebo. Initially JRuby was developed on Sun Microsystems but then moved to Engine Yard in 2009. In May 2012, the JRuby duo moved to Red Hat and focused more on developing JRuby. Two other contributors are Ola Bini and Nick Sieger. One of JRuby's advantages is the presence of multiple virtual machine collaboration features. The following is an example of the JRuby source code quoted from Wikipedia:

require 'java'

frame = javax.swing.JFrame.new
frame.getContentPane.add javax.swing.JLabel.new('Hello, World!')
frame.setDefaultCloseOperation javax.swing.JFrame::EXIT_ON_CLOSE
frame.pack
frame.set_visible true

Scala
Scala is claimed to be a simpler programming language compared to Java. One well-known web framework built using Scala is Lift and Scalatra. Scala was created by Martin Odersky. Martin wants a powerful functional programming language and also runs on the JVM. Besides, Martin was also involved in the development of the JVM with James Gosling and other JVM development teams. The following is an example of the Scala source code quoted from Wikipedia:

// Scala
class Point(
    val x: Double, val y: Double,
    addToGrid: Boolean = false
) {
  import Point._

  if (addToGrid)
    grid.add(this)

  def this() = this(0.0, 0.0)

  def distanceToPoint(other: Point) =
    distanceBetweenPoints(x, y, other.x, other.y)
}

object Point {
  private val grid = new Grid()

  def distanceBetweenPoints(x1: Double, y1: Double,
      x2: Double, y2: Double) = {
    math.hypot(x1 - x2, y1 - y2)
  }
}

The above programming language is among the many other programming languages implemented on the JVM. According to the List of JVM Language article on Wikipedia, there are around 60 programming languages developed on top of the JVM.

With a variety of specific characteristics and targets, JVM is the basis for other programming language developers to create their programming languages by relying on JVM as the foundation. So JVM isn't just for Java

Is Java Easy To Learn? - 7 Reasons Why Learning Java

Is_Java_Easy_To_Learn

When someone wants to learn a programming language, surely it must start by choosing what programming language to learn. You can drop your choice on Java. Why? Because Java is one of the best programming language.

Java as a programming language has existed and survived for 20 years. Java has proven that from day to day become more advanced. Although admittedly, there are some times where java programming development is slowing down, but the effect it comes with better performance. Changes to Enum, Generic, and Autoboxing in Java 5, as well as advances in performance in Java 6, and the choice of Java as a programming language for android by the Google, have made Java a popular programming language.

When you compare which programming language is the best? What programming language should I learn? Should I learn Java? etc.

So it will depend on your definition of the best programming language, when it comes to popularity, there is no doubt that Java is on top, even if compared to C, which has existed for 50 years. Java offers many opportunities in terms of employment. You can develop Java cores based on server applications, J2EE web and enterprise applications, and can even go into Android-based application programming.

Reasons to Learn Java For Beginners

There are several reasons that can be considered why Java is a programming language worth studying in terms of opportunities, development and support from various communities.

1. Java is easy to learn.

Wow ... really?

When you see Java syntax, most use English. The syntax of the Java programming language is the set of rules defining how a Java program is written and interpreted. The syntax is mostly derived from C and C++. Unlike in C++, in Java there are no global functions or variables, but there are data members which are also regarded as global variables.

This makes Java programs easy to read and also easy to learn. As one of the best programming languages, Java is designed to be easy to understand, and produce products quickly through this language.

2. Java is an object-oriented programming language

During this time, there are two kinds of programming languages, namely, procedural and object-oriented. The downside of a procedural language is difficult to develop a large-scale program.

Therefore object-oriented programming was created. Once you know and understand the concept of object-oriented programming regarding Abstraction, Encapsulation, Polymorphysm and Inheritance, then you can use it all with Java.

3. Have an outstanding IDE.

Believe it or not, Eclipse and Netbeans have had a crucial role to make Java as one of the best programming language. You can do the coding using the IDE with convenient, especially if you have been coding in DOS Editor or Notepad.

This IDE not only helps in the code completion process, but also in its ability to detect bugs, which is one of the important things in program development.
IDE has made a breakthrough by making Java development easier, faster and clearer.

Apart from the IDE, the Java platform also has some other useful tools like Maven and ANT to build Java applications, the decompilers, JConsole, Visual VM to monitor the use of Heap and others.

4. Rich API and outstanding documentation support from Javadoc.

One factor that makes Java successful is it has a rich API, and more importantly the API comes with a Java installation by default. Java provides APIs for I/O, networks, utilities, XML parsing, database connections, and so on.

Whatever is left behind is covered by open source libraries such as Apache Commons, Google Guava, and so on.

Furthermore there is Javadoc, which makes you learn Java easier. Javadoc also become a reliable reference when you perform coding. Javadoc is also a document that provides a lot of information about the Java API. Without documentation from Javadoc, maybe Java will not be popular. Javadoc also makes Java one of the best programming languages.

5. Very complete open source library.

Open source libraries ensure that Java can be used anywhere. Apache, Google and many other organizations have contributed in establishing many libraries, which in turn make Java development easier, faster and more effective.

There are several frameworks such as Spring, Struts, Maven ensuring java development using the best way, promoted by the use of design patterns and is supported by Java developers to give the best results.

6. Support from the community.

It is undeniable that the community has made Java a powerful programming language. No matter how good the programming language is, it won't last if there is no community to share knowledge and ready to help peoples in trouble.

Java has a lot of active forums, stackoverflow, organizations that provide open source and several groups of Java users to help everything. If you are a beginner, then there is a community to help beginners learn Java. Similarly, if you are in an advanced level or even an expert.

Furthermore, many programmers contribute as a tester. Expert programmers provide free advice on various Java forums and stackoverflow, so that for beginners will provide a more confident feeling.

7. Java is free and everywhere.

Everyone likes free ones. These factors make Java more popular among programmers as an individual or organization that will develop the strategic program.

Java is everywhere. That's right, Java is on mobile, desktop, cards, and so on.

The number of Java programmers is also a reason why many organizations choose Java as a programming language for the new development compared to other programming languages.

Well, that's 7 reasons why you should learning java programming.