hi to all
i made a code to find the mac number that runs on java and gives me output but when i implement it on applet and link the applet with html then applet links correctly but the logic to find mac number does not execute.....what should i do...here is my code

import java.net.*;


public class Main {

    
    public static void main(String[] args) {
   

 InetAddress ip;
	try {

		ip = InetAddress.getLocalHost();
		System.out.println("Current IP address : " + ip.getHostAddress());

		NetworkInterface network = NetworkInterface.getByInetAddress(ip);

		byte[] mac = network.getHardwareAddress();

		System.out.print("Current MAC address : ");

		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < mac.length; i++) {
           sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
		}
		System.out.println(sb.toString());

	} catch (UnknownHostException e) {

		e.printStackTrace();

	} catch (SocketException e){

		e.printStackTrace();

	}
     
    }

}

what i can use instead of system.out.println

Dani AI

Generated

Quick summary and practical fixes (builds on 's paint suggestion). System.out.println in an applet goes to the Java runtime console (and only if the console is enabled) — it doesn't render on the page. To show text in the applet area use AWT/Swing (paint, Labels), call showStatus(...) to write to the browser/plugin status area, or call JavaScript from the applet (LiveConnect) to update the page DOM.

Example patterns you can use:

/* showStatus (simple, part of Applet) */
showStatus("Local IP: " + ip.getHostAddress());
/* call JS to write into an element with id="out" */
JSObject.getWindow(this).eval(
  "document.getElementById('out').innerText = 'IP: " + ip + "';"
);

Why the IP looked “wrong”: InetAddress.getLocalHost() depends on the client machine's hostname/DNS and on which network interface the JVM picks. In many setups it resolves to loopback, a VPN, or a virtual adapter. A more robust client-side approach is to enumerate network interfaces and choose the first non-loopback IPv4 that is up:

Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
while (nifs.hasMoreElements()) {
  NetworkInterface nif = nifs.nextElement();
  if (!nif.isUp() || nif.isLoopback()) continue;
  for (Enumeration<InetAddress> e = nif.getInetAddresses(); e.hasMoreElements();) {
    InetAddress addr = e.nextElement();
    if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
      String ip = addr.getHostAddress();
      byte[] mac = nif.getHardwareAddress(); // may be null
    }
  }
}

About the MAC and security: getting a physical MAC from the client is often blocked or returns null in an unsigned applet. Access to local hardware or sensitive info requires signing the JAR and granting permissions (or running native code on the client). A common, safer pattern is to have the applet contact its origin server (unsigned applets may connect back to the server that served them) and let the server report the public IP (server-side gets remoteAddr). For debugging, run your applet with appletviewer or enable the Java Console so System.out/System.err and stack traces are visible. Note that browser support for Java applets has been removed in most modern browsers, so consider migrating to a server-side + JavaScript solution or a signed desktop application when appropriate.

I don't think that you can use println in this case. You need something like this:

public void paint(Graphics g) {
        g.drawString("Hello world!", 50, 25);
    }

thanks that works but the ip address i got is not correct in applet....why this happened

ip = InetAddress.getLocalHost();
               
                
		msgs="Current IP address : " + ip.getHostAddress();
                g.drawString(msgs,10,40);

i got output but not correct.....help me

hello,
try this :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication8;

import java.net.InetAddress;
import java.applet.*;
import java.awt.*;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class JavaApplication8 extends Applet{
    
      public void paint( Graphics g ) {
        try {
            InetAddress local = InetAddress.getLocalHost();         
        String myLocalIP = local.getHostAddress();
            //String ip = InetAddress.getLocalHost().toString();
             g.drawString(myLocalIP,10,40);
        } catch (UnknownHostException ex) {
            Logger.getLogger(JavaApplication8.class.getName()).log(Level.SEVERE, null, ex);
        }
     }   
}

If you don't see what you expect to see please provide us with more information about the error (error message etc etc). Using that, you are able to take your private IP (The IP that has been assigned to your PC by your router[dhcp]) not your public IP.

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.