Solved

how to invoke a Tagsoup from a java code???

Posted on 2007-04-01
20
399 Views
Last Modified: 2012-06-27
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+str);
           // I want to invoke java -jar tagsoup-1.0.4.jar http://moneycentral.msn.com/detail/stock_quote?Symbol=ca"; > ca.xml
        }
        in.close();
        }    
}
0
Comment
Question by:badour_ma
[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
  • 9
  • 6
  • 5
20 Comments
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18832235
0
 

Author Comment

by:badour_ma
ID: 18832295
thanks MArk,
but I have error:
com\psol\xbe2\Final.java:21: 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();
    }
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18832399
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 18832401
>> in.close();

Put that in a finally block.
0
 

Author Comment

by:badour_ma
ID: 18832450
the error fixed but
      Runtime.getRuntime ().exec("java -jar tagsoup-1.0.4.jar"+ url+" > ca.xml");
not excuted!!!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18832468
Where is the JAR file located? Is it in the same directory from where this class file is being run?
0
 

Author Comment

by:badour_ma
ID: 18832477
yes!!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18832559
What is the output you get?
0
 

Author Comment

by:badour_ma
ID: 18832572
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18832664
use --files flag instead of the >.

java -jar tagsoup-1.0.4.jar --files http://moneycentral.msn.com/detail/stock_quote?Symbol=ca"
0
 

Author Comment

by:badour_ma
ID: 18832680
I should output the reselt to ca.xml
0
 

Author Comment

by:badour_ma
ID: 18832688
also java -jar tagsoup-1.0.4.jar --files http://moneycentral.msn.com/detail/stock_quote?Symbol=ca" did not work
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18832690
I am unfamiliar with tagsoup, but you can also redirect the standard output:

System.setOut( ... );

http://www.jcreator.com/forums/index.php?showtopic=773
0
 

Author Comment

by:badour_ma
ID: 18832763
I tried to use it but I had the following error

com\psol\xbe2\TextAreaOutputStream.java:54: <identifier> expected
System.setOut( out );
             ^
com\psol\xbe2\TextAreaOutputStream.java:57: <identifier> expected
System.setErr( out );
             ^
com\psol\xbe2\TextAreaOutputStream.java: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" );

}
0
 
LVL 10

Accepted Solution

by:
ADSLMark earned 500 total points
ID: 18832866
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
    {
        //Read stock symbols
        String symbol;
        BufferedReader in = new BufferedReader(new FileReader(file));

        while((symbol = in.readLine()) != null)
        {
            //Create url
            String url = base + symbol;
            System.out.println(url);

            //Redirect standard output, execute tagsoup and restore standard output
            Process p = Runtime.getRuntime().exec("java -jar tagsoup-1.1.jar "+url);
            InputStream std = p.getInputStream();
            FileOutputStream out = new FileOutputStream(symbol+".xml");

            boolean done = false;
            while(!done)
            {
                try
                {
                    p.exitValue();
                    done = true;
                }
                catch(IllegalThreadStateException itse)
                {
                    try { Thread.sleep(20); } catch(InterruptedException ie) {}
                }

                // Do whatever you want with the output
                while (std.available() > 0)
                    out.write(std.read());
            }
        }

        in.close();
    }
}
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18832886
Don't forget to correct the version number of the tag-soup jar.
Mark
0
 

Author Comment

by:badour_ma
ID: 18833144
Thanks aloooooot Mark :-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18833162
So no points for telling Runtime.getRuntime ()....
0
 

Author Comment

by:badour_ma
ID: 18837832
Hi Mark,
Can u please explain why you do this

boolean done = false;
            while(!done)
            {
                try
                {
                    p.exitValue();
                    done = true;
                }
                catch(IllegalThreadStateException itse)
                {
                    try { Thread.sleep(20); } catch(InterruptedException ie) {}
                }

                // Do whatever you want with the output
                while (std.available() > 0)
                    out.write(std.read());
            }
        }

0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18839102
Keeps the stream from the tagsoup streaming to the file, until the process is finished.

Mark
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

Suggested Solutions

Title # Comments Views Activity
junit example issue 2 56
maximize the sum of fractions 33 75
Help with Progress 4gl Rounding Function 6 60
Java ArrayList and if statement 2 55
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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

734 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