Solved

Requesting a URL

Posted on 2004-09-15
9
250 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
  • 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
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.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
servlet  URL Rewriting 1 42
add projects t working set in maven 2 16
Crystal Reports Licensing Questions 4 33
Java Eclipse Loop 3 20
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

792 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