473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(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 = "python test234.py http://www.tutorialspoint.com/java/";

final Process sirDesc = Runtime.getRuntime().exec(command);

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



i want given URL to python program as results
Sep 24 '16 #1

✓ answered by Oralloy

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.

5 1233
Oralloy
988 Expert 512MB
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?
Sep 24 '16 #2
Oralloy
988 Expert 512MB
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.
Sep 24 '16 #3
// 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(url):"+"\n return get_text(url)"
//+"\ndef main():"+"\n print 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 = "python sir_desc.py https://docs.oracle.com/javase/tutorial/";

final Process sirDesc = Runtime.getRuntime().exec(command);

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("\nvalue is :\n "+result.toString());
}
}
Sep 26 '16 #4
where do i put python file if I working with Eclipse IDE ?
Sep 26 '16 #5
Oralloy
988 Expert 512MB
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.setProperty("user.dir", "your-dir");
  2. Include the full path in the process creation.
    final String command = "python /your-dir/test234.py http://www.tutorialspoint.com/java/";

Luck!
Sep 26 '16 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Travis Berg | last post by:
I'm running into a problem when trying to perform a callback to a Python function from a C extension. Specifically, the callback is being made by a pthread that seems to cause the problem. If I...
1
by: Stephen Kellett | last post by:
Hello everyone, I'm trying to do something in C calling Python and its failing. I'd be grateful if you could take a look and hopefully you have an answer. What I'm trying to do is determine...
4
by: muser | last post by:
Can anyone run this program through their compiler or if they can see a logical error please point it out. I have my tutor working on it at the moment but I would rather a less ambigious response...
6
by: Jens Thiede | last post by:
Q 1: I've not been using C++ much yet, but is it true that C++ is an unfriendly language to code in *OR*, as I think, is it just a case of C people, coding in C and calling it C++? Q 2: What...
0
by: ReeceSeeSharp | last post by:
Having difficulty trying to get this code in .NET (C#) to print out this string EXACTLY as it prints out within Java using a) the MD5 hashing algorithm and B). In Java here's how a string "ididnot"...
2
JavaStudent07
by: JavaStudent07 | last post by:
H:\>c:\j2sdk1.4.1\bin\javac.exe BubbleSort.java H:\>c:\j2sdk1.4.1\bin\java.exe BubbleSort Please input the name of the file from which the data will be acquired. Be sure to use the entire path....
0
varuns
by: varuns | last post by:
if i need to call a python function from c, i can use PyImport _Import() python-c API. Following code shows calling python function "add1" from python module "def1" int add(int x, int y) { ...
1
by: CAG | last post by:
Hi, In my application, i'm storing sql query as value of table field in database. Like in my NewHireEmployee table, value of JoinDateQuery field can be (select hiredate from employeeInfo) or...
3
by: anchitgood | last post by:
I have tried a lot but unable to correct just 1 logical error in the following code. This error is in the for loop, when fine and minutes are printed as the garbage values. Please check it out: ...
1
by: Yokil | last post by:
Hey I'm doing this program for a project. It's running and everything. But I can't seem to get it to go further on. It terminates suddenly. Is something wrong with the logic? Here's the program ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.