LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2010, 05:42 AM   #1
Kakarot_Rathish
Member
 
Registered: Sep 2008
Posts: 35
Blog Entries: 1

Rep: Reputation: 15
Java code to implement "wget" to save a web page in linux


This is the code i used, there is no error in execution but no file is bein saved in the working directory.
I'm new in java,so just started learning.
if some one can help me out with this wil be really great

Thanx in advance



public class Hel
{
public static void main(String args[])
throws IOException
{
try
{
Process q = Runtime.getRuntime().exec("export http_proxy='http://it06106036:it06106036@172.16.0.254:8080'");
Process p = Runtime.getRuntime().exec(" wget -U MyBrowser/1.0 http://59.165.190.44/exams/results1/btechNEW/BT55displayNEW.php?regno=mkageit026&Submit=Submit");

BufferedReader r= new BufferedReader(new InputStreamReader(q.getInputStream())); //juz added this to call the constructors
r.readLine();
BufferedReader s= new BufferedReader(new InputStreamReader(p.getInputStream()));
s.readLine();
}catch(IOException ioe)
{
System.exit(1);
}

}
}
 
Old 02-24-2010, 02:45 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Does
Code:
wget -U MyBrowser/1.0 http://59.165.190.44/exams/results1/btechNEW/BT55displayNEW.php?regno=mkageit026&Submit=Submit
work outside the program ?

Have you tested it in a shell ?

Also have a read of this
http://www.coderanch.com/t/454874/Ja...a-code#2026903
then this
http://www.javaworld.com/javaworld/j...ps.html?page=1
 
Old 02-24-2010, 03:15 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
The two execs run in completely separate environments.

Anyhow, this is completely the wrong way to go about this. Use an actual HTTP client API, like Apache's. Or *maybe* a JNI'd interface to cURL.
 
Old 03-06-2010, 01:18 AM   #4
Kakarot_Rathish
Member
 
Registered: Sep 2008
Posts: 35

Original Poster
Blog Entries: 1

Rep: Reputation: 15
in a shell,it works fine and it saves the page in the home directory

Quote:
Originally Posted by smoker View Post
Does
Code:
wget -U MyBrowser/1.0 http://59.165.190.44/exams/results1/btechNEW/BT55displayNEW.php?regno=mkageit026&Submit=Submit
work outside the program ?

Have you tested it in a shell ?

Also have a read of this
http://www.coderanch.com/t/454874/Ja...a-code#2026903
then this
http://www.javaworld.com/javaworld/j...ps.html?page=1
 
Old 03-08-2010, 12:10 AM   #5
Kakarot_Rathish
Member
 
Registered: Sep 2008
Posts: 35

Original Poster
Blog Entries: 1

Rep: Reputation: 15
import java.io.*;
import java.io.IOException;

public class Hel
{
public static void main(String args[])
throws IOException
{
try
{

String cmd = "ls -al";
Runtime run = Runtime.getRuntime();
Process pr = run.exec(cmd);
pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line=buf.readLine())!=null) {
System.out.println(line);}

}catch(IOException ioe)
{
System.exit(1);
}

}
}

i tried to just implement ls command....
and during that when i compile i get the following error.I have added try and catch but still iam gettin unhandled exception .plz help


Station71:~/RESA/wget# javac Hel.java
----------
1. ERROR in Hel.java (at line 25)
pr.waitFor();
^^^^^^^^^^^^
Unhandled exception type InterruptedException
----------
 
Old 03-08-2010, 03:02 AM   #6
Kakarot_Rathish
Member
 
Registered: Sep 2008
Posts: 35

Original Poster
Blog Entries: 1

Rep: Reputation: 15
when i removed pr.waitFor(),it worked just fine.
thank you

Quote:
Originally Posted by Kakarot_Rathish View Post
import java.io.*;
import java.io.IOException;

public class Hel
{
public static void main(String args[])
throws IOException
{
try
{

String cmd = "ls -al";
Runtime run = Runtime.getRuntime();
Process pr = run.exec(cmd);
pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line=buf.readLine())!=null) {
System.out.println(line);}

}catch(IOException ioe)
{
System.exit(1);
}

}
}

i tried to just implement ls command....
and during that when i compile i get the following error.I have added try and catch but still iam gettin unhandled exception .plz help


Station71:~/RESA/wget# javac Hel.java
----------
1. ERROR in Hel.java (at line 25)
pr.waitFor();
^^^^^^^^^^^^
Unhandled exception type InterruptedException
----------
 
  


Reply

Tags
java, save, webpage, wget


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"wget -p" problem with PHP page dedeco Linux - General 2 06-04-2009 07:58 PM
Wget or cURL code for checking changes to a web page? ewingtux Programming 2 12-16-2008 05:46 PM
my web browser "mozilla fire fox" isn't rendering the page, rather opening the page amolgupta Linux - Software 2 07-26-2005 01:41 AM
can't log into squirrelmail. error code "You must be logged in to access this page." rioguia Linux - Software 3 11-02-2004 11:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration