简体   繁体   中英

Applet CLASS_NOT_FOUND_EXCEPTION

let's, create applet and add it to jsp page, environment in use is STS Spring Tool Suite

create New -> Dynamic Web Project -> name="WEBtest"

Java Resources -> src-> new packeg="firstPack.secondPack.mainPack.appletPack" -> new class="MyApplet"

package firstPack.secondPack.mainPack.appletPack;
import java.awt.Color;

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

public class MyApplet extends JApplet {
    @Override
    public void init() {
        super.init();
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    populateGUI();
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
    void populateGUI(){
        JPanel panel=new JPanel();
        panel.setBackground( Color.LIGHT_GRAY);
        panel.add(new JLabel("QUAQ-QUAQ"));
        add(panel);
        this.revalidate();
    }
}

Run As -> Java Applet

Ok it works

web.xml was created automatically

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>WEBtest</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

so...

Web Content -> new jsp="default"

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    <h6>DUCK</h6>

    <h6>First TRY</h6>

    <applet code="firstPack.secondPack.mainPack.appletPack.MyApplet"></applet>

    <h6>Second TRY</h6>

    <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93">
        <param name="code"
            value="firstPack.secondPack.mainPack.appletPack.MyApplet">
    </object>

    <h6>Third TRY</h6>

    <embed code="firstPack.secondPack.mainPack.appletPack.MyApplet"
        type="application/x-java-applet;version=1.6" />

    <h6>Fourth TRY</h6>

    <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93">
        <param name="code"
            value="firstPack.secondPack.mainPack.appletPack.MyApplet">
        <comment> <embed
            code="firstPack.secondPack.mainPack.appletPack.MyApplet"
            type="application/x-java-applet;version=1.6">
            <noembed> No Java Support. </noembed>
        </embed> </comment>
    </object>


</body>
</html>

三

now drag and drop our project in "Server"

server is default to STS: "Privotal tc Server Developer Edition v3.1"

start the server

open browser at http://localhost:8080/WEBtest/

click on window

第一

click on Details

第二

CLASS_NOT_FOUND_EXCEPTION

here some project setting

在此处输入图片说明

since everything from "src" go to "WEB-INF/classes" then i assume that "code" parameter in applet is OK

nevertheless i also tried to extract java file into JAR, and add "archive" parameter, but yet CLASS_NOT_FOUND_EXCEPTION

i deleted all java version on PC, then installed Java SE Development Kit 8 Update 40 (64 bit),it also installed Java 8 Update 40 (64 bit)

after, i tried to open some example of applet from docs.oracle.com yet failed, thus it asked me to install Java 8 Update 40 without 64bit, so now i have both, but since applet examples works fine i doubting this is the problem and left it as it is

at the end of second day of googling i'm kinda desperate, yet it's important to make it work

since everything from "src" go to "WEB-INF/classes" then i assume that "code" parameter in applet is OK

Only the server has access to the WEB-INF/classes and WEB-INF/lib paths. If you try to make a direct fetch on the applet (using the browser address bar) you'll likely see a 'forbidden' page.

applet should be public content, by typing its URL in browser we should be able to download it

to achieve this should be enough to modify "Deployment Assembly" so ..

First Try

Project -> Property - > Deployment Assembly - > Source ( java resources folder "/src") -> Deploy Path ( change to root directory )

ok now it works

Now restore "src" folder to "WEB-INF/classes" (for server side purpose)

Create new source folder "appletSRC", change it Deployment Assembly to "root directory" and for some reason it doesn't work

If in project property change "Order and Export" part by pushing

"appletSRC" -> UP

it's possible to make applet work, but then "src" (which first was on the top) will not be deployed in "WEB-INF/classes".

Nevertheless Ultimate solution

Project -> Property - > Java Build Path -> Source -> appletSRC -> Output folder -> Edit

set to public folder in "webapp"

when declaring applet tag specify in the "archive" our public folder

Important finish "archive" with /slash, and code with .class extension otherwise if refreshing browser several times it will give error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM