★ wanayoo — archive 1999 http://blogs.oracle.com/thejavatutorials/tags/deploymentNouvelle recherche | Portail wanayoo

Thursday Feb 18, 2010

Getting Java Applets to Work on Mac OS

Are you having trouble viewing some Java applets on a Mac? Got next generation Java Plug-in? The next generation Java Plug-in (introduced in Java SE 6 update 10), needs to be enabled in order to view applets that leverage features of this Java Plug-in. The next generation Java Plug-in is not enabled by default on Mac OS systems. So here are a few tips to enable the plug-in.
  • Upgrade to a 6u17 based Java update. This support page from Apple might be helpful.
  • Open the Java Preferences application and toggle setting to "Run applets: in their own process".
  • Restart your browser and try viewing the applet.
When using Firefox 3.5 and above, in some cases the Java Preferences setting does not fully disable the default MRJPlugin and fully enable the next generation Java Plug-in. If after the performing the above steps you still have problems viewing some applets, you will need to go to the guts of the problem. :-)

Perform the following steps to disable the default MRJPlugin:

  • Go to the /Applications/Firefox.app/Contents/MacOS/plugins directory.
  • Move the following files to a temp directory or another location:
    • JavaEmbeddedPlugin.bundle
    • MRJPlugin.plugin
  • Restart your browser.
  • To check which plug-in is being used by your browser, view the about:plugins URL. An entry titled "JavaPlugin2_NPAPI.plugin" indicates that the next generation Java Plug-in is enabled in your browser.

You can now enjoy Java applets on MacOS!

- Sowmya

Wednesday Nov 25, 2009

Java Applets Quiz

Do you enjoy quizzes? Take a minute to answer this quiz about Java applets. Java Applets Quiz

Don't worry you wont be graded. ;-)

-- Sowmya Kannan

Tuesday Nov 24, 2009

Getting Applets To Talk To Each Other

Do you have more than one applet on a web page? Do you want the applets to share water cooler gossip? Ahem..ahem.. On a more serious note, do you want one applet to invoke methods or set variables of another applet?

Long long ago, you may have accomplished applet communication with the use of static variables. This method of sharing applet data is not fool proof. Different applets may run in different instances of the Java Virtual Machine software. This makes it impossible for an applet to see static variables set by another applet.

An applet can communicate with other applets by using JavaScript functions in the parent web page. JavaScript functions enable communication between applets by receiving messages from one applet and invoking methods of other applets. See the following topics for more information about the interaction between Java code and JavaScript code:

You should avoid using the following mechanisms to find other applets and share data between applets:

  • Avoid using static variables to share data between applets.
  • Do not use the getApplet and getApplets methods of the AppletContext class to find other applets. These methods only find applets that are running in the same instance of the Java Runtime Environment software.

Applets must originate from the same directory on the server in order to communicate with each other.

Consider two applets called Sender and Receiver. When a user clicks the button to increment the counter, the Sender applet invokes a JavaScript function to send a request to the Receiver applet. Upon receiving the request, the Receiver applet increments a counter variable and displays the value of the variable.

To enable communication with another applet, obtain a reference to an instance of the netscape.javascript.JSObject class. Use this instance to invoke JavaScript functions. The Sender applet uses an instance of the netscape.javascript.JSObject class to invoke a JavaScript function called sendMsgToIncrementCounter.

try {
    JSObject window = JSObject.getWindow(this);
    window.eval("sendMsgToIncrementCounter()");
} catch (JSException jse) {
    ...
}

Note: To compile Java code that has a reference to classes in the netscape.javascript package, include <your JDK path>/jre/lib/plugin.jar in your classpath. At runtime, the Java Plug-in software automatically makes these classes available to applets.

Write the JavaScript function that will receive requests from one applet and invoke methods of another applet on the web page. The sendMsgToIncrementCounter JavaScript function invokes the Receiver applet's incrementCounter method.

<script>
    function sendMsgToIncrementCounter() {            
        receiver.incrementCounter();
    }
<script>

Note that the JavaScript code uses the name receiver to obtain a reference to the Receiver applet on the web page. This name should be the same as the value of the id attribute that is specified when you deploy the Receiver applet.

The Receiver applet's incrementCounter method is shown next.

public void incrementCounter() {
    ctr++;
    String text = " Current Value Of Counter: " + (new Integer(ctr)).toString();
    ctrLbl.setText(text);
}

Deploy the applets on the web page as shown in the following code snippet. You can view the Sender and Receiver applets and associated JavaScript code in AppletPage.html in this example's source code..

<!-- Sender Applet -->
<script src="/old?u=http%3A%2F%2Fwww.java.com%2Fjs%2FdeployJava.js&y=1999"></script>
<script> 
    var attributes = { code:'Sender.class',
        archive:'examples/dist/applet_SenderReceiver/applet_SenderReceiver.jar',  width:300, height:50} ;
    var parameters = {};
    deployJava.runApplet(attributes, parameters, '1.6');
</script>

<!-- Receiver Applet -->
<script> 
    var attributes = { id:'receiver', code:'Receiver.class',
        archive:'examples/dist/applet_SenderReceiver/applet_SenderReceiver.jar',  width:300, height:50} ;
    var parameters = {};
    deployJava.runApplet(attributes, parameters, '1.6');

</script>

Download source code for the Sender Receiver Applets example to experiment further.

As always, please don't hesitate to send us your feedback!

-- Sowmya Kannan

Tuesday Sep 22, 2009

Java Tutorial Update

We have just pushed an updated version of the Java Tutorial to the web. This update features:

Please let us know what you think!

- Sharon Zakhour

Thursday Apr 02, 2009

Deployment Toolkit 101

Contents

What is the Deployment Toolkit or DT as it is affectionately referred to? :-)

The Deployment Toolkit is a set of JavaScript functions that can help developers easily deploy applets and Java Web Start applications (aka rich internet applications, RIAs) consistently across various browser and operating system configurations. As a developer, you now do not have to worry about handling the idiosyncracies of different clients. Just use the functions in the Deployment Toolkit - it takes care of all the underlying issues for you and generates the correct HTML.

Note:

  • We will use the phrase "rich internet applications" or "RIAs" in places where some functionality applies to both applets and Java Web Start applications.
  • Depending on the type of browser, you may not be able to see the HTML generated by the Deployment Toolkit when you try to view source for the page. To view the generated HTML, try saving the HTML page after its been loaded, or, use a tool such as Firebug (Mozilla Firefox add-on)

When was the Deployment Toolkit introduced and where can I find it?

The Deployment Toolkit was introduced in Java SE 6u10. It can be found at http://java.com/js/deployJava.js or https://www.java.com/js/deployJava.js

The http://java.com/js/deployJava.txt file contains the un-minimized version with comment blocks describing functions and usage.

Now that the introductions are done, let's move on to the details.

Using the Deployment Toolkit

Deploying an applet using the runApplet function

Use the runApplet function to deploy your applet. The runApplet function ensures that the required minimum version of the Java Runtime Environment (JRE) exists on the client and then runs the applet. It generates an <applet> tag with the information provided.

Function signature: runApplet: function(attributes, parameters, minimumVersion)

Parameters:

  • attributes: The attribute names and values of the generated <applet> tag
  • parameters: The names and values of the <param> tags in the generated <applet> tag
  • minimumVersion: The minimum version of JRE required to run this applet

Usage:

  • Specifying deployment options as attribute and parameter name-value pairs

    The attributes and parameters passed as name-value pairs are written out as attributes and nested <param> tags in the generated <applet> tag.

    // launch the Java 2D applet on JRE version 1.6.0 or higher with one parameter (fontSize)
    <script src="/old?u=http%3A%2F%2Fjava.com%2Fjs%2FdeployJava.js&y=1999"></script>
    <script>
        var attributes = {codebase:'http://java.sun.com/products/plugin/1.5.0/demos/jfc/Java2D',
                          code:'java2d.Java2DemoApplet.class',
                          archive:'Java2Demo.jar',
                          width:710, height:540} ;
        var parameters = {fontSize:16} ;
        var version = '1.6' ;
        deployJava.runApplet(attributes, parameters, version);
    </script>
    	
  • Using jnlp_href parameter to specify deployment options in a JNLP file

    The attributes and parameters (jnlp_href in this case) passed as name-value pairs are written out as attributes and nested <param> tags in the generated <applet> tag. It is better to specify the applet's width and height as attributes, as shown below.

    <script src="/old?u=http%3A%2F%2Fjava.com%2Fjs%2FdeployJava.js&y=1999"></script>
     <script> 
    		var attributes = { code:'java2d.Java2DemoApplet', width:710, height:540} ; 
    		var parameters = {jnlp_href: 'java2d.jnlp'} ; 
    		deployJava.runApplet(attributes, parameters, '1.6'); 
    </script>
    		
  • Specifying attribute and parameter name-value pairs as well as JNLP file

    Applets deployed using JNLP will run only if the end user has the next generation plug-in running on his / her browser (next generation plug-in introduced in Java SE 6u10). If you would like your applet to run on the old plug-in also, specify deployment options using attribute and parameter name-value pairs as well as JNLP file.

    <script src="/old?u=http%3A%2F%2Fjava.com%2Fjs%2FdeployJava.js&y=1999"></script>
    <script>	
    		var attributes = {codebase:'http://java.sun.com/products/plugin/1.5.0/demos/jfc/Java2D',
                            code:'java2d.Java2DemoApplet.class', archive:'Java2Demo.jar', 
                            width:710, height:540} ; 
    		var parameters = {fontSize:16, jnlp_href:'java2d.jnlp'} ; 
    		var version = '1.6' ; 
    		deployJava.runApplet(attributes, parameters, version);  	
    </script>
    
    If you are now wondering what happens if some deployment options have different values in the attributes name-value pairs and in the JNLP, read more about how final deployment information is determined.

Deploying a Java Web Start application using the createWebStartLaunchButton function

Use the createWebStartLaunchButton function to deploy a Java Web Start application. The createWebStartLaunchButton function generates a link (HTML anchor tag - <a> ) to the Java Web Start application's JNLP file.

This generated anchor tag is the Java Web Start application's button. When the end user clicks the Launch button, it ensures that an appropriate JRE is installed and then launches the Java Web Start application.

Function signature: createWebStartLaunchButton: function(jnlp, minimumVersion) or createWebStartLaunchButton: function(jnlp)

Parameters:

  • jnlp: The url of the JNLP file containing deployment information for the Java Web Start application. This needs to be an absolute path.
  • minimumVersion: The minimum version of JRE required to run this application

Usage:

  • Specifying a minimum JRE version that is required to run the application
    <script src="/old?u=http%3A%2F%2Fjava.com%2Fjs%2FdeployJava.js&y=1999"></script>
    <script>
        var url = "http://java.sun.com/javase/technologies/desktop/javawebstart/apps/notepad.jnlp";
        deployJava.createWebStartLaunchButton(url, '1.6.0');
    </script>
        

    Note: Running on any JRE version - Use the createWebStartLaunchButton: function(jnlp) function if your application does not have a minimum JRE requirement.

  • Working around the problem of specifying absolute URL for JNLP file

    The requirement of an absolute URL for the JNLP file of a Java Web Start application means that the web page is no longer easily portable between, for example, development, test and production environments. Obviously, the JNLP file path will be different in these environments and you should not have to change the url at each step. Applets do not have this problem because applets determine the codebase relative to the page in which they are embedded. You can simulate this behaviour with some JavaScript, such that the JNLP file's path is relative to the location of the web page from which the Java Web Start application will be launched. Here is some sample code:

    <script src="/old?u=http%3A%2F%2Fjava.com%2Fjs%2FdeployJava.js&y=1999"></script>		
    <script >
        var dir = location.href.substring(0,location.href.lastIndexOf('/')+1);
        var url = dir + "notepad.jnlp";
        deployJava.createWebStartLaunchButton(url, '1.6.0');
    </script>

Changing the Launch button of your Java Web Start application

You can change your Java Web Start application's Launch button, if you don't like the default button or you have another button that you have standardized on.

Use the deployJava.launchButtonPNG variable to point to the location of your image.

Variable: deployJava.launchButtonPNG

Usage:

  • Providing an alternate image url. In this example, the Launch button will now be an image of our own Duke waving
    		
    <script src=/old?u=http%3A%2F%2Fblogs.oracle.com%2Fthejavatutorials%2Ftags%2F%26quot%3Bhttp%3A%2Fjava.com%2Fjs%2FdeployJava.js%26quot%3B%26gt%3B%26lt%3B%2Fscript%26gt&y=1999
    <script>
        deployJava.launchButtonPNG='http://java.sun.com/products/jfc/tsc/articles/swing2d/WatermarkDemo/duke_wave.png'
        var url = "http://java.sun.com/javase/technologies/desktop/javawebstart/apps/notepad.jnlp";
        deployJava.createWebStartLaunchButton(url, '1.6.0');
    </script>
        

Detecting the client JRE version using the versionCheck function

There are many good reasons to be able to check if a particular version of the JRE is available on a client machine. For example, you may want to launch a different version of your RIA depending on the client's JRE version, or you may want to redirect the user to a different page etc.

Use the versionCheck function to check if a particular version or range of JRE versions is installed on the client.

Function signature: versionCheck: function(versionPattern)

Parameters:

  • versionPattern: String specifying the version or range of versions to check for, such as such as "1.4", "1.5.0\*" (1.5.x family), and "1.6.0_02+" (any version greater than or equal to 1.6.0_02).

Usage:

  • Creating a different user experience depending on the client's JRE version. In this example, we create a Launch button for the application only if the client's JRE version is greater than or equal to 1.6.0. If not, we redirect them to java.sun.com.
        
    <script src=/old?u=http%3A%2F%2Fblogs.oracle.com%2Fthejavatutorials%2Ftags%2F%26quot%3Bhttp%3A%2Fjava.com%2Fjs%2FdeployJava.js%26quot%3B%26gt%3B%26lt%3B%2Fscript%26gt&y=1999
    <script>
        if (deployJava.versionCheck('1.6.0+')) {            
            var url = "http://java.sun.com/javase/technologies/desktop/javawebstart/apps/notepad.jnlp";
            deployJava.createWebStartLaunchButton(url, '1.6.0');
        } else {
            document.location.href=/old?u=http%3A%2F%2Fblogs.oracle.com%2Fthejavatutorials%2Ftags%2F%26quot%3Bhttp%3A%2Fjava.sun.com%26quot%3B%3B&y=1999
        }
    </script>		
    		

Related Reading


As always, we welcome feedback. Tell us about other things you'd like information about!

-- Sowmya Kannan

Tuesday Mar 31, 2009

Deploying An Applet In Under 10 Minutes

JavaTM SE 6u10 allows us to deploy applets using Java Network Launch Protocol (JNLP). In fact, this is now a recommended best practice!

Tired of dealing with pesky browser issues when deploying applets? Think Deployment Toolkit.          
We now have the Deployment Toolkit, a JavaScript library with nifty functions to deploy applets and Java Web Start applications. It takes care of all underlying browser idiosyncracies :-)

Check it out at http://java.com/js/deployJava.js or https://www.java.com/js/deployJava.js

Let's walk through some steps to understand applet deployment. Check the "Related Reading" section to see more documentation on relevant technologies. We'll use the "Dynamic Tree Demo" applet as an example. You may want to setup build scripts to execute some of the following steps.

  1. Compile / build your applet's Java code and make sure all class files and resources such as images etc. are in a separate directory, example build/components.
  2. Create a jar file containing your applet's class files and resources. For example, the following command creates a jar file with the class files in the build/components directory.
    cd build 
    jar cvf  DynamicTreeDemo.jar  components
    			  		
  3. Sign your jar file if the applet needs special security permissions, for example, to read or write a file on client etc.
    jarsigner -keystore myKeyStore -storepass abc123 -keypass abc123 DynamicTreeDemo.jar johndoe
    where
    • keystore is setup and located at "myKeyStore"
    • alias is "johndoe"
    • keystore password and alias password are "abc123"
  4. Create a JNLP file that describes how your applet should be launched.
    dynamictree-applet.jnlp
    <?xml version="1.0" encoding="UTF-8"?>
    <jnlp spec="1.0+" codebase="" href="">
        <information>
            <title>Dynamic Tree Demo</title>
    
            <vendor>Dynamic Team</vendor>
        </information>
        <resources>
            <!-- Application Resources -->
            <j2se version="1.6+"
                  href="/old?u=http%3A%2F%2Fjava.sun.com%2Fproducts%2Fautodl%2Fj2se&y=1999"
                  max-heap-size="128m" />
    
            <jar href="/old?u=http%3A%2F%2Fblogs.oracle.com%2Fthejavatutorials%2Ftags%2FDynamicTreeDemo.jar&y=1999" main="true" />
    
        </resources>
        <applet-desc 
             name="Dynamic Tree Demo Applet"
             main-class="components.DynamicTreeApplet"
             width="300"
             height="300">
         </applet-desc>
    </jnlp>					
  5. Create the HTML page that will display the applet. Invoke the runApplet function from the Deployment Toolkit to deploy the applet.
    AppletPage.html
    <body>
        ....
        <script src="/old?u=http%3A%2F%2Fjava.com%2Fjs%2FdeployJava.js&y=1999"></script>
    
        <script> 
            var attributes = { code:'components.DynamicTreeApplet',  width:300, height:300} ; 
            var parameters = {jnlp_href: 'dynamictree-applet.jnlp'} ; 
            deployJava.runApplet(attributes, parameters, '1.6'); 
        </script>
        ....
    </body>					
  6. For this example, place DynamicTreeDemo.jar, dynamictree-applet.jnlp, and AppletPage.html in the same directory on the local machine or a web server. A web server is not required for testing this applet.
  7. View AppletPage.html in a web browser. The Dynamic Tree Demo Applet will be displayed. View Java Console Log for error and debug messages.

This method of applet deployment should work on JDK 7 too, once the required features are forward ported from Java SE 6u10.

Want to learn more about the Deployment Toolkit? Check out Deployment Toolkit 101.

Source Code

You can download the NetBeans project for this example


Related Reading


We'd love to hear feedback! Tell us about other deployment related topics that you'd like information on.

Stay tuned for more blogs on this subject...

-- Sowmya Kannan

About

thejavatutorials

Search
Recent Posts
Top Tags
Categories
Archives
« May 2011
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    
       
Today
Bookmarks
Menu
Feeds