0

I seem to be having an issue with getting my Java program to run in the web browser using the <applet> tag.

Here's my code for importing the java .class:

<applet code="Userid.class"width="740" height="400"></applet>

For some reason, I keep getting an error that says "NoClassDefFoundError Userid (wrong name: userid/Userid)"

The Java program itself is not graphical if that is one of the issues. Just in case, here is my source code for the Java application:

package userid;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.Writer;
import java.io.File;
import java.io.IOException;

public class Userid {

public static void main(String[] args) {
    Scanner in = new Scanner (System.in);
    String userid = in.nextLine();
    try{
        File users = new File(userid+".txt");
        BufferedWriter output;
        output = new BufferedWriter(new FileWriter(users, true));
        output.newLine();
        output.append(userid);
        output.close();
        new File(userid).mkdirs();
        System.out.println("> New user " +userid+ " has been added.");
        System.out.println("> Please use this name everytime you use Oswald.");
    }
    catch (IOException e) {                
    }
}    
}

The program runs just fine in NetBeans and the .class is in the same exact directory as the HTML file. Am I perhaps doing something wrong here? Thanks!

UltraStallion
  • 131
  • 1
  • 3
  • 10

2 Answers2

0

One problem is that the class is not extending from the java.applet.Applet class. To do that, you'd need to import java.applet.Applet and then change your class definition to:

public class Userid extends Applet {

However, like what was mentioned before, your program is not written to be an applet, it's written to be used with the command line, hence the System.in and System.out streams. I'm not going to question your decision to write an applet, even though (as chrylis said) they are all but obsolete, because they are a good way to practice Java skills, especially when working with the graphical interface.

Here is a resource to get you started with applets: http://java.about.com/od/webapplications/ss/firstapplet.htm

amklose
  • 1,143
  • 8
  • 15
0

As far as your Java code is concerned. There are no big problems in that. It can run fine as a standalone program. But, problem is about Applet

As per W3C, the applet tag has been depreciated (Check Here For More Info)

Previous versions of HTML allowed authors to include images (via IMG) and applets (via APPLET). These elements have several limitations:

They fail to solve the more general problem of how to include new and future media types. The APPLET element only works with Java-based applets. This element is deprecated in favor of OBJECT. They pose accessibility problems.

Though there are possible alternatives like Deployment Tool Kit for DeployJava.js which may allow you to do what you are looking for.

Also, I will advice you to search before posting to gain more knowledge on what you are looking for.

Go through this and this to understand the basics of how to do what you intend to do.

Community
  • 1
  • 1
Techidiot
  • 1,921
  • 1
  • 15
  • 28