YoLinux Tutorial: Java on Linux
This covers Java development and execution on Linux. Both commercially supported products and Open Source GPL’d software will be covered.
Related YoLinux Tutorials:
- Red Hat/Fedora/CentOS:
- yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 java-1.8.0-openjdk-javadoc
Server only, no GUI: yum install java-1.8.0-openjdk-headless.x86_64 - yum install java-1.7.0-openjdk.x86_64 java-1.7.0-openjdk-devel.x86_64 java-1.7.0-openjdk-javadoc
Server only, no GUI: yum install java-1.7.0-openjdk-headless.x86_64 - yum install java-1.6.0-openjdk.x86_64 java-1.6.0-openjdk-devel.x86_64 java-1.6.0-openjdk-javadoc
- yum install alternatives (need this to select the default if installing more than one version of java)
- 64 bit: apt-get install openjdk-7-jdk openjdk-7-jre-headless
- 32 bit: apt-get install openjdk-7-jdk:i386 openjdk-7-jre-headless:i386
Oracle has various bundled Java packages available for download: Java Standard Edition (SE), Java Runtime Environment (JRE), Software Development Kit (SDK), EE (Enterprise Edition), JavaFX, Netbeans (IDE) or just the bundle: Java SE and JDK development kit, used in this example. Note that the Java Runtime Environment (JRE) by itself is not for software development but only a JVM to run Java applications.
The Java «Enterprise Edition» with Glassfish application server is also available for Java web services development.
The installation of multiple versions of Java on different machines will require a «.bashrc» file which can locate the appropriate CLASSPATH.
.. . # # Java # if [ -d /opt/java/latest ] then PATH=/opt/java/latest/bin:$PATH export JAVA_HOME=/opt/java/latest export CLASSPATH=/opt/java/latest/lib/tools.jar:./ elif [ -d /opt/java/jdk1.6.0_01 ] then PATH=/opt/java/jdk1.6.0_01/bin:$PATH export JAVA_HOME=/opt/java/jdk1.6.0_01 export CLASSPATH=/opt/java/jdk1.6.0_01/lib/tools.jar:./ elif [ -d /usr/java/jdk1.6.0 ] then PATH=/usr/java/jdk1.6.0/bin:$PATH export JAVA_HOME=/usr/java/jdk1.6.0 export CLASSPATH=/usr/java/jdk1.6.0/lib/tools.jar:./ elif [ -d /usr/lib/jvm/jre-1.4.2-sun/bin ] then PATH=/usr/lib/jvm/jre-1.4.2-sun/bin:$PATH export JAVA_HOME=/usr/lib/jvm/jre-1.4.2-sun export CLASSPATH=/usr/lib/jvm/jre-1.4.2-sun/lib/tools.jar:/usr/lib/jvm/jre-1.4.2-sun/lib/rt.jar:./ fi . ..
Where the following soft link is typical: ln -s /opt/java/jdk1.6.0_01 /opt/java/latest
Must include «./» in CLASSPATH. This is necessary in 1.4.x and later.Note that admins often likes to preserve a common path between systems and the path /opt/java/latest or /usr/java/latest is chosen as a default for all systems. It is then often common to generate soft links to wherever the JDK resides. Eg when using the RHEL OpenJDK:
ln -s /usr/lib/jvm/java-1.7.0-openjdk.x86_64/jre /opt/java/latestJava SDK 1.6:
There is a choice of running 32 bit Java on a 32 bit or 64 bit Linux platform or running 64 bit Java on a 64 Linux bit platform.
- compressed tar file: jdk-7u4-linux-x64.tar.gz
Install:- cd /opt/java
- tar xzf ~/Downloads/jdk-7u4-linux-x64.tar.gz
This creates: /opt/java/jdk1.7.0_04/.
- self extracting binary RPM jdk-6u17-linux-x64-rpm.bin
Install:- Allow permissions to execute the binary package: chmod +x jdk-6u17-linux-x64-rpm.bin
- Execute the binary package: ./jdk-6u17-linux-x64-rpm.bin
- Creates RPMs:
- jdk-6u17-linux-amd64.rpm
- sun-javadb-common-10.4.2-1.1.i386.rpm
- sun-javadb-core-10.4.2-1.1.i386.rpm
- sun-javadb-client-10.4.2-1.1.i386.rpm
- sun-javadb-demo-10.4.2-1.1.i386.rpm
- sun-javadb-docs-10.4.2-1.1.i386.rpm
- sun-javadb-javadoc-10.4.2-1.1.i386.rpm
- Installs RPMs to:
- /usr/java/jdk1.6.0_17
- /etc/.java/.systemPrefs
- /etc/init.d/jexec
- Derby Database support installs to: /opt/sun/javadb/
Note: all of the sun-javadb RPMs are related to Derby, the all Java database.
- self extracting binary bundle jdk-6u17-linux-x64.bin
This unpacks to a local directory ./jdk1.6.0_17/
Do this in directory /usr/java to get a similar installation to the RPM result /usr/java/jdk1.6.0_17
64 bit Workstation Tip: This option of installing the bundle to a directory of your choice is my preferred installation method for 64 bit Linux workstations as it allows me to install both a 32 bit version and a 64 bit version of Java. On 64 bit Linux Workstations it is typical to use a 32 bit browser which is compatible with the 32 bit Adobe Flash plug-in. Also, only the 32 bit version of Java offers a browser plug-in for applet support.
Install to:- 64 bit Java: /usr/java/64/. (or /opt/java/64/. )
- 32 bit Java: /usr/java/. (or /opt/java/. )
On 64-bit Linux platforms the 32-bit JDK and 64-bit JDK cannot co-exist when installed from the RPM bundles. One must install one or the other but not both RPMs. You can install as many versions of the «.bin» bundle into distinct directories.
chmod +x jdk-6uxx-linux-xxxx-rpm.bin ./jdk-6uxx-linux-xxxx-rpm.bin . This is where you agree to their license. Press the space bar to scroll down in "More". Do you agree the the above license terms? [yes or no] yes .
Test program: HelloWorld.java
Compile: javac HelloWorld.java
(or /usr/java/latest/bin/javac HelloWorld.java)
Note that the file name and the class name are the same. This became a requirement back in SDK 1.4. The result of the compile is the file: Test.class[prompt]$ java HelloWorld
Hello world[Potential Pitfall] : Red Hat Enterprise Linux and other Linux systems which install GNU Java may find that this conflicts with the Sun Java installation. You may have to remove this (for example RHEL4):
rpm -e java-1.4.2-gcj-compat-1.4.2.0-27jpp java-1.4.2-gcj-compat-devel-1.4.2.0-27jpp.noarchThe newer RHEL5 uses OpenJDK and is a valid Java distribution based on Java 1.6.0.
Install: yum install javaThe Java SDK includes the following commands:
- javac: Linux Java compiler (i.e. javac program-name.java)
- java: Byte code interpreter / Java program launcher. (i.e. java program-name Do not include «.class» extension.)
Test version: java -version - appletviewer: Views Java applet embedded in html file. (appletviewer myfile.html)
- javaws: Java Web Start application manager. Java application handler for browser.
(Also see YoLinux Mozilla configuration tutorial) - javadoc: Generate API documentation from tagged comments.
- javah: Creates C header and stub files for Java class.
- javap: Java file disassembler
- jdb: Java debugger
- jar: JAR archive file generation tool.
- JDK Tools and Utilities: Full list of tools and information.
While files may be compiled using the command «javac», applications can quickly become too large and unwieldy for individual command line operations. Apache ANT is a Java build tool to compile and generate Java applications.
File: build.xml (example to build the previous HelloWorld example)
Builds and runs the application HelloWorld. Optionally one may generate a JAR (Java Archive) file as a methodology for releasing an application.
File: build.xml (example to build a JAR file for the previous HelloWorld example)
Builds a jar file and runs it. " /> "> «Hello World!» for Solaris OS, Linux, and Mac OS X
It’s time to write your first application! These detailed instructions are for users of Solaris OS, Linux, and Mac OS X. Instructions for other platforms are in «Hello World!» for Microsoft Windows and «Hello World!» for the NetBeans IDE.
A Checklist
To write your first program, you’ll need:
- The Java SE Development Kit 8 (JDK 8) You can download the version for Solaris OS, Linux, or Mac OS X. (Make sure you download the JDK, not the JRE.) Consult the installation instructions.
- A text editor In this example, we’ll use Pico, an editor available for many UNIX-based platforms. You can easily adapt these instructions if you use a different text editor, such as vi or emacs .
These two items are all you’ll need to write your first application.
Creating Your First Application
Your first application, HelloWorldApp , will simply display the greeting «Hello world!». To create this program, you will:
- Create a source file A source file contains code, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and edit source files.
- Compile the source file into a .class file The Java programming language compiler ( javac ) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this .class file are known as bytecodes.
- Run the program The Java application launcher tool ( java ) uses the Java virtual machine to run your application.
Create a Source File
To create a source file, you have two options:
- You can save the file HelloWorldApp.java on your computer and avoid a lot of typing. Then, you can go straight to Compile the Source File.
- Or, you can use the following (longer) instructions.
First, open a shell, or «terminal,» window.
When you first bring up the prompt, your current directory will usually be your home directory. You can change your current directory to your home directory at any time by typing cd at the prompt and then pressing Return.
The source files you create should be kept in a separate directory. You can create a directory by using the command mkdir . For example, to create the directory examples/java in the /tmp directory, use the following commands:
cd /tmp mkdir examples cd examples mkdir java
To change your current directory to this new directory, you then enter:
Now you can start creating your source file.
Start the Pico editor by typing pico at the prompt and pressing Return. If the system responds with the message pico: command not found , then Pico is most likely unavailable. Consult your system administrator for more information, or use another editor.
When you start Pico, it’ll display a new, blank buffer. This is the area in which you will type your code.
Type the following code into the new buffer:
/** * The HelloWorldApp class implements an application that * simply prints "Hello World!" to standard output. */ class HelloWorldApp < public static void main(String[] args) < System.out.println("Hello World!"); // Display the string. >>
Be Careful When You Type
Note: Type all code, commands, and file names exactly as shown. Both the compiler ( javac ) and launcher ( java ) are case-sensitive, so you must capitalize consistently.
HelloWorldApp is not the same as helloworldapp .
Save the code in a file with the name HelloWorldApp.java . In the Pico editor, you do this by typing Ctrl-O and then, at the bottom where you see the prompt File Name to write: , entering the directory in which you wish to create the file, followed by HelloWorldApp.java . For example, if you wish to save HelloWorldApp.java in the directory /tmp/examples/java , then you type /tmp/examples/java/HelloWorldApp.java and press Return.
You can type Ctrl-X to exit Pico.
Compile the Source File into a .class File
Bring up another shell window. To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is /tmp/examples/java , type the following command at the prompt and press Return:
If you enter pwd at the prompt, you should see the current directory, which in this example has been changed to /tmp/examples/java .
If you enter ls at the prompt, you should see your file.
Results of the ls command, showing the .java source file.
Now are ready to compile the source file. At the prompt, type the following command and press Return.
The compiler has generated a bytecode file, HelloWorldApp.class . At the prompt, type ls to see the new file that was generated: the following figure .
Results of the ls command, showing the generated .class file.
Now that you have a .class file, you can run your program.
If you encounter problems with the instructions in this step, consult the Common Problems (and Their Solutions).
Run the Program
In the same directory, enter at the prompt:
The next figure shows what you should now see.
The output prints «Hello World!» to the screen.
Congratulations! Your program works!
If you encounter problems with the instructions in this step, consult the Common Problems (and Their Solutions).
Previous page: «Hello World!» for Microsoft Windows
Next page: A Closer Look at the «Hello World!» Application - yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 java-1.8.0-openjdk-javadoc