aspose file tools
The moose likes Java in General and the fly likes Calling python function from java . NEED HELP URGENT Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Calling python function from java . NEED HELP URGENT" Watch "Calling python function from java . NEED HELP URGENT" New topic
Author

Calling python function from java . NEED HELP URGENT

Geetu Kumari
Greenhorn

Joined: Mar 10, 2008
Posts: 5
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
Marshal

Joined: Mar 22, 2005
Posts: 35237
    
    7
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).


Android appsImageJ pluginsJava web charts
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32654
    
    4
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

Joined: Jul 08, 2003
Posts: 24054
    
  13

There's also a rather nice Java implementation of Python called Jython, which you can embed directly in your Java program.


[Jess in Action][AskingGoodQuestions]
Geetu Kumari
Greenhorn

Joined: Mar 10, 2008
Posts: 5
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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Calling python function from java . NEED HELP URGENT
 
Similar Threads
Calling python function from java . NEED HELP URGENT
How to run .java file from Runnable
compiling a file from the GUI
How to capture events from vbs script in java
waitFor method of a process object not working