Hot questions for Using Applets in netbeans

Question:

How can I debug or run a class that extends java.applet.Applet? I am able to run the applet from the command line with appletviewer.

appletviewer runapp.html

Here is what class looks like:

import java.awt.Graphics;
import java.applet.Applet;

public class FancyApplet extends Applet {
    @Override
    public void paint(Graphics g) {
        g.drawString("Hello World!", 20, 20);
    }
}

Here is what the HTML file looks like

<!doctype html>

<head>
    <meta charset="utf-8">
    <title>A Fancy Applet</title>
</head>

<object type="application/java" width="200" height="300">
    <param name="code" value="FancyApplet.class">
</object>

How do I have to organize those files within my project and what settings do I have to use to debug the Applet in NetBeans? The thread Running a java applet from netbeans? did not help unfortunately. I did what was suggested, but get a message telling me that the main class was not found. What I took from it though was that in my case the HTML file is not even needed? But just because I am curious is it still possible or recommended to use it?


Answer:

As you don't have public static void main(String[] args) in your FancyApplet class, so the above error is produced.

You can run your java applet in the NetBeans IDE by pressing Shift + F6 key from your main class(keeping cursor inside FancyApplet class here). It'll run the applet program without searching for the main method.

This is an alternate shortcut method of running individual classes in NetBeans IDE.

Question:

I am trying to run my applet in netbeans IDE But browser is not opening automatically. If I copy address to browser it is running. Project runs in localhost.

Error is as follows---

  java.util.MissingResourceException: Can't find resource for bundle    
  org.openide.util.NbBundle$PBundle, key MSG_win_browser_invocation_failed
at java.util.ResourceBundle.getObject(ResourceBundle.java:450)
at java.util.ResourceBundle.getString(ResourceBundle.java:407)
at org.openide.util.NbBundle.getMessage(NbBundle.java:642)
at 
  org.netbeans.modules.extbrowser.NbDdeBrowserImpl$URLDisplayer.dispatchURL
  (NbDdeBrowserImpl.java:438)
at org.netbeans.modules.extbrowser.NbDdeBrowserImpl$URLDisplayer.run
   (NbDdeBrowserImpl.java:354)

  [catch] at java.lang.Thread.run(Thread.java:745)

Answer:

Sounds like a known issue in the Netbeans.

https://netbeans.org/bugzilla/show_bug.cgi?id=237542

It has a lots of dulicates over different versions, looks like they fixed in the nightly build, but it is still not released.

Question:

I am trying to build a very simple program with java applet.

The program should navigate to corresponding website as an user click on their links which are appearing on the applet window.

But in my case no applet window is popping up! Well there is a window popping up (below is the image) (as soon as I run the code) which is not the relevant with my program.

.java:

    package stringpractice;

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.net.*;
import javax.swing.plaf.basic.BasicListUI;

public class Applet extends JApplet {
    private HashMap<String,URL>websiteInfo;
    private ArrayList<String>titles;
    private JList mainList;

    private void inIt(){
        websiteInfo =new HashMap<String, URL>();
        titles =new ArrayList<String>();
        grabHTMLInfo();

        add(new JLabel("What website u wanna visit?"),BorderLayout.NORTH);
        mainList=new JList(titles.toArray());

        mainList.addListSelectionListener(


        new ListSelectionListener() {

            //@Override
            public void valueChanged(ListSelectionEvent event) {

                Object object = mainList.getSelectedValue();
                URL newDocument = websiteInfo.get(object);
                AppletContext browser = getAppletContext();
                browser.showDocument(newDocument);

            }
        }
 );
        add(new JScrollPane(mainList),BorderLayout.CENTER);

    }
    private void grabHTMLInfo(){
        String title;
        String address;
        int counter=0;
        URL url;
        title=getParameter("title"+counter);
        while(title!=null){
            address=getParameter("address"+counter);
            try {
                url =new URL(address);
                websiteInfo.put(title, url);
                titles.add(title);
            } catch (MalformedURLException uRLException) {

                uRLException.printStackTrace();
            }
            ++counter;
            title=getParameter("title"+counter);
        }



    }


}

.java main:

package stringpractice;

public class appletMain {

    public static void main(String[] args) {

    }

}

.HTML:

<html>
<title>testing applet</title>

<body>

<applet code="Applet.class" width="500" height="200">

<param name="title0" value="Google">
<param name="address0" value="https://www.google.com/?gfe_rd=cr&ei=M8ovVKu9AujJ8gfH8oH4Cw">


<param name="title1" value="Yahoo">
<param name="address1" value="https://se.yahoo.com/">



</applet>

</body>



</html>

Answer:

Your init() method is written as inIt(). As everything is case sensitive, your init method gets never called.

Question:

Why the program search the file:

    File FILE_PATH = new File("‪‪C:\\Users\\home\\Desktop\\DbWord.txt");
    System.out.println(FILE_PATH.exists());
    System.out.println(FILE_PATH.getAbsoluteFile());
    FileInputStream fIn = new FileInputStream(FILE_PATH);
    Scanner reader = new Scanner(fIn);

at: C:\Users\home\Documents\NetBeansProjects\MyDatabase\‪‪C:\Users\home\Desktop\DbWord.txt

How can i counteract the default location?

If something in this post not good, please tell me and no negative vote.

Thanks!

why negative votes??????????????? whats your problems??????????


Answer:

Please double check your error details. You might have seen something like the below error. Actually the program is not searching for the file "C:\Users\home\Documents\NetBeansProjects\MyDatabase\‪‪C:\Users\home\Desktop\DbWord.txt", it is trying to locate the file "C:\Users\home\Desktop\DbWord.txt" which does not exists in your machine. You are seeing "C:\Users\home\Documents\NetBeansProjects\MyDatabase\‪‪C:\Users\home\Desktop\DbWord.txt" along with the error because you have already used System.out.println(FILE_PATH.getAbsoluteFile()); statement in your code.

false
Exception in thread "main" java.io.FileNotFoundException: ‪‪C:\Users\home\Desktop\DbWord.txt (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
C:\Users\home\Documents\NetBeansProjects\MyDatabase\‪‪C:\Users\home\Desktop\DbWord.txt
    at com.stackoverflow.answer.SimpleFileHelper.main(SimpleFileHelper.java:17)

Hope you are clear now.

There are three main chances where a FileNotFoundException may be thrown.

  1. The named file does not exist.
  2. The named file is actually a directory not file.
  3. The named file cannot be opened for reading due to some reason.

The first two reasons are unlikely based on your description, please check the third point using file.canRead() method.

If the test above returns true, I would suspect the following:

You might have forgotten to explicitly throw or catch the potential exception (i.e., FileNotFoundExcetion). If you work in an IDE, you should have got some complaint from the compiler. But I suspect you didn't run your code in such an IDE.

Try the following code and see if the exception would be gone:

package com.stackoverflow.answer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class SimpleFileHelper {

    public static void main(String[] args) throws FileNotFoundException {
        File FILE_PATH = new File("C:/Users/home/Desktop/DbWord.txt");
        System.out.println(FILE_PATH.exists());
        System.out.println(FILE_PATH.getAbsoluteFile());
        FileInputStream fIn = new FileInputStream(FILE_PATH);
        Scanner reader = new Scanner(fIn);
    }
}