Results 1 to 6 of 6
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Cannot run JNLP / JApplet

    Hi, I am trying to run a JApplet base on Lesson: Java Applets (The Java™ Tutorials > Deployment) tutorial. I have compared the sample code on site and the code I did but still the calculator does not show.

    I am sure the calculator is working, have tested it on a JFrame before using JApplet. Calculator use only JPanel, JLabel and JButton.

    I am not sure if I have mistyped the sample code for loading JApplet.

    Main Class:
    Java Code:
    package projonlinecalculator;
    
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    
    public class Main extends JApplet
    {
    //    public static void main(String[] args)
        public void init()
        {
            try
            {
                SwingUtilities.invokeAndWait(new Runnable()
                {
                    public void run()
                    {
                        createGUI();
                    }
                });
            }
            catch (Exception e)
            {
                System.err.println("createGUI didn't complete successfully");
            }
        }
    
        private void createGUI()
        {
            //Create and set up the content pane.
            PnlCalculator calc = new PnlCalculator();
            calc.setOpaque(true);
            calc.setVisible(true);
            setContentPane(calc);
        }
    }
    JNLP: name is launch.jnlp
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <jnlp spec="1.0+" codebase="" href="">
        <information>
            <title>Simple Online Calculator</title>
            <vendor>Kristle and Jerome</vendor>
        </information>
        <resources>
    	<j2se version="1.5+" href="http://java.sun.com/products/autodl/j2se"/>
    	<jar href="projOnlineCalculator.jar" main="true"/>
        </resources>
        <applet-desc
    	main-class="projOnlineCalculator.Main"
    	width="300"
    	height="300">
        </applet-desc>
    <update check="background"/>
    </jnlp>
    HTML:
    Java Code:
    <html>
    <head><title>Simple Java Calculator</title></head>
    <body>
    Hi! Just trying the JApplet.<br />
    <script src="http://www.java.com/js/deployJava.js"></script>
    <script>
        var attributes = { code:'projOnlineCalculator.Main', archive:'projOnlineCalculator.jar',  width:300, height:300};
        var parameters = {jnlp_href: 'launch.jnlp'};
        deployJava.runApplet(attributes, parameters, '1.6');
    </script>
    </body>
    </html>
    Thanks,

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Cannot run JNLP / JApplet

    I was able to run the JApplet, problem or my error was I did not override init(). My first understanding was just one of public static void main(String[]) or public void init() will be used.

    I have a follow-up question. When I run the applet and close the tab (tabl ONLY) on the browser (the browser is still on but the tab that executed the JApplet is already close), then compile again the application it says that it cannot delete or overrigth the .jar file. I suspect that JApplet is still running, how would i deal into that? Should I override stop()?

    Here is the latest Main.class
    Java Code:
    package projonlinecalculator;
    
    import java.awt.BorderLayout;
    import javax.swing.JApplet;
    //import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class Main extends JApplet
    {
        //PnlCalculator calc;
        public static void main(String[] args)
        {
            try
            {
                SwingUtilities.invokeAndWait(new Runnable()
                {
                    public void run()
                    {
                        Main mainClass = new Main();
                        mainClass.init();
                    }
                });
            }
            catch (Exception e)
            {
                System.err.println("createGUI didn't complete successfully");
            }
        }
    
        public void init()
        {
            createGUI();
        }
    //
    //    public void stop()
    //    {
    //        calc.
    //    }
    
        private void createGUI()
        {
            //Create and set up the content pane.
            PnlCalculator calc = new PnlCalculator();
            calc.setOpaque(true);
            calc.setVisible(true);
            setLayout(new BorderLayout());
            setContentPane(calc);
        }
    }
    Thanks

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Cannot run JNLP / JApplet

    the application it says that it cannot delete or overrigth the .jar file
    Is that the OS that gives the error message? Is it because the browser still has the jar open so the OS will not allow the jar file to be changed until the browser gives up control of the file.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Cannot run JNLP / JApplet

    Also note that a JApplet's init() method isn't called on the EDT and needs Swing GUI code to be wrapped in invokeLater(...)
    Getting Started With Applets (The Java™ Tutorials > Deployment > Java Applets)

    db

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Cannot run JNLP / JApplet

    Hi, thanks for the reply, would follow your advices and post latest code tomorrow.

    In this site Life Cycle of an Applet (The Java™ Tutorials > Deployment > Java Applets) I have read that to be able to do that I have to override stop() and destroy() method. Sorry to ask this but what should I put on the method? I only use JPanel, JButton and JLabel. I do not see any component to clear. Kindly correct me if I am wrong.

    Thank again... :)

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Cannot run JNLP / JApplet

    Hi, here is the latest code I have after following your suggestions/advices. I call removeAll() in stop() method but not sure if this is correct.
    I also remove codebase in JNLP because it gives error when I run it on web.
    Java Code:
    [b]LAUNCH.JNLP[/b]
    <?xml version="1.0" encoding="UTF-8" ?>
    <jnlp spec="1.0+"
    	href="launch.jnlp" >
        	<information>
            	<title>SIMPLE CALCULATOR</title>
            	<vendor>bibo4</vendor>
            </information>
        	<resources>
    		<j2se version="1.5+"/>
    		<jar href="projOnlineCalculator.jar" main="true"/>
        	</resources>
        	<applet-desc
    		height=250
    		width=350
    		main-class="projonlinecalculator.Main"
    		name="projOnlineCalculator">
    	</applet-desc>
    <update check="background"/>
    </jnlp>
    Java Code:
    [b]MAIN.CLASS[/b]
    package projonlinecalculator;
    
    import java.awt.BorderLayout;
    import javax.swing.JApplet;
    //import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class Main extends JApplet
    {
        //PnlCalculator calc;
        public static void main(String[] args)
        {
            try
            {
                SwingUtilities.invokeAndWait(new Runnable()
                {
                    public void run()
                    {
                        Main mainClass = new Main();
                        mainClass.init();
                    }
                });
            }
            catch (Exception e)
            {
                System.err.println("createGUI didn't complete successfully");
            }
        }
    
        public void init()
        {
            try
            {
                SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        createGUI();
                    }
                });
            }
            catch (Exception e)
            {
                System.err.println("createGUI didn't complete successfully");
            }
        }
    
        public void stop()
        {
            removeAll();
        }
    
        private void createGUI()
        {
            //Create and set up the content pane.
            PnlCalculator calc = new PnlCalculator();
            calc.setOpaque(true);
            calc.setVisible(true);
            setLayout(new BorderLayout());
            setContentPane(calc);
        }
    }
    Thanks,

Similar Threads

  1. >>>> How To Open An .jnlp Twice? <<<<
    By oklol911 in forum New To Java
    Replies: 1
    Last Post: 02-22-2011, 03:37 AM
  2. Alternative to JNLP
    By tecno40 in forum Java Applets
    Replies: 0
    Last Post: 12-22-2010, 12:38 AM
  3. Jnlp
    By tecno40 in forum Java Applets
    Replies: 1
    Last Post: 11-29-2010, 03:37 AM
  4. JNLP Problem
    By barusk in forum Java Applets
    Replies: 2
    Last Post: 11-05-2010, 03:09 PM
  5. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 06:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •