Java Specifics in Fedora for Users and Developers
This section contains information about default Java implementation in Fedora, switching between different Java runtime environments and about few useful tools which can be used during packaging / development.
Java implementation in Fedora
Fedora ships with an open-source reference implementation of Java Standard Edition called OpenJDK. OpenJDK provides Java Runtime Environment for Java applications and set of development tools for Java developers.
From users point of view, java
command is probably the most interesting. It is a Java application launcher which spawns Java Virtual Machine (JVM), loads specified .class
file and executes its main method.
Here is an example how to run sample Java project from section [_example_java_project]:
$ java org/fedoraproject/helloworld/HelloWorld.class
OpenJDK provides a lot of interesting tools for Java developers:
-
javac
is a Java compiler which translates source files to Java bytecode, which can be later interpreted by JVM. -
jdb
is a simple command-line debugger for Java applications. -
javadoc
is a tool for generating Javadoc documentation. -
javap
can be used for disassembling Java class files.
Switching between different Java implementations
Users and developers may want to have multiple Java environments installed at the same time. It is possible in Fedora, but only one of them can be default Java environment in system. Fedora uses alternatives
for switching between different installed JREs/JDKs.
# alternatives --config java There are 3 programs which provide 'java'. Selection Command ----------------------------------------------- 1 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.2.0.8-1.fc35.x86_64/bin/java) *+ 2 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.14.1.1-5.fc35.x86_64/bin/java) 3 java-latest-openjdk.x86_64 (/usr/lib/jvm/java-18-openjdk-18.0.1.0.10-1.rolling.fc35.x86_64/bin/java) Enter to keep the current selection[+], or type selection number:
Example above shows how to chose default Java environment. java
command will then point to the Java implementation provided by given JRE.
See |
Developers may want to use Java compiler from different JDK. This can be achieved with alternatives --config javac
.
Building classpath with build-classpath
Most of the Java application needs to specify classpath in order to work correctly. Fedora contains several tools which make working with classpaths easier.
build-classpath
- this tool takes JAR filenames or artifact coordinates as arguments and translates them to classpath-like string. See the following example:
$ build-classpath log4j junit org.ow2.asm:asm
/usr/share/java/log4j.jar:/usr/share/java/junit.jar:/usr/share/java/objectweb-asm4/asm.jar
log4j
corresponds to log4j.jar
stored in %{_javadir}
. If the JAR file is stored in subdirectory under %{_javadir}
, it is neccessary to pass subdirectory/jarname
as an argument to build-classpath
. Example:
$ build-classpath httpcomponents/httpclient.jar
/usr/share/java/httpcomponents/httpclient.jar
Building JAR repository with build-jar-repository
Another tool is build-jar-repository
. It can fill specified directory with symbolic / hard links to specified JAR files. Similarly to build-classpath
, JARs can be identified by their names or artifact coordintes.
$ build-jar-repository my-repo log4j httpcomponents/httpclient junit:junit
$ ls -l my-repo/
total 0
lrwxrwxrwx. 1 msrb msrb 45 Oct 29 10:39 [httpcomponents][httpclient].jar -> /usr/share/java/httpcomponents/httpclient.jar
lrwxrwxrwx. 1 msrb msrb 25 Oct 29 10:39 [junit:junit].jar -> /usr/share/java/junit.jar
lrwxrwxrwx. 1 msrb msrb 25 Oct 29 10:39 [log4j].jar -> /usr/share/java/log4j.jar
Similar command rebuild-jar-repository
can be used to rebuild JAR repository previously built by build-jar-repository
. See man rebuild-jar-repository
for more information.
build-classpath-directory
is a small tool which can be used to build classpath string from specified directory.
$ build-classpath-directory /usr/share/java/xstream
/usr/share/java/xstream/xstream-benchmark.jar:/usr/share/java/xstream/xstream.jar
:/usr/share/java/xstream/xstream-hibernate.jar
Want to help? Learn how to contribute to Fedora Docs ›