Solved

Requesting a URL

Posted on 2004-09-15
9
257 Views
Last Modified: 2010-03-31
I have an application where I need a java program that will open up a URL and cause the web page to be ran.  The java program will be opening a web page that is itself a PHP script.

The code that I have been playing around with is like this:
------------------------------------------------------------------------
import java.net.*;
import java.io.*;

class Link {
      public static void main(String[] args) {
            try
               {
                       URL url = new URL("http://192.168.5.100/index.php");
                        URLConnection connection = url.openConnection();
               }
               catch (Exception e)
               {
                        System.out.println(e.toString());
               }
      }
}
-----------------------------------------------------------------------

Any help would be appreciated.

Also I am using an older version of Java if that matters, I believe it is version 1.2.2.  Please explain your solutions in detail as I have very little Java knowledge.

--
Thanks in Advance
Blaine
0
Comment
Question by:blainehilton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 12071178
to read the page use:

class Link {
     public static void main(String[] args) {
          try
             {
                    URL url = new URL("http://192.168.5.100/index.php");
                     URLConnection connection = url.openConnection();
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String line = null;
                    while (null!=(line=in.readLine()))
                    {
                       System.out.println(line);
                    }
             }
             catch (Exception e)
             {
                     System.out.println(e.toString());
             }
     }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12071183
if you want to actually render the html page you'll need to use JEditorPane.
Let me know if you need more details.
0
 
LVL 1

Author Comment

by:blainehilton
ID: 12071250
Thank you for your fast response, no I do not need to view the page, just have it run the PHP.

I ran the code you sent and it complied fine but when I ran it I received an error saying "java.net.MalformedURLException".  This is the same error I was having with my example.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 92

Expert Comment

by:objects
ID: 12071278
strange it looks alright, is the URL you are using the same as what is posted here?
0
 
LVL 1

Author Comment

by:blainehilton
ID: 12071305
The URL is pretty much the same, but is different.  It is an IP address and not a hostname and I am using the http:// and the page does exist, and the device can ping the server.

Does it matter that this is running on a TINI Module?  (http://www.ibutton.com/TINI/index.html)

When I run it with the command "java Link.class" from the DOS prompt I receive a different error.  The dos prompt gives me:

Exception in thread "main" java.lang.NoClassDefFoundError: Link/class

0
 
LVL 92

Expert Comment

by:objects
ID: 12071393
> When I run it with the command "java Link.class"

should be:

java Link
0
 
LVL 92

Expert Comment

by:objects
ID: 12071394
> The URL is pretty much the same, but is different.

what is it exactly then?
0
 
LVL 1

Author Comment

by:blainehilton
ID: 12071467
Using the command "java Link" it worked great, however it is not working on the TINI.
0
 
LVL 2

Accepted Solution

by:
SergeiKo earned 250 total points
ID: 12074346
Hello, blainehilton.

I have some practice in working with TINI. So I have some advices for you.

1. Tune your build command, you have to add HTTP classes, they are not included in the Core API
    (see <TINISDK>/docs/BuildDependency.txt for details).
    Here is the build file for my test class, which source I provide at the end of this question.

    Compiling classes:
javac.exe -classpath C:\Projects\TINISDK\tini1.13\bin\tiniclasses.jar -d bin -target 1.1 *.java

    Building .tini module.
java -classpath C:\Projects\TINISDK\tini1.13\bin\tini.jar BuildDependency -x C:\Projects\TINISDK\tini1.13\bin\owapi_dep.txt -p C:\Projects\TINISDK\tini1.13\bin\owapi_dependencies_TINI.jar;C:\Projects\TINISDK\tini1.13\bin\modules.jar;bin -f bin -o MiniBrowser.tini -d C:\Projects\TINISDK\tini1.13\bin\tini.db -add HTTP

    -f parameter is about the path to your compiled classes.
    About  -add  parameter see <TINISDK>/docs/BuildDependency.txt.

    After that copy *.tini file to the TINI and run, e.g.
java MiniBrowser.tini


//Example from The TINI Specification and Developer's Guide  Don Loomis
import java.net.*;
import java.io.*;
import com.dalsemi.tininet.TININet;

class MiniBrowser {
      public static void main( String[] args ) {
            try {
                  URL u = new URL(args[0]);
                  InputStream in = u.openConnection().getInputStream();

                  byte[] content = new byte[512];
                  int count = 0;
                  do {
                        count = in.read(content);
                        System.out.write(content, 0, count);
                  } while (count != -1);
            } catch ( Exception e ) {
                  System.out.println( e.getMessage() );
                  e.printStackTrace();
            }
      }
}


2. Visit http://discuss.dalsemi.com/ for more info on TINI.


Regards.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question