Hot questions for Using Applets in appletviewer

Top Java Programmings / Applets / appletviewer

Question:

I want to run an applet in appletviewer. My project directory has the following structure:

/home/sanctus/workspace/AppletDocumentLoader/bin/com/examples/ti

AppletDocumentLoader is my project, then in /bin/com/examples/ti are 3 class files and the HTML file ie. all in the same directory.

My src class

package com.examples.ti;
import java.applet.AppletContext;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class Applet extends JApplet {

    private static final long serialVersionUID = -8756947240188460854L;


    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    JLabel label = new JLabel("Hello World");
                    add(label);
                    JButton openDocument = new JButton();
                    openDocument.setText("Button");
                    openDocument.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            System.out.println("BUTTON CLICKED");
                            AppletContext appletContext = getAppletContext();
                            try {
                                appletContext
                                        .showDocument(
                                                new URL(
                                                        "http://www.google.com"),
                                                "_self");
                            } catch (MalformedURLException e1) {
                                e1.printStackTrace();
                            }

                        }
                    });
                    add(openDocument);
                }
            });
        } catch (InvocationTargetException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

My HTML file

<!DOCTYPE html>
<html>
<head>
  <title>Hi there</title>
</head>
<body>
  This is a page
  a simple page
</body>
<applet code="Applet.class" 
        archive="Applet.jar"
        width=350 height=350>
</applet>
</html>

And the error I get:

sanctus@sanctus-desktop:~$ appletviewer '/home/sanctus/workspace/AppletDocumentLoader/bin/com/examples/ti/index.html' 
java.lang.NoClassDefFoundError: Applet (wrong name: com/examples/ti/Applet)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:217)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:152)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:626)
    at sun.applet.AppletPanel.createApplet(AppletPanel.java:799)
    at sun.applet.AppletPanel.runLoader(AppletPanel.java:728)
    at sun.applet.AppletPanel.run(AppletPanel.java:378)
    at java.lang.Thread.run(Thread.java:745)

Answer:

The code parameter of the applet tag should be the full name of the Java class, hence com.examples.ti.Applet. See Applet code tags and class files

Question:

In the last week I worked with Java Web Start. I can successfully start applications and applets with JWS. But for one project I have to determine how to start multiple applets via JWS, which should not open in browser but in appletviewer.

My searches always came to one result: Appletviewer can only be started over command line. Is that right? Is there no possibility to give an instruction in my HTML page or something like that, which would bring the applet to be started with the appletviewer?

Edit:

Ok, for all those who could search for this in future: Appletviewer is only for development purposes and the only way to start it is the command line command:

appletviewer myAppletSite.html

But if you really want to start it automatically, you could write an java application to run this system command:

Runtime.getRuntime().exec("appletviewer myAppletSite.html");

Answer:

My searches always came to one result: Appletviewer can only be startet over commandline. Is that right?

No! In fact, if you launch an applet non embedded using Java Web Start, the JWS client will use the applet viewer to display it.

Question:

I am new to applets and came across something unusual. In one of the programs I went through, applet html tag is mentioned as a comment in the Applet Java file (.java) but the comment seems to be getting executed. How is this possible?

Code:

    import java.awt.*;

    import java.applet.*;

/*<applet code="MyApplet" width=100 height=50></applet> */ 

   //why we give comment here and how it is executed ?? 

     class MyApplet extends  Applet 
    {
    public void paint(Graphics g)
    {
    g.drawString("A Simple Applet",100,100);

     }
  }

Comment: /*<applet code="MyApplet" width=100 height=50></applet> */ 

How is the above comment getting executed? Aren't comments meant to be skipped?

Why is this method not mentioned on Oracle or any other websites? Even Oracle asks to create seperate HTML file. So does this method use some 3rd party library to execute the comment?

I am also getting an error when I don't make the class public and when I keep the file and class name different.

Can someone please explain this method? I googled a lot but an explanation for this method of including the tags in a comment is nowhere to be found. There is a post on SO but the answer is not up to the point. Please Help.

How could someone accidentally figure out that this is possible? Is this any particular feature of the Applet Class?


Answer:

But how does it override the usual mechanism of skipping the comment lines?

You're thinking the compiler, not the applet viewer. Different tools that work in different ways.

Does it parse comments too?

AFAIU the applet viewer only parses the Java source code (note, source code, not the binary) up to the class definition. By design it ignores any code that is not in a comment and inspects only the commented code for possible applet tags. This new functionality was introduced around Java 1.7 or 1.6 AFAIR.

I didn't get this point -'By design it ignores any code that is not in a comment'. Does this mean it only looks for comments and then further selects the comments with applet tag?

Sorry, now you repeat it back to me, it is unclear. "Does this mean it only looks for comments and then further selects the comments with applet tag?" Yep. And thanks, that's a much better way to express what I meant.

I'll close by adding some comments by @zakki that I think are too informative to be hidden in a comments section:

@zakki: I think appletviewer MyApplet.java parses MyApplet.java as HTML, and it ignores unsupported elements like import java.awt.*;. AppletViewer Tags

@zakki: It seems that appletviewer just process input as token stream ant it doesn't care java/html syntax. gist.github.com/zakki/b5176a7d37fa3938f0646d11d9bd01a5

Question:

Is there any alternative to applet viewer in JDK12?

I am new to Java and I tried to add AWT checkbox in Java It runs fine in JDK 1.8, but not running in JDK12.

I searched over internet and found that the applet viewer is Deprecated. (The Applet API is deprecated, no replacement) on the official oracle's help web page.

I search about this problem and got no satisfying answers so I posted here...

import java.applet
import java.awt.*;

 /*
<applet code="CreateCheckBox" width=200 height=200>
</applet>
*/
public class CreateCheckBox extends Applet{

public void init(){
    Checkbox checkBox1 = new Checkbox();
    checkBox1.setLabel("My Checkbox 1");
    Checkbox checkbox2 = new Checkbox("My Checkbox 2");
    add(checkBox1);
    add(checkbox2);
}
}

It should work in JDK12 because it runs in JDK1.8.


Answer:

There is officially no way to run applets on Java 11 or later:

  • The appletviewer application has been removed.
  • The java.applet.* classes that applets depend on have been removed.

    Correction : They haven't done this yet, but I expect they will soon.

Applets should be treated as dead technology.

You could conceivably implement your own version of the appletviewer classes and put them together with your applet code into a JAR file. It should work. However, it is a bad idea. The primary use-case for applets was to embed Java in websites, and that won't work because modern browsers no longer support Java plugins. (Indeed, many recent browsers actively block Java plugins!)

The best thing to do is to rewrite applets using different technology, depending on what they do:

  • AWT / Swing
  • JavaFX
  • SWT
  • HTML 5
  • etcetera

You said:

It should work in JDK12 because it runs in JDK1.8.

This is not logically correct.

Question:

I am working on a homework assignment that is requiring me to create a basic applet that runs in a browser. I already know that the applets have been deprecated, so I know that this probably will not work with current browsers.

This is my Java code:

import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JLabel;


public class SimpleJavaApplet extends JApplet{

    public void paint(Graphics g) {
        g.drawString("Hello World!", 80, 80);
        g.drawString("Blah!", 100, 100);
    }

}

This is my HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>A Short Test</title>
</head>
<body>
    <title>Test Applet</title>
    <applet code="SimpleJavaApplet.class" width = "200" height = "50">
    </applet>
    <h1>Test Test Test</h1>
</body>
</html>

What I was hoping would happen is that a browser would launch and it would show up with the HTML, but the only think I have been able to get to run is the AppletViewer. I am wondering if this is even possible anymore and if I should just let my professor know that.

Any ideas will be greatly appreciated.

Edit: Just to make things clearer, I am trying to run this code so it runs through a browser and not just the AppletViewer.


Answer:

Neither Chrome nor Firefox are no longer supported NPAPI.

Java and Firefox Browser

Firefox no longer provides NPAPI support (technology required for Java applets) As of September, 2018, Firefox no longer offers a version which supports NPAPI, the technology required to run Java applets. The Java Plugin for web browsers relies on the cross-platform plugin architecture NPAPI, which had been supported by all major web browsers for over a decade. The 64 bit version of Firefox has never supported NPAPI, and Firefox version 52ESR is the last release to support the technology. It is below the security baseline, and no longer supported.

Java and Google Chrome Browser

Chrome no longer supports NPAPI (technology required for Java applets) The Java Plugin for web browsers relies on the cross-platform plugin architecture NPAPI, which had been supported by all major web browsers for over a decade. Google's Chrome version 45 and above have dropped support for NPAPI, and therefore Java Plugin do not work on these browsers anymore.

Finally Java Applets and Java Web start were completely removed from Oracle Java 11 SE.

Java 11 SE Release Notes

The deployment stack, required for Applets and Web Start Applications, was deprecated in JDK 9 and has been removed in JDK 11.

and

The appletviewer tool was deprecated in JDK 9 (see JDK-8074165) and removed in this release.

So if you're using Java 9 or below the appletviewer is available or if you able to download and install an ancient Chrome (less than 45) you can run that applet but Java 11 or above there is no alternative solution just the migration.

Question:

public class SetupExam extends Applet {
    /**
     * 
     */
    private static final long serialVersionUID = -4106575420788590877L;
    private JTextField td_tf;
    private JTextField mfeq_tf;
    private JTextField nmfe_tf;

    /**
     * Create the applet.
     */
    public SetupExam() {
        setLayout(null);
        JLabel main_l = new JLabel("Exam");
        main_l.setHorizontalAlignment(SwingConstants.CENTER);
        main_l.setFont(new Font("Arial", Font.BOLD, 15));
        main_l.setBounds(7, 4, 601, 42);
        add(main_l);

        JLabel cat_l = new JLabel("1.Category");
        cat_l.setFont(new Font("Arial", Font.PLAIN, 15));
        cat_l.setBounds(16, 80, 162, 32);
        add(cat_l);

        JLabel td_l = new JLabel("2.Time Duration");
        td_l.setFont(new Font("Arial", Font.PLAIN, 15));
        td_l.setBounds(16, 130, 162, 32);
        add(td_l);

        JLabel noq_l = new JLabel("3.Number of Questions");
        noq_l.setFont(new Font("Arial", Font.PLAIN, 15));
        noq_l.setBounds(13, 184, 165, 32);
        add(noq_l);

        JLabel mfeq_l = new JLabel("4.Marks for each Question");
        mfeq_l.setFont(new Font("Arial", Font.PLAIN, 15));
        mfeq_l.setBounds(13, 236, 178, 32);
        add(mfeq_l);

        JLabel nmfe_l = new JLabel("5.Negtive Mark For Each");
        nmfe_l.setFont(new Font("Arial", Font.PLAIN, 15));
        nmfe_l.setBounds(13, 293, 178, 32);
        add(nmfe_l);

        JComboBox cat_combo = new JComboBox();
        cat_combo.setEditable(true);
        cat_combo.setBounds(237, 80, 208, 32);
        add(cat_combo);

        td_tf = new JTextField();
        td_tf.setBounds(237, 130, 208, 32);
        add(td_tf);
        td_tf.setColumns(10);

        mfeq_tf = new JTextField();
        mfeq_tf.setColumns(10);
        mfeq_tf.setBounds(237, 236, 208, 32);
        add(mfeq_tf);

        nmfe_tf = new JTextField();
        nmfe_tf.setColumns(10);
        nmfe_tf.setBounds(237, 293, 208, 32);
        add(nmfe_tf);


        JSpinner noq_spin = new JSpinner();
        noq_spin.setModel(new SpinnerNumberModel(10, 1, 100, 1));
        noq_spin.setBounds(237, 180, 208, 32);
        add(noq_spin);



        JButton sbmt_btn = new JButton(mfun());
        sbmt_btn.setFont(new Font("Arial", Font.PLAIN, 12));
        sbmt_btn.setBounds(228, 383, 126, 41);
        add(sbmt_btn);

    }
    public String mfun()
    {
        try
        {
         Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        Connection con=DriverManager.getConnection("jdbc:ucanaccess://C:\\workspace\\exam.mdb");
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("Select * from  exam_setup");
        while (rs.next())
            {
                //System.out.println(rs.getString(3));
                return rs.getString(3);

            }
         } catch (Exception ex)
                   {
                System.err.print("Exception: ");
                System.err.println(ex.getMessage());
                   }
        return null;


    }

}

I am using Ucanacess 4.0.1.

Before this I checked connectivity by inserting data in table exam through a Java program but it's not working when I added the code in my applet. I used following command to run applet

appletviewer ExamSystem.html

output:

Exception: UCAExc:::4.0.1 access denied ("java.util.logging.LoggingPermission" "control")


Answer:

The appletviewer utility apparently runs the applet code in a restricted environment, most likely similar to the "sandbox" that would be used for an unsigned applet. UCanAccess was not designed to run in such an environment.

Question:

I'm building a calculator applet that isn't displaying about half the code within the AppletViewer.

I'm using BlueJ (as required by my teacher) and the code compiles just fine, albeit, with a noticeable lag as compared to when I first started writing the program. When attempting to run in AppletViewer, only about the first half of code runs, but I never receive an error message. The status just stays at "Executing Applet in AppletViewer".

Within the code, I have methods that call other methods within more methods. I have multiple loops and arrays occurring for displaying the content and more variables then I can count.

I have tested individual methods in separate project files as I wrote them, before adding them to the actual project, so I'm fairly certain each component works in and of itself. It's only when I put them all together that things start displaying incorrectly.

Because of this, I'm wondering if I've simply put in too much content, bogged down the system. If so, is there a way increase the threshold, and how would I implement it?

Thank you, in advance.


Answer:

Solved:

The culprit lay in the last array set I was working with. A simple "<=" was causing an "array index out of range" error, despite no error message being displayed.