Hi and I must know if this script is valid to determine if the Java console and my Java compiler need reinstalling. I have tried so many samples and my browser shows an invalid message. My file is named 'filename.class' and below is the script I have used to test Java on my computer (but I don't know much Java).

class filename
    {
    public static void main(String[] args)
        {
        short vara;
        int varb;
        long varc;
        double vard;
        float vare;
        byte varf;
        char varg;
        String varh;
        boolean vari;

        vara=1;
        varb=127349634;
        varc=400000000;
        vard=3.6290048129;
        vare=95;
        varf='i';
        varg='/';
        varh="this is a string";
        vari=true;

        System.out.print("short="+vara);
        System.out.print("int="+varb);
        System.out.print("long="+varc);
        System.out.print("double="+vard);
        System.out.print("float="+vare);
        System.out.print("byte="+varf);
        System.out.print("char="+varg);
        System.out.print("String="+varh);
        System.out.print("boolean="+vari);
        }
    }

Let me know if there are any errors in the above script. Also, the error report that I am given is first a message in the browser status bar saying "applet filename notinited" (filename is the name of the applet). Also the report is as follows:

load: filename.class is not public or has no public constructor.
java.lang.IllegalAccessException: Class sun.applet.AppletPanel can not access a member of class filename with modifiers ""
	at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
	at java.lang.Class.newInstance0(Unknown Source)
	at java.lang.Class.newInstance(Unknown Source)
	at sun.applet.AppletPanel.createApplet(Unknown Source)
	at sun.plugin.AppletViewer.createApplet(Unknown Source)
	at sun.applet.AppletPanel.runLoader(Unknown Source)
	at sun.applet.AppletPanel.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

Does anybody know what is going on here because I have never gotten a single script to work.
P.S. My java compiler is "JCreator LE" and I have Java 3D installed.

Recommended Answers

All 9 Replies

First of all, you need to call your file "filename.java" not filename.class. When you compile the java source file a class file will be created that you can run in a Command prompt or Unix Console (not a browser) by typing

java filename

Otherwise your code should work as expected.

Hi and I must know if this script is valid to determine if the Java console and my Java compiler need reinstalling. I have tried so many samples and my browser shows an invalid message. My file is named 'filename.class' and below is the script I have used to test Java on my computer (but I don't know much Java).

class filename
    {
    public static void main(String[] args)
        {
        short vara;
        int varb;
        long varc;
        double vard;
        float vare;
        byte varf;
        char varg;
        String varh;
        boolean vari;

        vara=1;
        varb=127349634;
        varc=400000000;
        vard=3.6290048129;
        vare=95;
        varf='i';
        varg='/';
        varh="this is a string";
        vari=true;

        System.out.print("short="+vara);
        System.out.print("int="+varb);
        System.out.print("long="+varc);
        System.out.print("double="+vard);
        System.out.print("float="+vare);
        System.out.print("byte="+varf);
        System.out.print("char="+varg);
        System.out.print("String="+varh);
        System.out.print("boolean="+vari);
        }
    }

Let me know if there are any errors in the above script. Also, the error report that I am given is first a message in the browser status bar saying "applet filename notinited" (filename is the name of the applet). Also the report is as follows:

load: filename.class is not public or has no public constructor.
java.lang.IllegalAccessException: Class sun.applet.AppletPanel can not access a member of class filename with modifiers ""
	at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
	at java.lang.Class.newInstance0(Unknown Source)
	at java.lang.Class.newInstance(Unknown Source)
	at sun.applet.AppletPanel.createApplet(Unknown Source)
	at sun.plugin.AppletViewer.createApplet(Unknown Source)
	at sun.applet.AppletPanel.runLoader(Unknown Source)
	at sun.applet.AppletPanel.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

Does anybody know what is going on here because I have never gotten a single script to work.
P.S. My java compiler is "JCreator LE" and I have Java 3D installed.

There are no errors in the code. And you are quoting this as an applet which is wrong. This is a stand alone java programm. Which is suppose to be working.

The possible reason for the error:
1. The environment variable JAVA_HOME is not set.

to set the same set JAVA_HOME to the installation directory of jdk. (C:\Program Files\Java\jdk1.6.0_01 in my pc) .

Also add %JAVA_HOME%\bin to path environment variable.

You follow the following steps to run the program:
1. Save the file with name FileName.java(Standard java code convention not mandator but recommended)
2.javac FileName.java
to compile, this will create FileName.class file
3.java FileName
will run and display your desired output.

The same can be done from JCreator too but you need to set the build path.

Hope this will help you.

Thanks.
Feel free to contact me at:SNIPPED

You follow the following steps to run the program:
1. Save the file with name FileName.java(Standard java code convention not mandator but recommended)
2.javac FileName.java
to compile, this will create FileName.class file
3.java FileName
will run and display your desired output.

I have just tried those steps and steps 2 and 3 each came with an error. When I tried step 2 through the windows command prompt (I was in the same directory as the script), a message then appeared inside the command prompt saying "'javac' is not recognized as an internal or external command, operable program or batch file."
Does that mean I need to install something for the computer to recognized that command? And step 3 which had a more useful error displayed the following which I have seen in JCreator before but wont redisplay in JCreator:
'Exception in thread "main" java.lang.NoClassDefFoundError: filename'
So what could possibly cause the above fatal error which by the looks of it, prevents the applet from compiling properly. How do I solve the errors?
Note: before I was trying to run the applet through the browser which is why I didn't see these 2 errors before.

The error in Step 2 is caused by the fact that the %JAVA_HOME%\bin directory has not been added to your PATH variable. See afzal_01's post about this. Step 3 failed only because Step 2 did also.

I just searched the web for more info on what the path variable should be and I found a handy link at http://www.apl.jhu.edu/~hall/java/beginner/settingup.html
So I re-followed the instructions in afzal_01's post (also copied the java source code file into the bin directory) and although it exported the .class file, none of my 5 web browsers recognize the file. Even though step 3 will view the file, no browser will view the .class file. And all the browsers still report the "applet filename notinited" error as mentioned in the first post. Is there any explination to why the command prompt can view the java file but none of my web browser will view the .class file? (Web browsers tested: Internet Explorer, Safari, Google Crome, Firefox, Opera)

Is there any explination to why the command prompt can view the java file but none of my web browser will view the .class file? (Web browsers tested: Internet Explorer, Safari, Google Crome, Firefox, Opera)

Yes, because the code you are trying to run is not an applet, it is just a script that prints to standard output. That's what all those lines that read System.out.println mean.

Then I guess I will need to do a lot more research on Java to find out how to make proper Java applets. But for the time been I will leave this thread open for those who might know what makes a Java applet work.
But other than any comments on that I guess my debugging problem is mainly solved (just a lot of research to do).

Take a look at the Java API in the Applet class. You will also need to embed the applet in an HTML page, but it has been so long since I have done that I am not sure I can remember exactly what to do.

Just before I mark this thread as solved, I have managed to find a tutorial for displaying text in Java and now my code is as below.

import java.applet.*;
import java.awt.*;
 

//the below name starts the applet with the files name called 'filename'.
public class filename extends Applet
    {
        short vara;
        int varb;
        long varc;
        double vard;
        float vare;
        byte varf;
        char varg;
        String varh;
        boolean vari;

    public void init()
        {
        vara=1;
        varb=127349634;
        varc=2000000000;
        vard=3.57;
        vare=95;
        varf='i';
        varg='/';
        varh="this is a string";
        vari=true;
        }
 

// This method gets called when the applet is terminated
// That's when the user goes to another page or exits the browser.
    public void stop()
        {
     // no actions needed here now.
        }
 

// The standard method that you have to use to paint things on screen
    public void paint(Graphics g)
        {
        g.drawString("short="+vara,10,20);
        g.drawString("int="+varb,10,40);
        g.drawString("long="+varc,10,60);
        g.drawString("double="+vard,10,80);
        g.drawString("float="+vare,10,100);
        g.drawString("byte="+varf,10,120);
        g.drawString("char="+varg,10,140);
        g.drawString("String="+varh,10,160);
        g.drawString("boolean="+vari,10,180);
        }

    }

Thanks for the help peoples as this is my first working Java applet and can't wait to do more (from online tutorials).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.