Difference between revisions of "Java"

From ArchWiki
Jump to: navigation, search
(OpenJDK JVM)
(添加了中文界面)
 
(265 intermediate revisions by 91 users not shown)
Line 1: Line 1:
[[Category:Other desktop user's resources (English)]]
+
[[Category:Programming languages]]
{{i18n|Java}}
+
[[cs:Java]]
 +
[[de:Java]]
 +
[[es:Java]]
 +
[[fa:Java]]
 +
[[fr:Java]]
 +
[[it:Java]]
 +
[[ja:Java]]
 +
[[pt:Java]]
 +
[[ru:Java]]
 +
[[zh-hans:Java]]
 +
{{Related articles start}}
 +
{{Related|Java Package Guidelines}}
 +
{{Related|Java Runtime Environment Fonts}}
 +
{{Related articles 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)]]
+
From the [[Wikipedia:Java (programming language)|Wikipedia article]]:
  
==Installation==
+
: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 ([[Wikipedia:Java virtual machine|JVM]]) regardless of computer architecture.
There are two Java Virtual Machines in the Arch repositories that can be installed on your system — the OpenJDK open source implementation and Sun's closed source implementation. Arch recommends [http://openjdk.java.net/ OpenJDK.]. Additional Java implementations are available via the [[AUR]].
 
  
===OpenJDK JVM===
+
Arch Linux officially supports the open source [http://openjdk.java.net/ OpenJDK] versions 7, 8, and 10. 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.
To be able to run Java programs, you can install OpenJDK from the repositories. This also installs a Java Development Kit.
 
  
# pacman -Syu openjdk6
+
== Installation ==
  
You will likely need the {{Package Official|icedtea-web}} package for Java functionality in Firefox as suggested in this thread: https://bbs.archlinux.org/viewtopic.php?pid=895343
+
{{Note|
 +
* Arch Linux officially only supports the [[#OpenJDK|OpenJDK]] implementation.
 +
* 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.}}
  
  # pacman -S icedtea-web
+
Two ''common'' packages are respectively pulled as dependency, named {{Pkg|java-runtime-common}} (containing common files for Java Runtime Environments) and {{Pkg|java-environment-common}} (containing common files for Java Development Kits). The provided environment file {{Ic|/etc/profile.d/jre.sh}} points to a linked location {{Ic|/usr/lib/jvm/default/bin}}, set by the {{ic|archlinux-java}} helper script. The links {{Ic|/usr/lib/jvm/default}} and {{Ic|/usr/lib/jvm/default-runtime}} should '''always''' be edited with {{Ic|archlinux-java}}. This is used to display and point to a working default Java environment in {{Ic|<nowiki>/usr/lib/jvm/java-${JAVA_MAJOR_VERSION}-${VENDOR_NAME}</nowiki>}} or a Java runtime in {{Ic|<nowiki>/usr/lib/jvm/java-${JAVA_MAJOR_VERSION}-${VENDOR_NAME}/jre</nowiki>}}.
  
{{Note | If you experience any problems with the Java plugin, you can try the solution suggested for the Firefox Flash plugin: [[Flash#Plugins_are_installed_but_not_working]]}}
+
Most executables of the Java installation are provided by direct links in {{Ic|/usr/bin}}, while others are available in {{Ic|$PATH}}.
  
{{Note | If you use a non-reparenting window manager, you should uncomment the corresponding line in {{filename|/etc/profile.d/openjdk6.sh}}}}
+
{{Warning|File {{Ic|/etc/profile.d/jdk.sh}} is not provided any more by any package.}}
  
===Oracle JVM===
+
=== OpenJDK ===
  
{{Note|Arch Linux (and any other GNU/Linux distribution) can no longer package the Oracle implementation of Java. Read [http://mailman.archlinux.org/pipermail/arch-general/2011-August/021671.html here] for the announcement. Alternatively, you can download ''openjdk6'' from [extra] or use ''jre'' or ''jre6'' from the [[AUR]].<br />
+
[[Wikipedia:OpenJDK|OpenJDK]] is an open-source implementation of the Java Platform, Standard Edition (Java SE).
The rest of this section is left for reference purposes only.}}
 
  
To be able to run Java programs, you can install the Oracle JRE from the repositories:
+
;Headless JRE: The minimal Java runtime - needed for executing non GUI Java programs.
# pacman -S jre
+
;Full JRE: Full Java runtime environment - needed for executing Java GUI programs, depends on headless JRE.
 +
;JDK:[[Wikipedia:Java Development Kit|Java Development Kit]] - needed for Java development, depends on full JRE.
  
You will need to log out and log in again, or add {{Filename|/opt/java/jre/bin/}} to $PATH to use the ''java'' command.  
+
{| class="wikitable"
 +
! Version !! Headless JRE !! Full JRE !! JDK !! Documentation !! Sources
 +
|-
 +
| [http://openjdk.java.net/projects/jdk/10/ OpenJDK 10] || {{Pkg|jre10-openjdk-headless}} || {{Pkg|jre10-openjdk}} || {{Pkg|jdk10-openjdk}} || {{Pkg|openjdk10-doc}} || {{Pkg|openjdk10-src}}
 +
|-
 +
| [http://openjdk.java.net/projects/jdk8/ OpenJDK 8] || {{Pkg|jre8-openjdk-headless}} || {{Pkg|jre8-openjdk}} || {{Pkg|jdk8-openjdk}} || {{Pkg|openjdk8-doc}} || {{Pkg|openjdk8-src}}
 +
|-
 +
| [http://openjdk.java.net/projects/jdk7/ OpenJDK 7] || {{Pkg|jre7-openjdk-headless}} || {{Pkg|jre7-openjdk}} || {{Pkg|jdk7-openjdk}} || {{Pkg|openjdk7-doc}} || {{Pkg|openjdk7-src}}
 +
|}
  
To create Java programs, you can install the Oracle JDK from the repositories:
+
{{App|IcedTea-Web|Java Web Start and the deprecated Java browser plugin.|https://icedtea.classpath.org/wiki/IcedTea-Web|{{Pkg|icedtea-web}}}}
# pacman -S jdk
+
{{App|OpenJFX 8|The open-source implementation of JavaFX. You [https://wiki.openjdk.java.net/display/OpenJFX/Repositories+and+Releases do not need] to install this package if you are making use of Java SE (the Oracle's implementation of JRE and JDK described below). This package only concerns users of the open source implementation of Java (OpenJDK project).|http://openjdk.java.net/projects/openjfx/|{{Pkg|java-openjfx}}, {{Pkg|java-openjfx-doc}}, {{Pkg|java-openjfx-src}}}}
 +
{{App|OpenJDK EA|Oracle's OpenJDK Early-Access Build for latest developement version.|http://jdk.java.net|{{AUR|openjdk-devel}}}}
 +
{{App|OpenJFX EA|Oracle's OpenJFX Early-Access Build for latest developement version.|http://jdk.java.net/openjfx/|{{AUR|java-openjfx-devel}}}}
  
{{Note|If you experience any problems with the Java plugin, you can try the solution suggested for the Firefox Flash plugin: [[Flash#Plugins_are_installed_but_not_working]]}}
+
=== Other implementations ===
  
{{Note|For Oracle Java, you will need to source {{Filename|/etc/profile}} in order to for the JAVA_HOME path to point to the correct place}}
+
{{App|Java SE|Oracle's implementation of JRE and JDK.|http://www.oracle.com/technetwork/java/javase/downloads/index.html|{{AUR|jre}} {{AUR|jre9}} {{AUR|jre8}} {{AUR|jre7}} {{AUR|jre6}} {{AUR|jdk}} {{AUR|jdk9}} {{AUR|jdk8}} {{AUR|jdk7}} {{AUR|jdk6}} {{AUR|jdk5}} {{AUR|jdk-devel}}}}
# source /etc/profile
+
{{App|OpenJ9|Eclipse's implementation of JRE, contributed by IBM.|https://www.eclipse.org/openj9/|{{AUR|jdk9-openj9-bin}} {{AUR|jdk8-openj9-bin}}}}
 +
{{App|IBM J9|IBM's implementation of the eighth edition of JRE.|https://developer.ibm.com/javasdk/|{{AUR|jdk8-j9-bin}} {{AUR|jdk7-j9-bin}} {{AUR|jdk7r1-j9-bin}}}}
 +
{{App|Parrot VM|a VM with experimental support for Java [http://trac.parrot.org/parrot/wiki/Languages] 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].|http://www.parrot.org/|{{AUR|parrot}}}}
  
===Apache Harmony===
+
{{Note|32-bit versions of Java SE can be found by prefixing {{ic|bin32-}}, e.g. {{AUR|bin32-jre}} and {{AUR|bin32-jdk}}. They use {{AUR|java32-runtime-common}}, which functions as {{Pkg|java-runtime-common}} by suffixing with {{ic|32}}, e.g. {{ic|java32}}. The same analogy applies to {{AUR|java32-environment-common}}, which is only used by 32-bit JDK packages.}}
[http://harmony.apache.org/ Apache Harmony] is a clean room re-implementation of the Java language and comes under the Free Apache license. A binary re-distribution of the [http://aur.archlinux.org/packages.php?ID=51548 Harmony JRE] can be found in the AUR.
 
  
===Kaffe===
+
== Switching between JVM ==
[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 [https://aur.archlinux.org/packages.php?ID=50282 AUR]. The Kaffe VM is redistributed according to the GPL license.
 
  
===BEA JRockit JIT JVM (+JDK)===
+
The helper script {{Ic|archlinux-java}} provides such functionalities:
You can also install JIT version of Java from the [[AUR]].
 
[http://aur.archlinux.org/packages.php?ID=11190 jrockit]
 
  
===VMkit LLVM-based JIT VM===
+
archlinux-java <COMMAND>
[http://vmkit.llvm.org/index.html VMkit] is an LLVM-based framework for JIT virtual machines. J3 is a JVM running on VMkit. A broken build for VMkit can be found in the [http://aur.archlinux.org/packages.php?ID=51206 AUR]. J3 depends on the GNU classpath libraries, but may also work with the Apache class path libraries.
+
 +
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
  
===Parrot VM===
+
=== List compatible Java environments installed ===
The [http://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].
 
  
==Java sound with Pulseaudio==
+
$ archlinux-java status
By default, Java and [[Pulseaudio]] do not get along very well with each other, but this is easy to fix using padsp.
 
  
(These paths are correct for Sun's Java, you will need to change the paths for OpenJDK)
+
Example:
  
First, rename the {{filename|java}} binary to {{filename|java.bin}}
+
  $ archlinux-java status
  # mv /opt/java/jre/bin/java /opt/java/jre/bin/java.bin
+
  Available Java environments:
Then, create a new launcher script at {{filename|/opt/java/jre/bin/java}}
+
  java-7-openjdk (default)
  #!/bin/sh
+
  java-8-openjdk/jre
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.
+
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.
  
==Additional notes==
+
=== Change default Java environment ===
===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 need to edit {{filename|/etc/mysql/my.cnf}} and comment out the line looking like this:
 
  skip-networking
 
  
[https://wiki.archlinux.org/index.php/MySQL Read the article about MySQL for more information.]
+
# 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 it. 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 package "OpenJDK 8" will be considered first in this order, then other installed environments.
 +
 
 +
# 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}} ==
 +
 
 +
{{Note|This info also applies to {{ic|archlinux32-java}} for 32-bit Java packages, with the proper inclusion of {{ic|32}} to the package/executable names, where applicable.}}
 +
 
 +
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
 +
 
 +
== 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 {{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]].
 +
 
 +
$ 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 {{ic|LG3D}}, the non-re-parenting window manager [[wikipedia:Project_Looking_Glass|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 {{AUR|ttf-ms-fonts}} 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 {{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 [https://wiki.haskell.org/Xmonad/Frequently_asked_questions#Using_SetWMName 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.
 +
 
 +
=== System freezes when debugging JavaFX Applications ===
 +
 
 +
If your system freezes while debugging a JavaFX Application, you can try to supply the JVM option {{Ic|1=-Dsun.awt.disablegrab=true}}.
 +
 
 +
See http://bugs.java.com/view_bug.do?bug_id=6714678
 +
 
 +
=== JavaFX's MediaPlayer constructor throws an exception ===
 +
 
 +
Creating instance of MediaPlayer class from JavaFX's sound modules might throw following exception (both Oracle JDK and OpenJDK)
 +
 
 +
... (i.e. FXMLLoader construction exceptions) ...
 +
Caused by: MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!
 +
  at javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:146)
 +
  at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:511)
 +
  at javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:414)
 +
  at <constructor call>
 +
...
 +
 
 +
which is a result of some incompatibilities of JavaFX with modern {{pkg|ffmpeg}} build delivered within Arch Linux repository.
 +
 
 +
Working solution is to install {{AUR|ffmpeg-compat-55}}.
 +
 
 +
See http://www.reddit.com/r/archlinux/comments/70o8o6/using_a_javafx_mediaplayer_in_arch/
 +
 
 +
=== Java applications cannot open external links ===
 +
If a Java application is not able to open a link to, for example, your web browser, install {{Pkg|gvfs}}. This is required by the Desktop.Action.BROWSE method. See [https://bugs.launchpad.net/ubuntu/+source/openjdk-8/+bug/1574879/comments/2]
 +
 
 +
== 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 [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>'''..."
 +
 
 +
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: {{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 suppress those messages 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:
 +
 
 +
{{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}}.
 +
 
 +
==== GTK3 Support ====
 +
 
 +
In Java releases prior to version 9, the GTK LookAndFeel is linked against GTK2, whilst many newer desktop applications use GTK3. This incompatibility between GTK versions may break applications utilizing Java plugins with GUI, as the mixing of GTK2 and GTK3  in the same process is not supported (for example, LibreOffice 5.0).
 +
 
 +
Since [http://openjdk.java.net/jeps/283 Java 9], the GTK LookAndFeel can be run against GTK versions {{Ic|2}}, {{Ic|2.2}} and {{Ic|3}}, defaulting to GTK2. This can be overridden by setting the following property:
 +
 
 +
{{Ic|1=jdk.gtk.version=3}}
 +
 
 +
=== Better 2D performance ===
 +
 
 +
Switching to OpenGL-based hardware acceleration pipeline will improve 2D performance
 +
 
 +
export _JAVA_OPTIONS='-Dsun.java2d.opengl=true'
 +
 
 +
{{Note|Enabling this option may cause the UI of software like JetBrains IDEs misbehave, making them drawing windows, popups and toolbars partially.}}
 +
 
 +
=== Non-reparenting window managers / Grey window / Programs not drawing properly ===
 +
 
 +
Non-reparenting window managers user should set the following environment variable in their {{Ic|.xinitrc}}
 +
 
 +
export _JAVA_AWT_WM_NONREPARENTING=1
 +
 
 +
Not setting this may result in java programs not being drawn properly.
 +
 
 +
== See also ==
 +
 
 +
* [http://math.hws.edu/javanotes/ Introduction to Programming Using Java]

Latest revision as of 13:00, 15 October 2018

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, 8, and 10. 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:
  • Arch Linux officially only supports the OpenJDK implementation.
  • 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 are respectively pulled as dependency, named java-runtime-common (containing common files for Java Runtime Environments) and java-environment-common (containing common files for Java Development Kits). The provided environment file /etc/profile.d/jre.sh points to a linked location /usr/lib/jvm/default/bin, set by the archlinux-java helper script. The links /usr/lib/jvm/default and /usr/lib/jvm/default-runtime should always be edited with archlinux-java. This is used to display and point to a working default Java environment in /usr/lib/jvm/java-${JAVA_MAJOR_VERSION}-${VENDOR_NAME} or a Java runtime in /usr/lib/jvm/java-${JAVA_MAJOR_VERSION}-${VENDOR_NAME}/jre.

Most executables of the Java installation are provided by direct links in /usr/bin, while others are available in $PATH.

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

OpenJDK

OpenJDK is an open-source implementation of the Java Platform, Standard Edition (Java SE).

Headless JRE
The minimal Java runtime - needed for executing non GUI Java programs.
Full JRE
Full Java runtime environment - needed for executing Java GUI programs, depends on headless JRE.
JDK
Java Development Kit - needed for Java development, depends on full JRE.
Version Headless JRE Full JRE JDK Documentation Sources
OpenJDK 10 jre10-openjdk-headless jre10-openjdk jdk10-openjdk openjdk10-doc openjdk10-src
OpenJDK 8 jre8-openjdk-headless jre8-openjdk jdk8-openjdk openjdk8-doc openjdk8-src
OpenJDK 7 jre7-openjdk-headless jre7-openjdk jdk7-openjdk openjdk7-doc openjdk7-src

IcedTea-Web — Java Web Start and the deprecated Java browser plugin.

https://icedtea.classpath.org/wiki/IcedTea-Web || icedtea-web

OpenJFX 8 — The open-source implementation of JavaFX. You do not need to install this package if you are making use of Java SE (the Oracle's implementation of JRE and JDK described below). This package only concerns users of the open source implementation of Java (OpenJDK project).

http://openjdk.java.net/projects/openjfx/ || java-openjfx, java-openjfx-doc, java-openjfx-src

OpenJDK EA — Oracle's OpenJDK Early-Access Build for latest developement version.

http://jdk.java.net || openjdk-develAUR

OpenJFX EA — Oracle's OpenJFX Early-Access Build for latest developement version.

http://jdk.java.net/openjfx/ || java-openjfx-develAUR

Other implementations

Java SE — Oracle's implementation of JRE and JDK.

http://www.oracle.com/technetwork/java/javase/downloads/index.html || jreAUR jre9AUR jre8AUR jre7AUR jre6AUR jdkAUR jdk9AUR jdk8AUR jdk7AUR jdk6AUR jdk5AUR jdk-develAUR

OpenJ9 — Eclipse's implementation of JRE, contributed by IBM.

https://www.eclipse.org/openj9/ || jdk9-openj9-binAUR jdk8-openj9-binAUR

IBM J9 — IBM's implementation of the eighth edition of JRE.

https://developer.ibm.com/javasdk/ || jdk8-j9-binAUR jdk7-j9-binAUR jdk7r1-j9-binAUR

Parrot VM — a VM with experimental support for Java [1] through two different methods: either as a Java VM bytecode translator, or as a Java compiler targeting the Parrot VM.

http://www.parrot.org/ || parrotAUR
Note: 32-bit versions of Java SE can be found by prefixing bin32-, e.g. bin32-jreAUR and bin32-jdkAUR. They use java32-runtime-commonAUR, which functions as java-runtime-common by suffixing with 32, e.g. java32. The same analogy applies to java32-environment-commonAUR, which is only used by 32-bit JDK packages.

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 it. 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 package "OpenJDK 8" will be considered first in this order, then other installed environments.

# 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

Note: This info also applies to archlinux32-java for 32-bit Java packages, with the proper inclusion of 32 to the package/executable names, where applicable.

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

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 [2] for more information.

System freezes when debugging JavaFX Applications

If your system freezes while debugging a JavaFX Application, you can try to supply the JVM option -Dsun.awt.disablegrab=true.

See http://bugs.java.com/view_bug.do?bug_id=6714678

JavaFX's MediaPlayer constructor throws an exception

Creating instance of MediaPlayer class from JavaFX's sound modules might throw following exception (both Oracle JDK and OpenJDK)

... (i.e. FXMLLoader construction exceptions) ...
Caused by: MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!
 at javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:146)
 at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:511)
 at javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:414)
 at <constructor call>
...

which is a result of some incompatibilities of JavaFX with modern ffmpeg build delivered within Arch Linux repository.

Working solution is to install ffmpeg-compat-55AUR.

See http://www.reddit.com/r/archlinux/comments/70o8o6/using_a_javafx_mediaplayer_in_arch/

Java applications cannot open external links

If a Java application is not able to open a link to, for example, your web browser, install gvfs. This is required by the Desktop.Action.BROWSE method. See [3]

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 suppress those messages 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.

GTK3 Support

In Java releases prior to version 9, the GTK LookAndFeel is linked against GTK2, whilst many newer desktop applications use GTK3. This incompatibility between GTK versions may break applications utilizing Java plugins with GUI, as the mixing of GTK2 and GTK3 in the same process is not supported (for example, LibreOffice 5.0).

Since Java 9, the GTK LookAndFeel can be run against GTK versions 2, 2.2 and 3, defaulting to GTK2. This can be overridden by setting the following property:

jdk.gtk.version=3

Better 2D performance

Switching to OpenGL-based hardware acceleration pipeline will improve 2D performance

export _JAVA_OPTIONS='-Dsun.java2d.opengl=true'
Note: Enabling this option may cause the UI of software like JetBrains IDEs misbehave, making them drawing windows, popups and toolbars partially.

Non-reparenting window managers / Grey window / Programs not drawing properly

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

export _JAVA_AWT_WM_NONREPARENTING=1

Not setting this may result in java programs not being drawn properly.

See also