Java

From Gentoo Wiki
(Redirected from Project:Java/User Guide)
Jump to:navigation Jump to:search
This page contains changes which are not marked for translation.

Java is a programming language, originally developed by Sun Microsystems, which uses a platform-independent virtual machine to execute Java bytecode in real-time. It is a popular choice for developers who want to create cross-platform business applications.

What is Java?

Overview

Java is a programming language developed by Sun Microsystems. The language is object-oriented and designed to run on multiple platforms without the need of recompiling code for each platform. Although Java can be compiled as a native program, much of Java's popularity can be attributed to its portability, along with other features such as automatic memory management. To make platform independence possible the Java compiler compiles the Java code to an intermediate representation called Java bytecode that runs on a JVM (Java Virtual Machine) and not directly on the operating system.

In order to run Java bytecode, one needs to have a JRE (Java Runtime Environment) installed. A JRE provides core libraries, a platform dependent JVM, plugins for browsers, among other things. A JDK (Java Development Kit) adds programming tools, such as a bytecode compiler and a debugger.

JVM languages

The Java virtual machine is not used exclusively by Java programming language. Multiple programming languages use the Java platform and run on the JVM. Examples of such include: Clojure, Apache Groovy, Kotlin or Scala.

Installing a virtual machine

The choices

Gentoo provides Java Runtime Environments (JREs) and Java Development Kits (JDKs). The available choices include:

Vendor JDK
OpenJDK dev-java/openjdk and dev-java/openjdk-bin
Eclipse Temurin dev-java/openjdk-jre-bin

Installing a JRE/JDK

To install the profile's default JDK run:

root #emerge --ask --oneshot virtual/jdk

To install the profile's default JRE run:

root #emerge --ask --oneshot virtual/jre
Note
Be aware each JDK will include a JRE; installing a JRE is not necessary if a JDK has been emerged.

Setting up a headless JRE

Sometimes there is no need for a full JRE with all the capabilities of java. Using java on a server often does not require any GUI, graphical, sound or even printer related features. To install a simplified (sometimes also referred to as headless) JRE, a few USE flags need to be changed for the selected JRE flavor.

FILE /etc/portage/package.useRequired USE flag changes
dev-java/openjdk headless-awt -alsa -cups
dev-java/openjdk-bin headless-awt -alsa -cups

Depending on the current Gentoo profile, this might already be the case. As usual, the USE flag settings that are applicable to a particular package can be checked by running emerge in pretend mode:

user $emerge --pretend --verbose virtual/jre

Configuring the Java Virtual Machine

Overview

Gentoo has the ability to have multiple JDKs and JREs installed without causing conflicts.

Setting a default

The eselect command can be used to present a list of installed Java instances (be it JRE or JDK). Here is an example of the output:

user $eselect java-vm list
Available Java Virtual Machines:
  [1]   openjdk-8 
  [2]   openjdk-11 
  [3]   openjdk-17
  [4]   openjdk-bin-8  system-vm user-vm

The user-vm flag indicates the default JVM for the user. The system-vm flag indicates the default JVM for the system and the fallback if a user JVM is not set. The number in the brackets (i.e. [1]) is the reference for the particular JVM. To set the default system JVM:

root #eselect java-vm set system 1

To set a preferred user JVM:

user $eselect java-vm set user 1
Note
source-ing the profile for existing shell sessions is not usually needed when changing the user or system VM. The only exception is that variables such as JAVA_HOME will still point to the old location after setting a user VM for the first time or revert back to the system VM.

Java browser plugins

Important
The Java plugin support had been deprecated in JDK 9[1].
Note
Note that Chromium-based browsers since version 42 and Firefox since version 52 no longer support NPAPI-based plugins[2]. This effectively disables the Java plugin on modern browsers.

For those who need a Java-enabled browser for a specific use case, there is e.g. palemoon/palemoon-bin available in the palemoon overlay, which has long-term support for NPAPI and thus Java plugins up to JDK 8[3].

USE flags for use with Java

Setting USE flags

For more information regarding USE flags, refer to the USE flags chapter from the Gentoo Handbook.

USE flags

Following USE flags go in JAVA_PKG_IUSE, see Gentoo Java USE flags for details and other specific USE flags of Java:

  • The source flag installs a zip of the source code of a package. This is traditionally used for IDEs to 'attach' source to the libraries that are being use;
  • For Java packages, the doc flag will build API documentation using javadoc.

Multiple Java Versions

For those that may need multiple versions of Java, one may use slotted packages.

root #emerge --ask dev-java/openjdk:8
root #emerge --ask dev-java/openjdk:11
root #emerge --ask dev-java/openjdk:17
root #emerge --ask dev-java/openjdk:21

Troubleshooting

Minecraft launcher errors

  • A specific error in which minecraft-launcher crashed after a few seconds, throwing "Alarm" and "SaveToBuffer failed" error was solved by setting the USE flag threads for net-misc/curl.
  • When executing minecraft-launcher the following error was produced:
user $./minecraft-launcher
[0229/184549.183275:ERROR:sandbox_linux.cc(346)] InitializeSandbox() called with multiple threads in process gpu-process.

This was solved by executing minecraft-launcher with the following option:

user $MESA_GLSL_CACHE_DISABLE=true ./minecraft-launcher

See also

External resources

More information can be found offline:

user $man java-config
user $java-config --help

References

  1. JDK 9 and the Java Plugin, java.com. Retrieved on November 30, 2018
  2. How do I enable Java in my web browser?, java.com. Retrieved on November 30, 2018
  3. Pale Moon future roadmap, palemoon.org. Retrieved on June 28, 2019

This page is based on a document formerly found on our main website gentoo.org.
The following people contributed to the original document: Joshua Nichols, Karl Trygve Kalleberg, nightmorph
They are listed here because wiki history does not allow for any external attribution. If you edit the wiki article, please do not add yourself here; your contributions are recorded on each article's associated history page.