Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Help with Simple Java Problem, plz?

Options
  • 05-07-2001 3:05am
    #1
    Closed Accounts Posts: 1,322 ✭✭✭


    Guys,

    I am having a problem with simply getting an applet to work. Let's just recap to make sure I am doing everything right.

    1. WRITE YOUR JAVA APPLET E.g.
    import java.awt.*;
    import java.applet.*;
    
    public class myApplet extends Applet{
        void myApplet(){
        
        }
    
        public void paint(Graphics g){
            g.drawString("Hello, World!", 10, 10);
        }
    }
    
    2. COMPILE THE CODE (on the command line)
    javac myApplet.java

    4. CALL THE APPLET FROM THE HTML DOCUMENT E.g.
    <APPLET CODE="myApplet"></APPLET>
    or
    <APPLET CODE="myApplet.class"></APPLET>

    Am I leaving something out, because anytime I try the above it results in my browser giving me the famous "gray box of nothingness". And on the browser status bar it says that the class myApplet cannot be found. The HTML document and the .class file exist in the same directory. I have used applets on my web sites before. The only difference is that I didn't write them.

    I know one of you out there will be able to answer this stupid question in an instant. So if you have the time to point me in the right direction, giz a bell.

    :-phobos-)


    [This message has been edited by phobos (edited 05-07-2001).]


Comments

  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Worked just fine for me, try using appletviewer to run your applet:
    appleviewer myApplet.html

    You'll need to set a width and height in your applet tag for this to work.

    What version of Java are you using?


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Is your CLASSPATH set?


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    How can I configure the JRE. I guessed that was the problem. But don't you think that this is a big problem. Because let's say I am not a programmer and I want to use an applet on a site, but I get this kind of error message. I would have no ideas as to what's wrong. Is this problem bigger than we think?

    Running stuff like servlets is no problem because the web server can execute them (on the server side). But when it comes to executing stuff inside the web browser there is a problem. But then again, when I vist web sites that have applets on them, they work fine. It is just the applets that I am testing that don't work. The first post in this thread dictates the exact steps I am taking, when it comes to applet deployment.

    ...thanx 4 da feeback lads

    ;-phobos-)

    [This message has been edited by phobos (edited 05-07-2001).]


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    It's more than likely a problem with the JRE rather than the CLASSPATH. It wouldn't have compiled if the CLASSPATH was wrong.


  • Registered Users Posts: 380 ✭✭dogs


    <font face="Verdana, Arial" size="2">Originally posted by phobos:
    public class myApplet extends Applet{
        void myApplet(){ }
    }
    
    </font>

    isn't myApplet() the myApplet constructor ? but you're giving it a 'void' return type, maybe change it to...
         public myApplet() { ... }
    
    ...instead ?


  • Advertisement
  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    Actually my original post had that error in it, but my efforts don't. The constuctor is defined as:

    public myApplet(){

    }


    To write the code I am using the JBuilder 3.5 editor, saving the *.java files and compiling them at the command line using javac. If I were to compile/run them in JBuilder there is no problem, they work. But it is when I take that exact class file and try to get it working outside of JBuilder things go wrong. So it has to be down to the Browser Runtime Environment. But then my next question is, why does applets I download from the web work in my browser?

    ;-phobos-)



  • Closed Accounts Posts: 589 ✭✭✭Magwitch


    Paint is used to display images, not text, and has far more parameters. If you want to display text try the "println" instead....

    import java.io.*;

    class MyApplet
    { public static void main (String[] args)
    {
    System.out.println ("hello world");
    }
    }

    For the paint method to work, replace the ("Hello, world" 10, 10) with (Image path, 10, 10, this) where image path is where the IMAGE you want displayed is stored on your computer or server, keeping the image in the same folder as the app will negate the need for a full path (just put in the image name and file extension). "this" is a reference to the ImageObserver Java component which reloads the image. You must also use the Public void init() statement with the paint method to initiate the painting applet.

    You program is working but has nothing to display.

    JBuilder sets the Classpath upon instalation and there will not be a problem unless you install another java authoring program which may conflict unless the Classpath is altered manually.


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    Mag,

    I think you are a little confused when it comes to overriding the paint method of the Applet class. The method drawString defined in the awt graphics class, will draw a string to an XY coord on a given canvas. So in my code example I am merely doing that. The method you are describing is drawImage which is also defined in the graphics class. Also I don't think an applet will have a main() method (that is for applications). That is called when launching an application and can obtain command line parameters etc. Applets have a seperate method for doing just that.

    That aside, I still think it is irrelavant what the applet is supposed to do. I could have easily created an applet that would simply resize itself and change it's background colour (all in the class constructor), without having to bother overriding the paint method, and I'm sure the same thing would have happened.

    Cheers anyway, anyone got any more ideas...

    :-phobos-)


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    K,

    I still haven't found a sollution to this problem yet. Surely amongst the programmers on these boards, somebody will know what's wrong!.

    I can see how this can be overlooked because Applets are not something everybody would care about. But when developing internet applications, tis something that stands out, hense is quite important.

    If I don't get a response over the next couple of days I will assume nobody has a clue, and I will leave it. frown.gif

    Thanx for your help guys!

    ;-phobos-)


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Try re-installing Java. And your browsers too. Drastic I know but it ran just fine for me so it's nothing wrong with the code so it's got to be your setup.


  • Advertisement
  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    But it's not just my machine. I could not get it working in college either. Everybody in my class was unable to get it working. My lecturer got ****ed off (tried using different browsers etc), and nothing worked, so he left it alone. He was acutally hoping that one of us would get it. But the thing is, is that I need to get it working for a project that I am currently working on. Every single FAQ that I looked through doesn't answer my question. It is unbelievably frustrating because I have gotten much more complex stuff up and running with Java. frown.gif

    Thanx 4 da help

    :-phobos-)


  • Registered Users Posts: 2,277 ✭✭✭DiscoStu


    try this

    <applet code="***" codebase = "***" width="x" height="y">
    </applet>

    after "code" enter the applet name. after "codebase" enter the the full url to the directory that the applet's class file is contained in ie "http://www.ihopethisworksoriwillhaveeggonmyface.com/classes/
    the width and height tags are self explanitory".

    i also belive that the dimensions of the applet must be defined in its constructor but im not to sure and really couldnt be arsed looking it up at this time of night.

    also if you are runningthe applet locally and not with a web server running on the machine make sure that the path to the file starts with this "file:///X|/" where X is the drive letter, and all spaces in the path are replaced with %20.
    <sig>Insert witty signature here</sig>

    [This message has been edited by DiscoStu (edited 15-07-2001).]


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    DiscoStu you are the MAN!! smile.gif

    Twas the CODEBASE. Under my nose all the time and thanx to you m8, I can forget about this little bugger. I dunno how I could be so stupid. One other thing I noticed is that since I have been developing in different languages over the past few years, I have to remember the little differences between them (such as case sensitivity etc). This is also the case this time. Because HTML is so flexible (ie. you could practiacally type anything + it will still work), I got the case of the *.class file wrong (in the APPLET tag), which also didn't help.

    Thanx again m8

    ;-phobos-)



  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    Just when I though the coast was clear.

    It seems that simple applets work. By simple, I mean applets that stick to the AWT libraries. If I try to construct an applet with some kind of GUI (using Swing for example), and execute it in IE, there is a new error message this time. The status bar in IE says "Can't instantiate [classname]". So it can find the class (original problem solved) but it can instantiate it!. WTF is going on?.

    I am running IE 5.0 (which should have an updated version of the JVM). Does anyone know what the problem might be?

    ;-phobos-


  • Registered Users Posts: 2,277 ✭✭✭DiscoStu


    wow i almost solved a problem and i only got 50% in my java exams! smile.gif What jdk version are you using? as far as i know ie5 only supports the java spec up to jdk 1.2 and not 1.3 so if you are using 1.3 there will be problems. one thing you must remember is that the jvm in ie is microsofts interpretation of java. they say that it is 100% compatable but they also say directx is backwards compatable smile.gif

    <sig>Insert witty signature here</sig>


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    The funny thing Stu, is that I always get A's in Java (not to sound boastful).

    In college we used both Netscape and IE, nothing worked. It had crossed my mind that IE was using it's own JVM which might not be compatable. But the highest JDK I am using is 1.2.2 and J2SDKEE 1.2.1 . I haven't bothered with 1.3 yet. I heard a rumour that Swing gives lots of compatability problems in relation to applets, is this true?, should I simply use AWT?.

    On Microsoft's site you can download the new JVM for IE, but their last build was the end of last year (so that's kinda old). Is there anyway I can find out what version of the JVM IE is using on my machine?

    Thanks mate you are being a real help, I owe ya one.

    ;-phobos-)


  • Registered Users Posts: 2,277 ✭✭✭DiscoStu


    to get the jvm version in ie just enable the java console and open it up. its in internet options under the advanced tab.

    are you entering the correct path in the import statment? for swing it is javax.swing.*
    its a silly mistake but silly ones are the most common.

    come to think about it ive never come across an applet that uses swing.

    <sig>Insert witty signature here</sig>


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    <font face="Verdana, Arial" size="2">Originally posted by phobos:
    Is there anyway I can find out what version of the JVM IE is using on my machine?
    </font>

    Help about dialog? There are also windows UI classes which run fine afaik, however you end up screwing anyone not using Windows over. I don't know what they are offhand though.

    If your still learning Java I recommend getting these two books (both by O'Reilly)

    Java in a Nutshell and Java Examples in a Nutshell


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    Cheers lads,

    I am still learning Java, but would like to think I am passed the whole applet part. I am concentrating on J2EE stuff at the moment, which has more to do with a distributed architecture. But it's ironic when I go to write some simple client side ware I bugger it up.

    Cheers for the book ideas Hobbes, but if I buy another Java book I will be offically be living in a library. And yes O'Reilly books are the best. My trend is usually Sams if you are a complete beginner and then O'Reilly to learn the newer stuff fast.

    Something tells me I should keep away from the swing stuff and settle for AWT. That way I should be able be able to do what I want, without loosing cross-platform compatibility.

    ;-phobos-)


Advertisement