File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling python function from java . NEED HELP URGENT

 
Geetu Kumari
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I want to call python function from java Class OR execute python file from java Class
if possible Please explain me with piece of code urgently .......

Thanks in advance,
Geeta

I tried for this ... but it not giving any error and not executing also .....

public class MyJavaClass
{
public static void main(String[] args)
{
try
{
System.out.println("start");
String file = "/home/geeta/Install/newEclipse/NoCrashPlease/testWebProject/src/pythonPrograms/test.py" ;
Process p =Runtime.getRuntime().exec("chmod -R 777 " + file);
p.waitFor();
p = Runtime.getRuntime().exec("python " +file);
p.waitFor();
System.out.println("end........................");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

/* test.py contain */
print 'SUCCESS'
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of Runtime.exec(String) I'd recommend to use Runtime.exec(String[]). This and other tips on using Runtime.exec -like dealing with the process's streams- are explained in detail in the article "When Runtime.exec() doesn't" by Daconta (which is really required reading when using it).
 
Campbell Ritchie
Sheriff
Pie
Posts: 47289
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:"When Runtime.exec() doesn't"
I think it's spelt "When Runtime.exec() won't", but as Ulf has said, you need to find it with Google and read it. It is a little easier now the ProcessBuilder class has been introduced, but only slightly easier. And don't go saying your request is urgent.

This question is too difficult for the beginners' forum. Moving.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also a rather nice Java implementation of Python called Jython, which you can embed directly in your Java program.
 
Geetu Kumari
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,
Ulf Dittmer and Campbell Ritchie



" When Runtime.exec() won't " It is really helpful.

Thanks again !!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic