appletviewer Command in Linux



Java Development Kit, better known as JDK, is the most frequently used development environment for creating applications that use the Java programming language. The older JDK versions contain a command line utility named appletviewer that is used for testing and debugging Java applets.

In the latest JDK versions, however, this tool is not available because of several reasons, such as security concerns, development complexity, incompatibility with modern web standards, etc.

If you still want to use this command for some specific purpose, you must install an older version of Java JDK (i.e., JDK 8 or earlier).

Table of Contents

Here is a comprehensive guide to the options available with the appletviewer command −

appletviewer Command in Linux

The appletviewer command opens URLs and shows any applets found on those pages in separate windows. It is important to note that if the pages from the URLs do not contain any applets using the <object>, <embed>, or <applet> tags, then the appletviewer command does nothing.

As discussed earlier, the appletviewer command is deprecated for security reasons and is unavailable in the JDK 9 and later versions. To use this command on your Linux operating system, you must have an older version of the JDK installed on your system. To do this, go through the following steps −

Step 1: Install Java JDK

You can install an older version of JDK on your system by executing the below-mentioned command from the Linux terminal −

sudo apt install openjdk-8-jdk openjdk-8-jre -y
Install Java JDK

Step 2: Verify Java Installation

Execute the below command to confirm that JDK 8 or an earlier version is installed on your system −

java -version

The following output snippet shows that we are using openjdk 1.8.0_412 on our Linux system −

Verify Java Installation

Step 3: Syntax to Use appletviewer Command on Linux

After the successful installation of Java JDK 8 or an earlier version, you can use the appletviewer command by following the given syntax −

appletviewer <filename>

Replace <filename> with a valid file name that contains the applet you want to execute using the appletviewer command.

Step 4: Using appletviewer Command in Linux

Lets consider an example of how the appletviewer command works in Linux. For this purpose, first create a file named exampleClass.java using the following command −

nano exampleClass.java

Now paste the below-given code into it −

import java.awt.*;
import java.applet.*;

public class exampleClass extends Applet{

   public void init(){   
      setForeground(Color.white);
      setBackground(Color.gray);
   }

   public void appletPaint(Graphics graph){
      graph.drawString("Hi Geeks! Let's Learn appletviewer Command", 30,30);
   }
}

In this code −

  • First, we import all the classes of the java.awt and java.applet packages.
  • Next, we create an exampleClass that extends the Applet class.
  • Within this class, we invoke the init() method that sets the foreground and background color to white and gray, respectively.
  • After this, we create a public method appletPaint() that displays the text "Hi Geeks! Let's Learn appletviewer Command" at the coordinates (30, 30).
appletviewer Command in Linux

Step 5: Compile Java Applet Code From the Linux Terminal

Now press CTRL+X to save this code in a file named exampleClass.java and compile it by executing the below-mentioned command −

javac exampleClass.java
Compile Java Applet Code From the Linux Terminal

The cursor moves to the subsequent line, indicating that the exampleClass has been compiled successfully. On successful execution, this command will generate a new file named exampleClass.class.

Step 6: Create an HTML File

Now create a file named exampleClass.html and paste the following code into it −

<html>
<body>
   <applet code="exampleClass.class" width="350" height="200"></applet>
</body>
</html>

In this HTML code, the applet tag specifies the compiled Java class file (exampleClass.class) to load and execute the applet −

Create an HTML File

Press CTRL+X and then y to save the changes to the file; after this hit the Enter key to navigate back to the terminal.

Step 7: Run the appletviewer Command

In the terminal, write the appletviewer command followed by the HTML file name to execute the applet code and get the desired results −

appletviewer exampleClass.html

When we execute this command, a new window pops up that shows the following results −

Output of appletviewer command

This is how the appletviewer command works in Linux.

Key Aspects of appletviewer Command in Linux

The following table highlights different aspects of the appletviewer command in Linux. These points will help you understand when to use or not use the appletviewer command in Linux −

AspectDescription
AvailabilityThe appletviewer command has been deprecated and is no longer available in JDK 9 and later versions.
FunctionalityIt lets us execute and debug Java applets without requiring a web browser.
Syntaxappletviewer <fileName>, where fileName represents an HTML file that contains an applet to be executed.
UsageMainly used for testing and developing Java applets. It scans an HTML file and uses the <applet> tag to run the specified applet.
DebuggingIt runs applets in a controlled environment to easily debug and test without browser-specific issues.
Security IssuesAppletViewer does not address the known security issues of applets.
Troubleshooting Tip for appletviewer ErrorsMake sure you are using the correct JDK version (i.e., JDK 8 or earlier) to run the applet without any errors.

This sums up the appletviewer command in Linux.

Conclusion

appletviewer is a command-line utility that was used for testing and debugging applets in JDK 8 and earlier versions. However, Oracle deprecated this command due to security and browser-compatibility issues. Do you still want to use it for some specific purposes? Well! You can do that by installing an older JDK version (i.e., JDK 8 or an earlier version). In this write-up, we explained how you can install an older JDK version, verify its installation, and then use the appletviewer command to execute a Java applet.

For better understanding of the appletviewer command, we create a Java file in the nano editor that extends the Applet class. Within this class we specify some code to display a message on the applet. After this, we compile the code using the javac command, and specify the compiled code in an HTML file using the <applet> tag. Finally, we execute the appletviewer command to get the desired results.