Home > Java > javaTutorial > body text

Teach you how to configure the Applet environment

Y2J
Release: 2017-05-18 10:07:08
Original
2848 people have browsed it

Java applets, also known as Java Applets, can run in a web browser. Java Applets must be embedded in HTML pages in the form of scripts to run in web browsers.

I always thought that if I installed the JDK locally and specified the JAVA_HOME and PATH environment variables, all Java programs would be able to run. Later, a colleague asked me to When I helped him solve the problem that a Gantt chart of a project management software could not be run in the web browser, I discovered that the running environment configurations of Java Applet and general Java applications were different. To run Java applications on Windows, as long as you have the JDK binary directory and specify JAVA_HOME and PATH, you can use it directly. You do not need to install it through the JDK installation program. Therefore, you can back up the installed JDK directory as a compressed package. When you reinstall the system or install the Java environment on other machines in the future, you can directly copy the JDK binary compressed package and use it directly after decompression.

But in Windows, when configuring the running environment of Java Applet, it is not enough to only specify the JDK directory. You must meet the following requirements:

1. Use the JRE installation package. Installation

2. The JRE version provides Java Plugin

3 The bit length of .JRE and the web browser must be consistent, such as both 32-bit or 64-bit software versionsThis is because the JRE will install Java-related Information is written to the registry and a Java Plugin is installed for the web browser. When the Java Plugin of the web browser runs an Applet, it will first read the Java information from the Windows registry and then execute the Applet program.

It should be noted here that different versions of JRE support different web browsers and versions. My local web browsers include IE8, Firefox16, and Chrome19. After installing JRE6u11, only IE8 can run Applets; under Firefox16, you can see that the Java Plugin is installed through about:plugins, but cannot run Applets; while under Chrome19, it shows that there is no Java Plugin installed. Java Plugin. After installing JRE7u9, all browsers can run Applets. So, the simplest thing is to just install the latest JRE version. Usually if the browser does not have Java Plugin installed, the user will be prompted to install the plug-in when running a web page with an Applet. Generally, it can be installed directly.

Java Tester is a website that is used to check the locally installed JRE version and software manufacturer. It can also check whether the web browser can run the Applet program:

Java Tester - What Version of Java Are You Using?

For ordinary users, it is very simple to install the Applet running environment, but sometimes it is the browser of Java developers that cannot run Applet, which is a bit shabby. Java developers often install multiple Java versions on their computers. Sometimes they directly move or

delete

the JDK directory without uninstalling it through the Windows control panel, resulting in residual Java installation information in the Windows registry. , there is no problem running the Java application, but the Java Plugin cannot find the JRE in the web browser to execute the Applet. After installing JRE, a Java icon will appear on the Java control panel. If you find that the Java icon cannot be displayed, it will prompt that the program cannot be found when you click it, as shown below:

Teach you how to configure the Applet environmentThe Java icon that cannot be displayed above means that the JRE environment on Windows cannot be found and the JRE needs to be reinstalled. The normally available JRE environment is as shown below:

Teach you how to configure the Applet environmentClick the Java icon to open the Java Control Panel (javacpl), indicating that the JRE on Windows is already available.

To sum up, Java applications and Java Applets have different requirements for the running environment. When a Java application is running, there is no need to find the registry, as long as the JDK directory is specified, it can be run. For Applet applets, the Java Plugin in the web browser needs to find the JRE environment through the registry and run the Java Applet.

Therefore, it is recommended that the local Java environment:

Install a higher version of JRE to support newer web browsers running Applets Program The JDK required for Java applications,

directly copy the JDK binary directory and use , no installation is required, multiple JDKs can coexist, and the JDK version must be used when executing.

Appendix 1

: If you cannot reinstall JRE, you can follow the steps in the following article to clear the remaining Java installation information in the registry and then run the JRE installer:

Appendix 2

: How to enable Java in a web browser? (It must meet the prerequisite that JRE is installed and available, and the web browser has Java Plugin installed)

www.java.com/zh_CN/download/help/enable_browser.xml

Appendix 3: Hello Applet

1. Write an Applet applet, Inherit Applet base class:

Java code

public class HelloApplet extends Applet {  
  
    private static final long serialVersionUID = 5511892956119084309L;  
  
    @Override  
    public void init() {  
        Graphics g = this.getGraphics();  
        paint(g);  
    }  
  
    public void paint(Graphics g) {  
        g.drawString("Hello Applet!", 45, 45);  
    }  
}
Copy after login

2. Compile Applet

Java code

CMD>javac HelloApplet.java
Copy after login

3. Embed the Applet applet in the index.html web page

Html code

...  
<applet alt="" code="cn.david.applet.HelloApplet.class" archive="applet-1.0.0-SNAPSHOT.jar"  width="200" height="200"   
codebase=".">  
</applet>  
...
Copy after login

Note:

*Applet must be embedded into a web page to run. Use the tag to embed Applet

*codeAttributeSpecify Applet Class

*The archive attribute specifies the jar package where the applet is located. If it is not packaged, it can be omitted

*The codebase attribute specifies the root directory used to find the Applet class and Jar package. This directory is relative to For the directory where the web page is located, specify a relative directory. codebase="." means to search for the Applet applet in the web page directory.

*The tag should be used in the web page. When using , it can run normally in the web page, but the applet window cannot be displayed in the appletviewer.

4. Use appletviewer to test Applet applet

Java code

CMD>appletviewer index.html
Copy after login

Teach you how to configure the Applet environment

5. Run Applet in web browser Applet

Drag the web page to the browser to view it, or deploy the web page and Applet applet to the HTML document directory of Apache to access it through the URL.

【Related Recommendations】

1. Special Recommendation: "php Programmer Toolbox" V0.1 version download

2. Java free video tutorial

3.Detailed explanation of the difference between Application and Applet

4. Introduce in detail what is Java applet

5. Teach you how to run applet in browser

The above is the detailed content of Teach you how to configure the Applet environment. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template