LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Java code to implement "wget" to save a web page in linux (http://www.linuxquestions.org/questions/programming-9/java-code-to-implement-wget-to-save-a-web-page-in-linux-791219/)

Kakarot_Rathish 02-24-2010 04:42 AM

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

}
}

smoker 02-24-2010 01:45 PM

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

tuxdev 02-24-2010 02:15 PM

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.

Kakarot_Rathish 03-06-2010 12:18 AM

in a shell,it works fine and it saves the page in the home directory

Quote:

Originally Posted by smoker (Post 3875415)
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


Kakarot_Rathish 03-07-2010 11:10 PM

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

Kakarot_Rathish 03-08-2010 02:02 AM

when i removed pr.waitFor(),it worked just fine.
thank you

Quote:

Originally Posted by Kakarot_Rathish (Post 3889772)
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
----------



All times are GMT -5. The time now is 02:57 AM.