Java

From ArchWiki
Revision as of 20:44, 26 August 2014 by Wikimig (Talk | contribs) (Remove out of date "Java sound with PulseAudio" scandalous section)

Jump to: navigation, search

Related articles

"Java is a programming language originally developed by Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture." — Wikipedia article

Arch Linux officially supports the open source OpenJDK versions 7 and 8. All these JVM can be installed without conflict and switched between using helper script archlinux-java. Several other Java environments are available in AUR but are not officially supported.

Installation

The following packages are available in the official repositories:

OpenJDK 7:

Package name Use
jre7-openjdk-headless Java runtime environment (JRE) without any graphical tool - version 7
jre7-openjdk Complete Java Runtime Environment (JRE) - version 7
jdk7-openjdk Java Development Kit (JDK) - version 7
openjdk7-doc OpenJDK javadoc - version 7
openjdk7-src OpenJDK sources - version 7

OpenJDK 8:

Package name Use
jre8-openjdk-headless Java runtime environment (JRE) without any graphical tool - version 8
jre8-openjdk Complete Java Runtime Environment (JRE) - version 8
jdk8-openjdk Java Development Kit (JDK) - version 8
openjdk8-doc OpenJDK javadoc - version 8
openjdk8-src OpenJDK sources - version 8
Note: Installing a JDK will automatically pull its JRE dependency.
Note: After installation, the Java environment will need to be recognized by the shell ($PATH and $JAVA_HOME variables). This can be done by sourcing /etc/profile from the command line or by logging out/in again of a Desktop Environment.

A common package named java-common automatically pulled as dependency provides environment file /etc/profile.d/jre.sh. This file contains all JVM common environment variables. Package java-common also provides a utility script archlinux-java that can display and change the default Java environment. This script sets link /usr/lib/jvm/java-default-runtime to point at a valid non-conflicting Java environment installed in /var/lib/jvm/java-${JAVA_MAJOR_VERSION}-${VENDOR_NAME}. It also creates and updates links in /usr/bin for all executable provided by the Java environment set.

Warning: File /etc/profile.d/jdk.sh is not provided anymore by any package.

Switching between JVM

Helper script archlinux-java provides such functionalities:

archlinux-java <COMMAND>

COMMAND:
	status		List installed Java environments and enabled one
	get		Return the short name of the Java environment set as default
	set <JAVA_ENV>	Force <JAVA_ENV> as default
	unset		Unset current default Java environment
	fix		Fix an invalid/broken default Java environment configuration

List compatible Java environments installed

% archlinux-java status

Example:

% archlinux-java status
Available Java environments:
  java-7-openjdk (default)
  java-8-openjdk/jre

Note the (default) denoting that java-7-openjdk is currently set as default. Invocation of java and other binaries will rely on this Java install. Also note on the previous output that only the JRE part of OpenJDK 8 is installed here.

Change default Java environment

# archlinux-java set <JAVA_ENV_NAME>

Example:

# archlinux-java set java-8-openjdk/jre

Note that archlinux-java will not let you set an invalid Java environment. In the previous example, jre8-openjdk is installed but jdk8-openjdk is not so trying to set java-8-openjdk will fail:

# archlinux-java set java-8-openjdk
'/usr/lib/jvm/java-8-openjdk' is not a valid Java environment path

Unsetting the default Java environment

There should be no need to unset a Java environment as packages providing them should take care of this. Still should you want to do so, just use command unset:

# archlinux-java unset

Fixing the default Java environment

If /usr/bin links are incorrect or if an invalid Java environment link is set, calling the archlinux-java fix command tries to fix these. Also note that if no default Java environment is set, this will look for valid ones and try to set it for you (Officially supported packages "OpenJDK 7" will be considered first):

# archlinux-java fix

One time setup

Links in /usr/bin point at wrapper script /usr/lib/java-common-wrapper which in turns uses environment variable JAVA_HOME. Setting this variable before calling a binary will thus allow use of a Java environment other than the one set as default:

% javac -version
javac 1.7.0_65

% JAVA_HOME=/usr/lib/jvm/java-8-openjdk javac -version
javac 1.8.0_05

% javac -version
javac 1.7.0_65

Known unexpected behavior

  • When a JDK is installed, forcing the JAVA_HOME variable can produce an unexpected "failing link" error:
% archlinux-java status                                     
Available Java environments:                                                   
  java-7-openjdk (default)
  java-8-openjdk/jre                # note jdk8-openjdk is NOT installed

% java -version
java version "1.7.0_65"
OpenJDK Runtime Environment (IcedTea 2.5.1) (Arch Linux build 7.u65_2.5.1-7-x86_64)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)

% JAVA_HOME=/usr/lib/jvm/java-8-openjdk/jre javac -version 
/usr/bin/javac: line 2: /usr/lib/jvm/java-8-openjdk/jre/bin/javac: No such file or directory

The javac link exists in /usr/bin to point at /usr/lib/java-common-wrapper that will then call /usr/lib/jvm/java-7-openjdk/bin/javac. Forcing JAVA_HOME results in /usr/bin/javac trying to call /usr/lib/jvm/java-8-openjdk/bin/javac which is not installed. This is a known behavior (calling javac on a JRE with no corresponding JDK does not make sense anyway).

Package pre-requisites to support archlinux-java

This section is targeted at packager willing to provide packages in AUR for an alternate JVM and be able to integrate will Arch Linux JVM scheme to use archlinux-java. To do so, packages should:

  • Place all files under /usr/lib/jvm/java-${JAVA_MAJOR_VERSION}-${VENDOR_NAME}
  • Not provide any link on /usr/bin, these are automatically detected and set by archlinux-java
  • Suffix man pages with -${VENDOR_NAME}${JAVA_MAJOR_VERSION} to prevent conflicts (see jre8-openjdk file list where man pages are suffixed with -openjdk8)
  • Not declare any conflicts nor replaces with other JDKs, java-runtime, java-runtime-headless nor java-environment
  • Use script archlinux-java in install functions to set the Java environment as default if no other valid Java environment is already set (ie: package should not force install as default). See officially supported Java environment package sources for examples
  • Not try to change the value of JAVA_HOME

Also please note that:

  • Packages that need any Java environment should declare dependency on java-runtime, java-runtime-headless or java-environment as usual
  • Packages that need a specific Java vendor should declare dependency on the corresponding package and take special care to start their piece of software by pre-setting JAVA_HOME
  • OpenJDK packages now declare provides="java-runtime-openjdk=${pkgver}" etc. This enables a third party package to declare dependency on an OpenJDK without specifying which version

Unsupported JVM from AUR

Warning: Packages in AUR may or may not support archlinux-java

Oracle Java SE

Several packages from AUR provide Oracle's implementations of JRE and JDK: jreAUR and jdkAUR but also jre8-oracleAUR and jdk8-oracleAUR which use archlinux-java

Java SE 6/7

The AUR contains jre6AUR/jre7AUR and jdk6AUR/jdk7AUR, which are the Oracle implementations of Java SE 6 and Java SE 7.

JDK-compat

The Oracle JDK (6 and 7) can also be installed in parallel with another Java installation (for example OpenJDK). The packages can be found in the AUR: jdk6-compatAUR, jdk7-compatAUR and jdk8-compatAUR.

Oracle JRockit

JRockit is a JIT version of Java, provided by Oracle and available as jrockitAUR from the AUR.

VMkit

VMkit is an LLVM-based framework for JIT virtual machines. J3 is a JVM running on VMkit. The webpage can be found here: vmkit. J3 depends on the GNU classpath libraries, but may also work with the Apache class path libraries.

Parrot VM

Parrot is a VM that offers experimental support for Java through two different methods: Either as a Java VM bytecode translator or as a Java compiler targeting the Parrot VM. parrot is available in the official repositories and parrot-gitAUR in the AUR.

Troubleshooting

MySQL

Due to the fact that the JDBC-drivers often use the port in the URL to establish a connection to the database, it is considered "remote" (i.e., MySQL does not listen to the port as per its default settings) despite the fact that they are possibly running on the same host, Thus, to use JDBC and MySQL you should enable remote access to MySQL, following the instructions in MySQL#Grant Remote Access.

Impersonate another window manager

You may use the wmname from suckless.org to make the JVM believe you are running a different window manager. This may solve a rendering issue of Java GUIs occurring in window managers like Awesome or Dwm.

$ wmname LG3D

You must restart the application in question after issuing the wmname command.

This works because the JVM contains a hard-coded list of known, non-re-parenting window managers. For maximum irony, some users prefer to impersonate LG3D, the non-re-parenting window manager written by Sun, in Java.

Illegible fonts

In addition to the suggestions mentioned below in #Better font rendering, some fonts may still not be legible afterwards. If this is the case, there is a good chance Microsoft fonts are being used. Install ttf-ms-fontsAUR from the AUR.

Missing dependencies

If you encounter, often when installing from the AUR, a missing dependency of java-runtime, you might need to install some form of jdk.

Missing text in some applications

If some applications are completely missing texts it may help to use the options under #Tips and tricks as suggested in FS#40871.

Tips and tricks

Note: Suggestions in this section are applicable to all applications, using explicitly installed (external) Java runtime. Some applications are bundled with own (private) runtime or use own mechanics for GUI, font rendering, etc., so none of written below is guaranteed to work.

Behavior of most Java applications can be controlled by supplying predefined variables to Java runtime. From this forum post, a way to do it consists of adding the following line in your ~/.bashrc (or /etc/profile.d/jre.sh to affect programs that are not run by sourcing ~/.bashrc, e.g., launching a program from Gnome's Applications view):

export _JAVA_OPTIONS="-D<option 1> -D<option 2>..."

For example, to use system anti-aliased fonts and make swing use the GTK look and feel:

export _JAVA_OPTIONS='-Dawt.useSystemAAFontSettings=on -Dswing.aatext=true -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel'

Better font rendering

Both closed source and open source implementations of Java are known to have improperly implemented anti-aliasing of fonts. This can be fixed with the following options: awt.useSystemAAFontSettings=on, swing.aatext=true

See Java Runtime Environment Fonts for more detailed information.

GTK LookAndFeel

If your Java programs look ugly, you may want to set up the default look and feel for the swing components: swing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel.

Some stubborn Java programs insist on using the cross platform Metal look and feel. In some of these cases you can force these apps to use the GTK look and feel by setting the following property:

swing.crossplatformlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel.