Difference between revisions of "Java"

From ArchWiki
Jump to: navigation, search
(Tips and tricks)
(Launching an application with the non-default java version: quotes)
 
(132 intermediate revisions by 48 users not shown)
Line 1: Line 1:
[[Category:Programming language]]
+
[[Category:Programming languages]]
 
[[cs:Java]]
 
[[cs:Java]]
 +
[[de:Java]]
 
[[es:Java]]
 
[[es:Java]]
 +
[[fa:Java]]
 
[[fr:Java]]
 
[[fr:Java]]
 
[[it:Java]]
 
[[it:Java]]
 +
[[ja:Java]]
 
[[pt:Java]]
 
[[pt:Java]]
 
[[ru:Java]]
 
[[ru:Java]]
 
[[tr:Java]]
 
[[tr:Java]]
{{Article summary start}}
+
{{Related articles start}}
{{Article summary text|This article explains how to install and configure JRE/JDK.}}
+
{{Related|Java Package Guidelines}}
{{Article summary heading|Related}}
+
{{Related|Java Runtime Environment Fonts}}
{{Article summary wiki|Java Package Guidelines}}
+
{{Related articles end}}
{{Article summary end}}
+
  
"''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:Java (programming language)|Wikipedia article]]
+
From the [[Wikipedia:Java (programming language)|Wikipedia article]]:
 +
 
 +
: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.
 +
 
 +
Arch Linux officially supports the open source [http://openjdk.java.net/ OpenJDK] versions 7 and 8. All these JVM can be installed without conflict and switched between using helper script {{Ic|archlinux-java}}. Several other Java environments are available in [[AUR]] but are  not officially supported.
  
 
== Installation ==
 
== Installation ==
The only JVM implementation in supported repositories is the open source [http://openjdk.java.net/ OpenJDK]. Additional Java implementations are available via the [[AUR]]. Keep in mind that the open-source and closed-source versions cannot be installed simultaneously. The open-source version is nearly perfect at the time of writing, and there is mostly no need anymore to install Oracle's proprietary version of Java.
 
  
{{Note|After installation, the Java environment will need recognized by the shell ({{Ic|$PATH}} variable and {{Ic|$JAVA_HOME}}).  This can be done from the command line by sourcing {{Ic|/etc/profile}}, and for Desktop Environments it is likely a logout/login will be necessary.}}
+
{{Note|Installing a JDK will automatically pull its JRE dependency.}}
  
=== OpenJDK JVM ===
+
{{Note|After installation, the Java environment will need to be recognized by the shell ({{Ic|$PATH}} variable). This can be done by sourcing {{Ic|/etc/profile}} from the command line or by logging out/in again of a Desktop Environment.}}
To be able to run Java programs, you can install [[Wikipedia:JRE|Java runtime]] with the package {{Pkg|jre7-openjdk}}, available in the [[official repositories]]. There is also a [[Wikipedia:JDK|Java Development Kit]] in {{Pkg|jdk7-openjdk}}. As of [https://mailman.archlinux.org/pipermail/arch-general/2013-February/032930.html March 2013] {{Ic|openjdk6}} is no longer available.
+
  
You will likely need the {{Pkg|icedtea-web-java7}} package for Java functionality in browsers, namely [[Wikipedia:Java applet|applets]] and [[Wikipedia:Java Web Start|Web Start]] (for more details see [[Browser Plugins#Java (IcedTea)]]).
+
Two ''common'' packages named {{Pkg|java-runtime-common}} and {{Pkg|java-environment-common}} are automatically pulled as dependency and provide environment file {{Ic|/etc/profile.d/jre.sh}}. This file contains all JVM common environment variables. Package {{Pkg|java-runtime-common}} also provides a utility script {{Ic|archlinux-java}} that can display and change the default Java environment. This script sets links {{Ic|/usr/lib/jvm/default}} and {{Ic|/usr/lib/jvm/default-runtime}} to point at a valid non-conflicting Java environment installed and Java runtime in {{Ic|/usr/lib/jvm/java-${JAVA_MAJOR_VERSION}-${VENDOR_NAME}}}. Most executables provided by the Java environment set have direct links from {{Ic|/usr/bin}}, others are available in {{Ic|$PATH}}.
  
==== Flagging OpenJDK packages as out-of-date ====
+
{{Warning|File {{Ic|/etc/profile.d/jdk.sh}} is not provided any more by any package.}}
Please flag packages {{Pkg|jre7-openjdk-headless}}, {{Pkg|jre7-openjdk}} and {{Pkg|jdk7-openjdk}} as out-of-date based on their [http://icedtea.wildebeest.org/download/source IcedTea version] (2.3.4) rather than on their Oracle version (u9).
+
IcedTea-web projects (packages {{Pkg|icedtea-web}} and {{Pkg|icedtea-web-java7}}) have version numbers that are independant from the IcedTea one (packages {{Pkg|jre7-openjdk-headless}}, {{Pkg|jre7-openjdk}} and {{Pkg|jdk7-openjdk}}) so please flag each package based on its right version number.
+
  
=== Oracle JVM ===
+
The following packages are available in the [[official repositories]]:
Prior to the [https://mailman.archlinux.org/pipermail/arch-general/2011-August/021671.html retirement of the Oracle DLJ], it was possible to simply install the {{AUR|jre}} and {{AUR|jdk}} packages from the repositories. However, now Arch Linux (and any other GNU/Linux distribution) can no longer package the Oracle implementation of Java. The OpenJDK packages are the recommended providers of {{Ic|java-runtime}} and {{Ic|java-environment}}.
+
  
=== Oracle JVM (Java 6) ===
+
=== OpenJDK 7 ===
AUR contains package for {{AUR|jre6}} and {{AUR|jdk6}}, which are the Oracle implementations of Java SE 6.  These packages conflict with the other java packages, so there are also {{AUR|jre6-compat}} and {{AUR|jdk6-compat}} which can be used along side other java installations.
+
  
=== Kaffe (Discontinued) ===
+
{| class="wikitable"
[http://www.kaffe.org/ Kaffe] is another clean-room implementation of a Java VM without official endorsement from Sun/Oracle. A [[git]] package of Kaffe can be found in the [[AUR]] here: {{AUR|kaffe-git}}. The Kaffe VM is redistributed according to the GPL license.
+
! Package name !! Use
 +
|-
 +
|{{Pkg|jre7-openjdk-headless}} || Java runtime environment (''JRE'') without any graphical tool - version 7
 +
|-
 +
|{{Pkg|jre7-openjdk}}          || Complete Java Runtime Environment (''JRE'') - version 7
 +
|-
 +
|{{Pkg|jdk7-openjdk}}         || Java Development Kit (''JDK'') - version 7
 +
|-
 +
|{{Pkg|openjdk7-doc}}          || OpenJDK javadoc - version 7
 +
|-
 +
|{{Pkg|openjdk7-src}}          || OpenJDK sources - version 7
 +
|}
  
=== BEA JRockit JIT JVM (+JDK) ===
+
=== OpenJDK 8 ===
[[Wikipedia:JRockit|JRockit]] is JIT version of Java, provided by Oracle and available from the AUR here: {{AUR|jrockit}}.
+
  
=== VMkit LLVM-based JIT VM ===
+
{| class="wikitable"
[http://vmkit.llvm.org/index.html VMkit] is an LLVM-based framework for JIT virtual machines. J3 is a JVM running on VMkit. The webpage can be found here: [[http://vmkit.llvm.org/get_started.html vmkit]]. J3 depends on the GNU classpath libraries, but may also work with the Apache class path libraries.
+
! Package name !! Use
 +
|-
 +
|{{Pkg|jre8-openjdk-headless}} || Java runtime environment (''JRE'') without any graphical tool - version 8
 +
|-
 +
|{{Pkg|jre8-openjdk}}          || Complete Java Runtime Environment (''JRE'') - version 8
 +
|-
 +
|{{Pkg|jdk8-openjdk}}          || Java Development Kit (''JDK'') - version 8
 +
|-
 +
|{{Pkg|openjdk8-doc}}          || OpenJDK javadoc - version 8
 +
|-
 +
|{{Pkg|openjdk8-src}}          || OpenJDK sources - version 8
 +
|-
 +
|}
 +
 
 +
=== OpenJFX ===
 +
 
 +
JavaFX is also available from the official repositories. It requires the OpenJDK 8.
 +
 
 +
{| class="wikitable"
 +
! Package name !! Use
 +
|-
 +
|{{Pkg|java-openjfx}} || Java OpenJFX 8 client application platform (open-source implementation of JavaFX)
 +
|-
 +
|{{Pkg|java-openjfx-doc}} || OpenJFX javadoc
 +
|-
 +
|{{Pkg|java-openjfx-src}} || OpenJFX sources
 +
|-
 +
|}
 +
 
 +
== Flagging packages as out-of-date ==
 +
 
 +
Although the Arch Linux package releases may contain a reference to the proprietary versions the packages are based on, the open-source project has its own versioning scheme:
 +
 
 +
* {{Pkg|jre7-openjdk}}, {{Pkg|jdk7-openjdk}}, and {{Pkg|jre7-openjdk-headless}} should be flagged as out-of-date based on the [http://icedtea.wildebeest.org/download/source ''IcedTea'' version] (e.g. {{ic|2.4.3}}), rather than on the Oracle reference version (e.g. {{ic|u45}} in the release {{ic|7.u45_2.4.3-1}}).
 +
* {{Pkg|icedtea-web}} should be flagged as out-of-date based on the [http://icedtea.wildebeest.org/download/source ''IcedTea Web'' version] (e.g. {{ic|1.4.1}}). This is independent of the ''IcedTea'' version.
 +
 
 +
== Switching between JVM ==
 +
 
 +
The helper script {{Ic|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 {{Ic|java-7-openjdk}} is currently set as default. Invocation of {{Ic|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
 +
 
 +
{{Tip|To see possible {{ic|<JAVA_ENV_NAME>}} names, use {{ic|archlinux-java status}}.}}
 +
 
 +
Note that {{Ic|archlinux-java}} will not let you set an invalid Java environment. In the previous example, {{pkg|jre8-openjdk}} is installed but {{pkg|jdk8-openjdk}} is '''not''' so trying to set {{Ic|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 {{Ic|unset}}:
 +
 
 +
# archlinux-java unset
 +
 
 +
=== Fixing the default Java environment ===
 +
 
 +
If an invalid Java environment link is set, calling the {{Ic|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" and "OpenJDK 8" will be considered first in this order, then un-official packages from [[AUR]].
 +
 
 +
# archlinux-java fix
 +
 
 +
=== Launching an application with the non-default java version ===
 +
 
 +
If you want to launch an application with another version of java than the default one (for example if you have both version jre7 and jre8 installed on your system), you can wrap your application in a small bash script to locally change the default PATH of java. For example if the default version is jre7 and you want use jre8:
 +
 
 +
#!/bin/sh
 +
 +
export PATH=/usr/lib/jvm/java-8-openjdk/jre/bin/:$PATH
 +
exec /path/to/application "$@"
 +
 
 +
== Package pre-requisites to support {{Ic|archlinux-java}} ==
 +
 
 +
This section is targeted at packager willing to provide packages in [[AUR]] for an alternate JVM and be able to integrate with Arch Linux JVM scheme to use {{Ic|archlinux-java}}. To do so, packages should:
 +
 
 +
* Place all files under {{Ic|/usr/lib/jvm/java-${JAVA_MAJOR_VERSION}-${VENDOR_NAME} }}
 +
* Ensure all executables for which [https://www.archlinux.org/packages/extra/any/java-runtime-common/files/ java-runtime-common] and [https://www.archlinux.org/packages/extra/any/java-environment-common/files/ java-environment-common] provide links are available in the corresponding package
 +
* Ship links from {{Ic|/usr/bin}} to executables, only if these links do not already belong to [https://www.archlinux.org/packages/extra/any/java-runtime-common/files/ java-runtime-common] and [https://www.archlinux.org/packages/extra/any/java-environment-common/files/ java-environment-common]
 +
* Suffix man pages with {{Ic|-${VENDOR_NAME}${JAVA_MAJOR_VERSION} }} to prevent conflicts (see [https://www.archlinux.org/packages/extra/x86_64/jre8-openjdk/files/ jre8-openjdk file list] where man pages are suffixed with {{Ic|-openjdk8}})
 +
* Do not declare any [[PKGBUILD#conflicts|conflicts]] nor [[PKGBUILD#replaces|replaces]] with other JDKs, {{Ic|java-runtime}}, {{Ic|java-runtime-headless}} nor {{Ic|java-environment}}
 +
* Use script {{Ic|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 [https://projects.archlinux.org/svntogit/packages.git/tree/trunk?h=packages/java7-openjdk officially supported Java environment package sources] for examples
 +
 
 +
Also please note that:
 +
* Packages that need '''any''' Java environment should declare dependency on {{Ic|java-runtime}}, {{Ic|java-runtime-headless}} or {{Ic|java-environment}} as usual
 +
* Packages that need a '''specific Java vendor''' should declare dependency on the corresponding package
 +
* OpenJDK packages now declare {{Ic|1=provides="java-runtime-openjdk=${pkgver}"}} etc. This enables a third-party package to declare dependency on an OpenJDK without specifying a version
 +
 
 +
== Unsupported JVM from AUR ==
 +
 
 +
{{Warning|Packages in [[AUR]] may or may not support {{Ic|archlinux-java}}}}
 +
 
 +
=== Java SE ===
 +
 
 +
Several packages from [[AUR]] provide Oracle's implementations of JRE and JDK, but the main ones are {{AUR|jre}}, {{AUR|server-jre}} and {{AUR|jdk}}.
 +
 
 +
==== Java SE 9 ====
 +
 
 +
The development version of Java 9 includes {{AUR|jre-devel}} and {{AUR|jdk-devel}}.
 +
 
 +
==== Java SE 6/7 ====
 +
 
 +
Older versions include {{AUR|jre6}}/{{AUR|jre7}} and {{AUR|jdk6}}/{{AUR|jdk7}}.
 +
 
 +
==== 32-bit Java SE ====
 +
 
 +
Almost all of the above packages can be found in 32-bit by prefixing {{ic|bin32-}}, e.g. {{AUR|bin32-jre}} and {{AUR|bin32-jdk}}.
 +
 
 +
{{Note|These packages use {{ic|archlinux-java32}} ({{AUR|java32-runtime-common}}), which is separate from {{ic|archlinux-java}} ({{Pkg|java-runtime-common}}), but functions the same, by suffixing the Java links with {{ic|32}}, e.g. {{ic|java32}}.}}
 +
 
 +
=== Oracle JRockit ===
 +
 
 +
[http://www.oracle.com/technetwork/middleware/jrockit/overview/index.html JRockit] is Oracle's JIT version of Java, available as {{AUR|jrockit}}{{Broken package link|{{aur-mirror|jrockit}}}}.
 +
 
 +
=== VMkit ===
 +
 
 +
[http://vmkit.llvm.org/index.html VMkit] is an LLVM-based framework for JIT virtual machines. J3 is a JVM running on VMkit. The webpage can be found here: [http://vmkit.llvm.org/get_started.html vmkit]. J3 depends on the GNU classpath libraries, but may also work with the Apache class path libraries.
  
 
=== Parrot VM ===
 
=== Parrot VM ===
The [https://www.archlinux.org/packages/?q=parrot Parrot VM] offers experimental [http://trac.parrot.org/parrot/wiki/Languages support for Java] through two different methods: Either as a [http://code.google.com/p/parrot-jvm/ Java VM bytecode translator] or as a [https://github.com/chrisdolan/perk Java compiler targeting the Parrot VM]. Available in the AUR package {{AUR|parrot-git}}.
+
 
 +
[http://www.parrot.org/ Parrot] is a VM that offers experimental [http://trac.parrot.org/parrot/wiki/Languages support for Java] through two different methods: Either as a [http://code.google.com/p/parrot-jvm/ Java VM bytecode translator] or as a [https://github.com/chrisdolan/perk Java compiler targeting the Parrot VM]. {{Pkg|parrot}} is available in the [[official repositories]] and {{AUR|parrot-git}}{{Broken package link|{{aur-mirror|parrot-git}}}} in the AUR.
  
 
== Troubleshooting ==
 
== Troubleshooting ==
 +
 
=== MySQL ===
 
=== 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 instructions in [[MySQL#Enable remote access|MySQL article]].
 
  
=== Java sound with Pulseaudio ===
+
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]].
{{Note|This procedure is likely to be relevant for previous version of Java (Java 6) only.}}
+
  
By default, Java and [[Pulseaudio]] do not get along very well with each other, but this is easy to fix using padsp.
+
=== Impersonate another window manager ===
  
(These paths are correct for Sun's Java, you will need to change the paths for OpenJDK)
+
You may use the {{pkg|wmname}} from [http://tools.suckless.org/x/wmname 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]] or [[Ratpoison]].
  
First, rename the {{Ic|java}} binary to {{Ic|java.bin}}
+
  $ wmname LG3D
  # mv /opt/java/jre/bin/java /opt/java/jre/bin/java.bin
+
Then, create a new launcher script at {{Ic|/opt/java/jre/bin/java}}
+
#!/bin/sh
+
padsp /opt/java/jre/bin/java.bin "$@"
+
Finally, make the launcher script executable
+
# chmod +x /opt/java/jre/bin/java
+
You will need to redo this process on each update of Java.
+
  
You can also try replacing padsp with aoss, which can also fix it under standard ALSA as well as in Pulse, do what works best. I must warn everyone that these hacks sometimes work perfect, but are sometimes very unstable as well.
+
You must restart the application in question after issuing the wmname command.
=== Impersonate Another Window Manager ===
+
You may use the {{pkg|wmname}} from [http://tools.suckless.org/wmname  suckless.org] to make the JVM believe you are running a different window manager. This may solve a rendering issue of Java GUIs occuring in window managers like [[Awesome]] or [[Dwm]].
+
  
<pre>$ wmname LG3D</pre>
+
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 {{ic|LG3D}}, the non-re-parenting window manager [[wikipedia:Project_Looking_Glass|written by Sun, in Java]].
  
(You must restart the application in question after issuing the wmname command.)
+
=== Illegible fonts ===
  
This works because the JVM contains a hard-coded list of known non-re-parenting window managers.  For maximum irony, many users prefer to impersonate &ldquo;<code>LG3D</code>,&rdquo; the non-re-parenting window manager [http://en.wikipedia.org/wiki/Project_Looking_Glass written by Sun, in Java].
+
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 {{AUR|ttf-ms-fonts}} from the [[AUR]].
  
=== Fonts are Illegible ===
+
=== Missing text in some applications ===
In addition to the suggestions mentioned below in [[https://wiki.archlinux.org/index.php/Java#Better_font_rendering| tips and tricks]] some fonts may still not be legible afterwards. If this is the case there is a good chance ms-fonts are being used. Install {{AUR|ttf-ms-fonts}} from the AUR.
+
 
 +
If some applications are completely missing texts it may help to use the options under [[#Tips and tricks]] as suggested in {{Bug|40871}}.
 +
 
 +
=== Applications not resizing with WM, menus immediately closing  ===
 +
The standard Java GUI toolkit has a hard-coded list of "non-reparenting" window managers. If using one that is not on that list, there can be some problems with running some Java applications. One of the most common problems is "gray blobs", when the Java application renders as a plain gray box instead of rendering the GUI. Another one might be menus responding to your click, but closing immediately.
 +
 
 +
There are several things that may help:
 +
 
 +
* For {{Pkg|jre7-openjdk}} or {{Pkg|jre8-openjdk}}, append the line {{Ic|1=export _JAVA_AWT_WM_NONREPARENTING=1}} in {{ic|/etc/profile.d/jre.sh}}. Then, source the file {{ic|/etc/profile.d/jre.sh}} or log out and log back in.
 +
* For Oracle's JRE/JDK, use [http://xmonad.org/xmonad-docs/xmonad-contrib/XMonad-Hooks-SetWMName.html SetWMName.] However, its effect may be canceled when also using {{ic|XMonad.Hooks.EwmhDesktops}}. In this case, appending
 +
 
 +
  >> setWMName "LG3D"
 +
 
 +
to the {{ic|LogHook}} may help.
 +
 
 +
See [http://wiki.haskell.org/Xmonad/Frequently_asked_questions#Problems_with_Java_applications.2C_Applet_java_console] for more information.
  
 
== Tips and tricks ==
 
== 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.}}
 
{{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 [https://bbs.archlinux.org/viewtopic.php?id=72892 this forum post], a way to do it consists of adding the following line in your {{Ic|~/.bashrc}} (or {{Ic|/etc/profile.d/jre.sh}} to affect all users):
+
Behavior of most Java applications can be controlled by supplying predefined variables to Java runtime. From [https://bbs.archlinux.org/viewtopic.php?id=72892 this forum post], a way to do it consists of adding the following line in your {{Ic|~/.bashrc}} (or {{Ic|/etc/profile.d/jre.sh}} to affect programs that are not run by sourcing {{Ic|~/.bashrc}}, e.g., launching a program from Gnome's Applications view):
  
 
  export _JAVA_OPTIONS="-D'''<option 1>''' -D'''<option 2>'''..."
 
  export _JAVA_OPTIONS="-D'''<option 1>''' -D'''<option 2>'''..."
Line 92: Line 251:
  
 
=== Better font rendering ===
 
=== 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: {{Ic|1=awt.useSystemAAFontSettings=on}}, {{Ic|1=swing.aatext=true}}
+
 
 +
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: {{Ic|1=-Dawt.useSystemAAFontSettings=on}}, {{Ic|1=-Dswing.aatext=true}}
 +
 
 +
See [[Java Runtime Environment fonts]] for more detailed information.
 +
 
 +
=== Silence 'Picked up _JAVA_OPTIONS' message on command line ===
 +
 
 +
Setting the _JAVA_OPTIONS environment variables makes java (openjdk) write to stderr messages of the form: 'Picked up _JAVA_OPTIONS=...'. To supress those mesages
 +
in your terminal you can unset the environment variable in your shell startup files and alias java to pass those same options as command line arguments:
 +
 
 +
  _SILENT_JAVA_OPTIONS="$_JAVA_OPTIONS"
 +
  unset _JAVA_OPTIONS
 +
  alias java='java "$_SILENT_JAVA_OPTIONS"'
  
 
=== GTK LookAndFeel ===
 
=== GTK LookAndFeel ===
If your Java programs look ugly, you may want to set up the default look and feel for the swing components: {{Ic|1=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:
+
If your Java programs look ugly, you may want to set up the default look and feel for the swing components:
 +
 
 +
{{Ic|1=swing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel}}.
 +
 
 +
Some 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:
  
 
{{Ic|1=swing.crossplatformlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel}}.
 
{{Ic|1=swing.crossplatformlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel}}.
  
=== Symlinks change ===
+
{{Note|Forcing Java to use GTK may break some applications. Currently JRE/JDK (as of 8u60) is linked against GTK2 while many desktop applications start using GTK3. If a GTK3 app has Java plugins with GUI, the app is likely to crash when opening the Java GUI, as mixing GTK2 and GTK3 in the same process is not supported. Libreoffice 5.0 is an example for this.}}
Typically, OpenJDK installation creates symlinks in {{Ic|/usr/bin}} for java, javac ... etc. If you want to change these symlinks to any other JDK (e.g. if you installed environment-compat packages of Oracle JDK), [https://github.com/d1x/linux-utils/blob/master/replace-java-symlinks this] script might be handy for you.
+
 
 +
=== Non-reparenting window managers ===
 +
 
 +
Non-reparenting window managers user should set the following environment variable in their {{Ic|.xinitrc}}
 +
 
 +
  export _JAVA_AWT_WM_NONREPARENTING=1

Latest revision as of 13:40, 18 July 2016

From the Wikipedia article:

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.

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

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 variable). This can be done by sourcing /etc/profile from the command line or by logging out/in again of a Desktop Environment.

Two common packages named java-runtime-common and java-environment-common are automatically pulled as dependency and provide environment file /etc/profile.d/jre.sh. This file contains all JVM common environment variables. Package java-runtime-common also provides a utility script archlinux-java that can display and change the default Java environment. This script sets links /usr/lib/jvm/default and /usr/lib/jvm/default-runtime to point at a valid non-conflicting Java environment installed and Java runtime in /usr/lib/jvm/java-${JAVA_MAJOR_VERSION}-${VENDOR_NAME}. Most executables provided by the Java environment set have direct links from /usr/bin, others are available in $PATH.

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

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

OpenJFX

JavaFX is also available from the official repositories. It requires the OpenJDK 8.

Package name Use
java-openjfx Java OpenJFX 8 client application platform (open-source implementation of JavaFX)
java-openjfx-doc OpenJFX javadoc
java-openjfx-src OpenJFX sources

Flagging packages as out-of-date

Although the Arch Linux package releases may contain a reference to the proprietary versions the packages are based on, the open-source project has its own versioning scheme:

Switching between JVM

The 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
Tip: To see possible <JAVA_ENV_NAME> names, use archlinux-java status.

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 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" and "OpenJDK 8" will be considered first in this order, then un-official packages from AUR.

# archlinux-java fix

Launching an application with the non-default java version

If you want to launch an application with another version of java than the default one (for example if you have both version jre7 and jre8 installed on your system), you can wrap your application in a small bash script to locally change the default PATH of java. For example if the default version is jre7 and you want use jre8:

#!/bin/sh 

export PATH=/usr/lib/jvm/java-8-openjdk/jre/bin/:$PATH
exec /path/to/application "$@"

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 with Arch Linux JVM scheme to use archlinux-java. To do so, packages should:

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
  • OpenJDK packages now declare provides="java-runtime-openjdk=${pkgver}" etc. This enables a third-party package to declare dependency on an OpenJDK without specifying a version

Unsupported JVM from AUR

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

Java SE

Several packages from AUR provide Oracle's implementations of JRE and JDK, but the main ones are jreAUR, server-jreAUR and jdkAUR.

Java SE 9

The development version of Java 9 includes jre-develAUR and jdk-develAUR.

Java SE 6/7

Older versions include jre6AUR/jre7AUR and jdk6AUR/jdk7AUR.

32-bit Java SE

Almost all of the above packages can be found in 32-bit by prefixing bin32-, e.g. bin32-jreAUR and bin32-jdkAUR.

Note: These packages use archlinux-java32 (java32-runtime-commonAUR), which is separate from archlinux-java (java-runtime-common), but functions the same, by suffixing the Java links with 32, e.g. java32.

Oracle JRockit

JRockit is Oracle's JIT version of Java, available as jrockitAUR[broken link: archived in aur-mirror].

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[broken link: archived in aur-mirror] 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 or Ratpoison.

$ 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 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.

Applications not resizing with WM, menus immediately closing

The standard Java GUI toolkit has a hard-coded list of "non-reparenting" window managers. If using one that is not on that list, there can be some problems with running some Java applications. One of the most common problems is "gray blobs", when the Java application renders as a plain gray box instead of rendering the GUI. Another one might be menus responding to your click, but closing immediately.

There are several things that may help:

  • For jre7-openjdk or jre8-openjdk, append the line export _JAVA_AWT_WM_NONREPARENTING=1 in /etc/profile.d/jre.sh. Then, source the file /etc/profile.d/jre.sh or log out and log back in.
  • For Oracle's JRE/JDK, use SetWMName. However, its effect may be canceled when also using XMonad.Hooks.EwmhDesktops. In this case, appending
 >> setWMName "LG3D"

to the LogHook may help.

See [1] for more information.

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: -Dawt.useSystemAAFontSettings=on, -Dswing.aatext=true

See Java Runtime Environment fonts for more detailed information.

Silence 'Picked up _JAVA_OPTIONS' message on command line

Setting the _JAVA_OPTIONS environment variables makes java (openjdk) write to stderr messages of the form: 'Picked up _JAVA_OPTIONS=...'. To supress those mesages in your terminal you can unset the environment variable in your shell startup files and alias java to pass those same options as command line arguments:

 _SILENT_JAVA_OPTIONS="$_JAVA_OPTIONS"
 unset _JAVA_OPTIONS
 alias java='java "$_SILENT_JAVA_OPTIONS"'

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 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.

Note: Forcing Java to use GTK may break some applications. Currently JRE/JDK (as of 8u60) is linked against GTK2 while many desktop applications start using GTK3. If a GTK3 app has Java plugins with GUI, the app is likely to crash when opening the Java GUI, as mixing GTK2 and GTK3 in the same process is not supported. Libreoffice 5.0 is an example for this.

Non-reparenting window managers

Non-reparenting window managers user should set the following environment variable in their .xinitrc

 export _JAVA_AWT_WM_NONREPARENTING=1