badour_ma
asked on
how to invoke a Tagsoup from a java code???
hi,
I want to excute this command from the command promt
java -jar tagsoup-1.0.4.jar http://moneycentral.msn.com/detail/stock_quote?Symbol=ca"; > ca.xml
into my java code
package com.psol.xbe2;
import java.io.*;
public class Final
{
public static void main (String args[]) throws IOException
{
String ur="http://moneycentral.msn.com/detail/stock_quote?Symbol=";
BufferedReader in = new BufferedReader(new FileReader("ex1.txt"));
String str;
while ((str = in.readLine()) != null)
{
System.out.println(ur=ur+s tr);
// I want to invoke java -jar tagsoup-1.0.4.jar http://moneycentral.msn.com/detail/stock_quote?Symbol=ca"; > ca.xml
}
in.close();
}
}
I want to excute this command from the command promt
java -jar tagsoup-1.0.4.jar http://moneycentral.msn.com/detail/stock_quote?Symbol=ca"; > ca.xml
into my java code
package com.psol.xbe2;
import java.io.*;
public class Final
{
public static void main (String args[]) throws IOException
{
String ur="http://moneycentral.msn.com/detail/stock_quote?Symbol=";
BufferedReader in = new BufferedReader(new FileReader("ex1.txt"));
String str;
while ((str = in.readLine()) != null)
{
System.out.println(ur=ur+s
// I want to invoke java -jar tagsoup-1.0.4.jar http://moneycentral.msn.com/detail/stock_quote?Symbol=ca"; > ca.xml
}
in.close();
}
}
ASKER
thanks MArk,
but I have error:
com\psol\xbe2\Final.java:2 1: cannot resolve symbol
symbol : method exec (java.lang.String)
location: class java.lang.System
System.exec("java -jar tagsoup-1.0.4.jar"+ url+" > ca.xml");
^
package com.psol.xbe2;
import java.io.*;
import java.util.*;
import java.lang.*;
public class Final
{
private final static String file = "ex1.txt";
private final static String base = "http://moneycentral.msn.com/detail/stock_quote?Symbol=";
public static void main(String[] args)
throws IOException
{
String symbol;
BufferedReader in = new BufferedReader(new FileReader(file));
while((symbol = in.readLine()) != null)
{
String url = base + symbol;
System.out.println(url);
System.exec("java -jar tagsoup-1.0.4.jar"+ url+" > ca.xml");
}
in.close();
}
}
but I have error:
com\psol\xbe2\Final.java:2
symbol : method exec (java.lang.String)
location: class java.lang.System
System.exec("java -jar tagsoup-1.0.4.jar"+ url+" > ca.xml");
^
package com.psol.xbe2;
import java.io.*;
import java.util.*;
import java.lang.*;
public class Final
{
private final static String file = "ex1.txt";
private final static String base = "http://moneycentral.msn.com/detail/stock_quote?Symbol=";
public static void main(String[] args)
throws IOException
{
String symbol;
BufferedReader in = new BufferedReader(new FileReader(file));
while((symbol = in.readLine()) != null)
{
String url = base + symbol;
System.out.println(url);
System.exec("java -jar tagsoup-1.0.4.jar"+ url+" > ca.xml");
}
in.close();
}
}
Its Runtime.getRuntime ().exec ()
And you might want to store the command "java -jar ...." in a properties file and load it from there, so that you can keep changing it on runtime without modifying your source code.
And you might want to store the command "java -jar ...." in a properties file and load it from there, so that you can keep changing it on runtime without modifying your source code.
>> in.close();
Put that in a finally block.
Put that in a finally block.
ASKER
the error fixed but
Runtime.getRuntime ().exec("java -jar tagsoup-1.0.4.jar"+ url+" > ca.xml");
not excuted!!!
Runtime.getRuntime ().exec("java -jar tagsoup-1.0.4.jar"+ url+" > ca.xml");
not excuted!!!
Where is the JAR file located? Is it in the same directory from where this class file is being run?
ASKER
yes!!
What is the output you get?
ASKER
use --files flag instead of the >.
java -jar tagsoup-1.0.4.jar --files http://moneycentral.msn.com/detail/stock_quote?Symbol=ca"
java -jar tagsoup-1.0.4.jar --files http://moneycentral.msn.com/detail/stock_quote?Symbol=ca"
ASKER
I should output the reselt to ca.xml
ASKER
also java -jar tagsoup-1.0.4.jar --files http://moneycentral.msn.com/detail/stock_quote?Symbol=ca" did not work
I am unfamiliar with tagsoup, but you can also redirect the standard output:
System.setOut( ... );
http://www.jcreator.com/forums/index.php?showtopic=773
System.setOut( ... );
http://www.jcreator.com/forums/index.php?showtopic=773
ASKER
I tried to use it but I had the following error
com\psol\xbe2\TextAreaOutp utStream.j ava:54: <identifier> expected
System.setOut( out );
^
com\psol\xbe2\TextAreaOutp utStream.j ava:57: <identifier> expected
System.setErr( out );
^
com\psol\xbe2\TextAreaOutp utStream.j ava:60: <identifier> expected
System.out.println( "Hello World" );
^
3 errors
/*
*
*
* @(#) TextAreaOutputStream.java
*
*/
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
/**
* An output stream that writes its output to a javax.swing.JTextArea
* control.
*
* @author Ranganath Kini
* @see javax.swing.JTextArea
*/
public class TextAreaOutputStream extends OutputStream {
private JTextArea textControl;
/**
* Creates a new instance of TextAreaOutputStream which writes
* to the specified instance of javax.swing.JTextArea control.
*
* @param control A reference to the javax.swing.JTextArea
* control to which the output must be redirected
* to.
*/
public TextAreaOutputStream( JTextArea control ) {
textControl = control;
}
/**
* Writes the specified byte as a character to the
* javax.swing.JTextArea.
*
* @param b The byte to be written as character to the
* JTextArea.
*/
public void write( int b ) throws IOException {
// append the data as characters to the JTextArea control
textControl.append( String.valueOf( ( char )b ) );
}
// Create an instance of javax.swing.JTextArea control
JTextArea txtConsole = new JTextArea();
// Now create a new TextAreaOutputStream to write to our JTextArea control and wrap a
// PrintStream around it to support the println/printf methods.
PrintStream out = new PrintStream( new TextAreaOutputStream( txtConsole ) );
// redirect standard output stream to the TextAreaOutputStream
System.setOut( out );
// redirect standard error stream to the TextAreaOutputStream
System.setErr( out );
// now test the mechanism
System.out.println( "Hello World" );
}
com\psol\xbe2\TextAreaOutp
System.setOut( out );
^
com\psol\xbe2\TextAreaOutp
System.setErr( out );
^
com\psol\xbe2\TextAreaOutp
System.out.println( "Hello World" );
^
3 errors
/*
*
*
* @(#) TextAreaOutputStream.java
*
*/
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
/**
* An output stream that writes its output to a javax.swing.JTextArea
* control.
*
* @author Ranganath Kini
* @see javax.swing.JTextArea
*/
public class TextAreaOutputStream extends OutputStream {
private JTextArea textControl;
/**
* Creates a new instance of TextAreaOutputStream which writes
* to the specified instance of javax.swing.JTextArea control.
*
* @param control A reference to the javax.swing.JTextArea
* control to which the output must be redirected
* to.
*/
public TextAreaOutputStream( JTextArea control ) {
textControl = control;
}
/**
* Writes the specified byte as a character to the
* javax.swing.JTextArea.
*
* @param b The byte to be written as character to the
* JTextArea.
*/
public void write( int b ) throws IOException {
// append the data as characters to the JTextArea control
textControl.append( String.valueOf( ( char )b ) );
}
// Create an instance of javax.swing.JTextArea control
JTextArea txtConsole = new JTextArea();
// Now create a new TextAreaOutputStream to write to our JTextArea control and wrap a
// PrintStream around it to support the println/printf methods.
PrintStream out = new PrintStream( new TextAreaOutputStream( txtConsole ) );
// redirect standard output stream to the TextAreaOutputStream
System.setOut( out );
// redirect standard error stream to the TextAreaOutputStream
System.setErr( out );
// now test the mechanism
System.out.println( "Hello World" );
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Don't forget to correct the version number of the tag-soup jar.
Mark
Mark
ASKER
Thanks aloooooot Mark :-)
So no points for telling Runtime.getRuntime ()....
ASKER
Hi Mark,
Can u please explain why you do this
boolean done = false;
while(!done)
{
try
{
p.exitValue();
done = true;
}
catch(IllegalThreadStateEx ception itse)
{
try { Thread.sleep(20); } catch(InterruptedException ie) {}
}
// Do whatever you want with the output
while (std.available() > 0)
out.write(std.read());
}
}
Can u please explain why you do this
boolean done = false;
while(!done)
{
try
{
p.exitValue();
done = true;
}
catch(IllegalThreadStateEx
{
try { Thread.sleep(20); } catch(InterruptedException
}
// Do whatever you want with the output
while (std.available() > 0)
out.write(std.read());
}
}
Keeps the stream from the tagsoup streaming to the file, until the process is finished.
Mark
Mark
See: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html
Mark