Beginner: Get Local IP address from Java applet
Hey guys,
I'm new to java programming, and look forward to some help with my java applet here. I've tried to follow some guides, i might have just stuck my nose too far ahead for my skill level, but can you help me out?
Im trying to receive Local and Public IP address, by running a web applet.
The code goes:
Code :
import java.applet.Applet;
import java.net.*;
import java.util.*;
import java.io.*;
public class GetIP extends Applet
{
public static void main(String[] args) {
System.out.println("Test print");
try
{
System.out.println("Local IP: " + InetAddress.getLocalHost().getHostAddress());
}
catch (Exception ex)
{
System.out.println("Error: " + ex.getMessage());
}
try
{
String url = "http://vallentinsource.com/globalip.php";
BufferedReader br = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
String receive = br.readLine();
System.out.println("PublicIP: " + receive);
}
catch (Exception ex)
{
System.out.println("Error: " + ex.getMessage());
}
}
}
Beginner: Get Local IP address from Java applet
Re: Beginner: Get Local IP address from Java applet
How are you executing the code? Applets are meant to run in a browser not with the java command.
Re: Beginner: Get Local IP address from Java applet
Quote:
Originally Posted by
Norm
How are you executing the code? Applets are meant to run in a browser not with the java command.
Hi Norm,
Thanks for replying.
I'm executing the code from a .html file, such as:
Code :
<Html>
<Head>
<Title>Java test</Title>
</Head>
<Body>
Test<br>
<br>
<br>
<Applet Code="F:\Path\to\class\GetIP.class">
</Applet>
</Body>
</Html>
Am i missing something?
When i do run the code from my console, it reports back fine with the results.
Re: Beginner: Get Local IP address from Java applet
Read up on applets and how they are executed in a browser:
Lesson: Java Applets (The Java™ Tutorials > Deployment)
Applets doe NOT use a main() method. That's for apps