Easy way to Run a Java Applet in Browser

In How To 185 views

Java Applet

You are going to learn how to create and integrate Java based applet in Web application. First of all let’s know what an “Applet” really is. An Applet is a Java based small program, which can be included in HTML page. There is no particular and specific template for creating Applet. But you’re lucky to have an option to create applet in java project and then packaging it in web project. Anyways, let’s move to the process.

Things Required:

You must have following in order to create an Applet.

  • NetBeans IDE version 7.2, 7.3, 7.4, 8.0 etc.
  • Java Development kit version 7 or 8.
  • GlassFish server open source editor.

Process of Creating Java Applet Source File

Creating Java Project

  • Choose new project> then select Java in categories.
  • Now select java class library (in case you are creating new Applet source file). Select project with existing source and then locate source file in source package folder (in case you are importing applet source file).
  • You can specify the name of project with any name. Let’s say you name is In case you’ve imported an Applet file then run it.

Creating Applet Source File

  • Now open the properties window by right clicking the HelloApplet project file. Then Select the desired format and tap/click Ok.
  • Now right click the HelloApplet project node and select new and other. Select Java and Applet under categories and File type (respectively).
  • Write MyApplet under Class Name and type me.hello under Package. Tap/click Finish button.
  • Now copy>paste this following code over default code of, in order to define Applet class.

package org.me.hello;

import java.applet.Applet;

import java.awt.Graphics;

public class MyApplet extends Applet

@Override

public void paint(Graphics g) {

g.drawString(“Hello applet!”, 50, 25);

}}

Running Applet Source File

Now right click on MyApplet.java file node in project window. Then choose Run File from the contextual menu. MyApplet.html launcher file is created in build folder.          

Embedding Applet in Web Application

You’ve done your work. Now it’s time to make this Applet available to users. You just need to create a web application and add the Applet JAR file to classpath, and finally add Applet tag to the HTML file of that application.

How to create a web project?

  • First of all choose File> then New Project> select Web Application in Java Web category> click/tap next.
  • Type HelloWebApplet under file name.
  • Change location to any other folder in your system. Then select the Target Server and click/tap finish.

Adding Applet JAR file to web application

You can do so by adding the project that contains Applet’s JAR file to web application or by adding the JAR file itself. Beware that adding java project to web project will enable IDE to build applet whenever you build web application.  New version of Applet will be built upon each modification in web application. But if JAR file of Applet is not in NetBeans IDE project then it won’t rebuild when you build a web project. Anyways. It’s up to your choice. Let’s see how to add Applet JAR file into web application.

  • Right click HelloWebApplet node in project window and go to contextual menu> select Properties.
  • Now select Packaging Category.
  • If Applet is in java project then click/tap add project and locate folder containing that project. If you’re using an applet JAR file which is not in project then click/tap add file or folder and locate JAR file’s location. Then click/tap Ok button. And then close the properties window.
  • JAR file is copied to web page library of web application by default. It is the build/web folder which is the root directory of the application.

Running the JSP or HTML file

  • Double click the default jsp file in project window for embedding Applet in JSP file. index.jsp file is created by IDE on creating a web project.
  • On the other hand, if you want to embed the Applet in HTML file then click/tap HelloWebApplet project node and choose new, select web under categories and click/tap next. Select HTML under file type. Specify a name of your file and select a web folder for its location and click finish.
  • Now it’s time to embed Applet in file. Add following Applet tags, Anywhere between file’s body tags.
  • In an HTML file: <applet code=”org.me.hello.MyApplet” archive=”HelloApplet.jar”></applet>
  • In a JSP file: <applet code=”org.me.hello.MyApplet” archive=”HelloApplet.jar” width=”600″ height=”480″/>
  • Now right click n JSP or HTML node in project window then choose Run file (in contextual menu). The server will deploy the JSP or HTML file in IDE’s default browser. And you’re done!

 

Related Search

Tags: #Applet #Applet Source File #browser #GlassFish #html #Java #Java Applet #NetBeans #programming

author
Author: 
    Using Tor Browser on Android in 3 Steps
    Using Tor Browser on Android in 3 Steps
    How to Use Tor on Android: When
    Update Your Internet Browser
    Update Your Internet Browser
    Explore how to update browser to the
    Identify Browser's Hijacking & Fix
    Identify Browser's Hijacking & Fix
    Explore and know if your web browser
    Must read×

    Top