rspvsanjay,
When you have code to share, please put it in [code] brackets. You will see a "button" just above the text input area.
Your code looks something like this:
-
//thank you sir Oralloy
-
//plese tell me where i have done mistake in below program
-
import java.io.*;
-
class test234
-
{
-
public static void main(String a[])
-
{
-
try{
-
String prg = "import sys\ndef main():\ndef get_text(str):"+"\n return str"+
-
"\ndef get_url_text(url):"+"\n return get_text(url)"
-
+"\ndef main():"+"\n get_url_text(sys.argv[1])"+"\nif __name__ == \"__main__\": main()";
-
-
BufferedWriter out = new BufferedWriter(new FileWriter("test234.py"));
-
-
out.write(prg);
-
-
out.close();
-
-
//final String command = "test234.py http://www.tutorialspoint.com/java/";
-
-
//final ProcessBuilder pb = new ProcessBuilder();
-
ProcessBuilder pb = new ProcessBuilder("python","test234.py","http://www.tutorialspoint.com/java/");
-
-
final Process sirDesc = pb.start();
-
-
final java.io.InputStream inStreamBase = sirDesc.getInputStream();
-
-
final java.io.InputStreamReader inStreamISR = new java.io.InputStreamReader(inStreamBase);
-
-
final java.io.BufferedReader inStream = new java.io.BufferedReader(inStreamISR);
-
-
String inLine;
-
-
final StringBuilder result = new StringBuilder();
-
-
while (null != (inLine = inStream.readLine()))
-
-
result.append(inLine);
-
-
System.out.println("\n value is :\n "+result.toString());
-
}catch(Exception e){}
-
}
-
}
-
Since you can see what I see, would you answer your question? This needs to be reasonably formatted for people to read and understand. In the future, please do so.
Please read:
https://bytes.com/forum/faq.php?faq=posting_guidelines
Now that we've discussed that, follow-up questions are quite reasonable, but you have to tell me your problem and what you've tried. When all you do is give me code and ask what is wrong, I have no idea.
That said, I will make a few guesses:
- You are dynamically creating and executing a python program from within the Java.
- Your Python file executes perfectly.
- Your code executes with no exceptions.
- The code output is just the header (" value is :") followed by an empty string.
- You read and understood the Java documentation for the ProcessBuilder and Process classes.
- The try/catch block is only there to suppress error print-out when the program executes correctly.
- You understand how sub-processes are created.
- You understand how file handles and pipes are used to communicate with sub-processes.
Since I think that I may have overstepped my bounds in helping you with what looks like it is homework, I will try to help you diagnose and understand what you have:
- Verify that the Java code correctly compiles.
- Verify that the Python program is correctly created.
- Verify that the created Python program executes as required; do this by running it from the command line.
- Run your Java program and verify that no exceptions or errors occur.
- If you are using Eclipse, IntelliJ, or other IDE execute your program one line at a time and verify that good things happened.
- If you are using command line only, then you might want to put a printout after each statement in your program.
- In general, verify that each line of your code executes correctly, and the resulting machine state is exactly what you expect.
Here are a few links that I think you should read. The first has the example which was used as a basis of my first reply to you.
- http://docs.oracle.com/javase/7/docs...ssBuilder.html
- Or: http://docs.oracle.com/javase/7/docs...ssBuilder.html
- http://docs.oracle.com/javase/7/docs...g/Process.html
- http://docs.oracle.com/javase/7/docs...Exception.html
Once you've done that, I think you will have answered your own question, and solved the problem.
* IDE - Integrated Development Environment