Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Beginner: Get Local IP address from Java applet

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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:

    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());
    		}
    	}
    }


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner: Get Local IP address from Java applet

    Wops. double post
    Last edited by Killingeskibet; January 26th, 2013 at 11:20 AM. Reason: Double post

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner: Get Local IP address from Java applet

    Quote Originally Posted by Norm View Post
    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:

    <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.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    curmudgeon (January 26th, 2013)

Similar Threads

  1. Java Applet Beginner
    By LoganC in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 8th, 2012, 08:17 PM
  2. How can I get Remote Address from Object? (Java RMI)
    By sahin in forum Java Networking
    Replies: 1
    Last Post: February 4th, 2012, 10:02 PM
  3. Address Book program in Java
    By DaveNAchill in forum Paid Java Projects
    Replies: 3
    Last Post: January 6th, 2012, 04:03 AM
  4. Address Book Program in java
    By DaveNAchill in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 09:25 AM
  5. Very beginner - What's wrong in my applet declaration?
    By rforte in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 30th, 2010, 04:54 AM