calling python from java,see logical error, why the URL string not return as result

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rspvsanjay
    New Member
    • Sep 2016
    • 21

    calling python from java,see logical error, why the URL string not return as result

    import java.io.*;
    class FirstClass
    {
    public static void main(String a[])
    {
    try{
    String prg = "import sys\ndef main():\ndef get_text(str):" +"\n return str"+
    "\ndef get_url_text(ur l):"+"\n return get_text(url)"
    +"\ndef main():"+"\n get_url_text(sy s.argv[1])"+"\nif __name__ == \"__main__\" : main()";

    BufferedWriter out = new BufferedWriter( new FileWriter("tes t234.py"));

    out.write(prg);

    out.close();

    final String command = "python test234.py http://www.tutorialspo int.com/java/";

    final Process sirDesc = Runtime.getRunt ime().exec(comm and);

    final java.io.InputSt ream inStreamBase = sirDesc.getInpu tStream();

    final java.io.InputSt reamReader inStreamISR = new java.io.InputSt reamReader(inSt reamBase);

    final java.io.Buffere dReader inStream = new java.io.Buffere dReader(inStrea mISR);

    String inLine;

    final StringBuilder result = new StringBuilder() ;

    while (null != (inLine = inStream.readLi ne()))

    result.append(i nLine);

    System.out.prin tln("\n value is :\n "+result.toStri ng());
    }catch(Exceptio n e){}
    }
    }



    i want given URL to python program as results
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    I already discussed this in your previous thread: https://bytes.com/topic/java/answers...-result-string

    Will you please tell us what you have done, and what errors you are seeing?

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      By the way, your use of the try/catch is suppressing output of all errors. The catch block does nothing, so any error information that was available is lost.

      Please try removing the try/catch block and re-running your code - I expect that the messages will be valuable to you.

      Comment

      • rspvsanjay
        New Member
        • Sep 2016
        • 21

        #4
        // thank sir my program is working perfectly there was logical //error of print statment not used in python program
        // where do i put python file if i am using Eclipse IDE ??
        import java.io.*;
        class FirstClass
        {
        public static void main(String a[])throws Exception
        {

        //String prg = "import sys\ndef get_text(str):" +"\n return str"+
        //"\ndef get_url_text(ur l):"+"\n return get_text(url)"
        //+"\ndef main():"+"\n print get_url_text(sy s.argv[1])"+"\nif __name__ == \"__main__\" : main()";

        //BufferedWriter out = new BufferedWriter( new FileWriter("tes t234.py"));

        //out.write(prg);

        //out.close();

        final String command = "python sir_desc.py https://docs.oracle.com/javase/tutorial/";

        final Process sirDesc = Runtime.getRunt ime().exec(comm and);

        final java.io.InputSt ream inStreamBase = sirDesc.getInpu tStream();

        final java.io.InputSt reamReader inStreamISR = new java.io.InputSt reamReader(inSt reamBase);

        final java.io.Buffere dReader inStream = new java.io.Buffere dReader(inStrea mISR);

        String inLine;

        final StringBuilder result = new StringBuilder() ;

        while (null != (inLine = inStream.readLi ne()))

        result.append(i nLine);

        System.out.prin tln("\nvalue is :\n "+result.toStri ng());
        }
        }

        Comment

        • rspvsanjay
          New Member
          • Sep 2016
          • 21

          #5
          where do i put python file if I working with Eclipse IDE ?

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            You might want to create a Python project as part of your workspace.

            Once this is done, you can use at least two easy methods to access the file:
            1. Change the directory of execution (cd)
              System.setPrope rty("user.dir", "your-dir");
            2. Include the full path in the process creation.
              final String command = "python /your-dir/test234.py http://www.tutorialspo int.com/java/";


            Luck!

            Comment

            Working...