Question : Java newbie - getting going questions!

I'm trying to create a simple Java class, and not having much success getting it to work!

I've plenty of experience in JavaScript (and other languages, generally the VB arena), but this is my first venture into "real" Java.

The class is simply to return the IP address of the local machine.

From a load of web searching examples, I've established that I can do the following in JS:

var IP = document.GetIPAddress;


With the class defined in a <applet> tag as follows:

<applet
 codebase = "."
 code     = "GetIPAddress.class"
 name     = "GetIPAddress"
 width    = "0"
 height   = "0"
 hspace   = "0"
 vspace   = "0">
</applet>


and the class is:

import java.net.*;
import java.io.*;

public class GetIPAddress {

      public static void main(String [] args) {

            try
            {
                  InetAddress thisIp = InetAddress.getLocalHost();
                  System.out.println(thisIp.getHostAddress());
            }
            catch (Exception e)
            {
                  e.printStackTrace();
            }

      }

}


But all I get returned in the IP variable is "[object HTMLAppletElement]"

Tried using Return instead of System.out, this fails to compile stating that the return cannot return a value (that has got me totally baffled, as that's the whole point of return!).


If I add a function to the class (e.g. for testing):

      public String Test()
      {
            return "Hello";
      }

This:

var IP = document.GetIPAddress.Test;

returns "undefined"


and this:

var IP = document.GetIPAddress.Test();

Throws the error "document.GetIPAddress.Test is not a function" - But it clearly is!


I'm obviously missing some basics here, but what?


Also, is it a requirement that a class is defined with <applet> tags on a web page, or can it be instantiated directly in JS?



Thanks




Jim

Answer : Java newbie - getting going questions!

Take a look at these examples.

http://java.sun.com/applets/jdk/1.4/index.html
Random Solutions  
 
programming4us programming4us