How to run python within java?
How to run python within java?
(OP)
I want to run python command within java. Not sure how to do this. Seems like runtime.getRuntime.exec() is something that I can use. Can I run python command using getRuntime().. Please let me know. I am trying to change the file permission using python, now I need to call the python within java. Can anyone help plz.
Thank
Thank
RE: How to run python within java?
--------------------------------------------------
Free Database Connection Pooling Software
http://www.primrose.org.uk
RE: How to run python within java?
Haven't tried interaction between Jython and a host application yet but it is designed to plug into Java applications so it shouldn't be hard.
http://www.jython.org
Note: Jython does at this time lag quite seriously to Python development
RE: How to run python within java?
I get the following exception at runtime and my python is not being executed. I am on windows and python is in the path.
CODE
int java.lang.Win32Process.create(java.lang.String, java.lang.String, java.lang.String, java.io.FileDescriptor, java.io.FileDescriptor, java.io.FileDescriptor)
void java.lang.Win32Process.<init>(java.lang.String[], java.lang.String[], java.lang.String)
java.lang.Process java.lang.Runtime.execInternal(java.lang.String[], java.lang.String[], java.lang.String)
java.lang.Process java.lang.Runtime.exec(java.lang.String[], java.lang.String[], java.io.File)
java.lang.Process java.lang.Runtime.exec(java.lang.String, java.lang.String[], java.io.File)
java.lang.Process java.lang.Runtime.exec(java.lang.String, java.lang.String[])
java.lang.Process java.lang.Runtime.exec(java.lang.String)
void RuntimeTest.main(java.lang.String[])
CODE
public class RuntimeTest
{
public static void main(String[] args)
{
try
{
Runtime r = Runtime.getRuntime();
Process p = r.exec("C:\\Documents and Settings\\TEST\\Desktop\\PYTHON\\c.py");
p.waitFor();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
CODE
from stat import *
print "SUCCESS"
RE: How to run python within java?
There are several problems anyway :
1) Your use of spaces in the file to the .py file are problematic - try not to use them. If you do, you will need to encapsualte them in quotes.
2) It looks like you are trying to execute a file - where is the command to invoke the python interpreter ?
3) You need to invoke a new shell, not use the existing shell when working on Win32.
EG:
CODE
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
--------------------------------------------------
Free Database Connection Pooling Software
http://www.primrose.org.uk
RE: How to run python within java?
CODE
from stat import *
os.chmod("test.txt",666)
print "SUCCESS"
And here is the java code, modified according to the above suggestions (Thanks):
CODE
public class pythonTest
{
public static void main(String[] args)
{
try
{
Runtime r = Runtime.getRuntime();
Process p = r.exec("cmd /c C:\\c.py");
p.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
p.waitFor();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Thanks for helping me out.
RE: How to run python within java?
Perhaps you should change the file to its full path in this line :
os.chmod("test.txt",666)
--------------------------------------------------
Free Database Connection Pooling Software
http://www.primrose.org.uk
RE: How to run python within java?
You can change file permission by executing in Windows attrib +r filename, that would make things easier.
Runtime.getRuntime().exec("cmd / c attrib +R file.ext");
Btw, did you try the API someone mentioned in your other post? Did it work? I'm a bit curious about it.
Cheers.
Dian
RE: How to run python within java?
I bet that other API does something similar to this :
CODE
// RW
if (System.getProperty("os.name").indexOf("Windows") != -1) {
Runtime.getRuntime().exec("cmd /c attrib -R " +file);
} else {
Runtime.getRuntime().exec("chmod -R 775 " +file);
}
// RO
if (System.getProperty("os.name").indexOf("Windows") != -1) {
Runtime.getRuntime().exec("cmd /c attrib +R " +file);
} else {
Runtime.getRuntime().exec("chmod -R 444 " +file);
}
--------------------------------------------------
Free Database Connection Pooling Software
http://www.primrose.org.uk
RE: How to run python within java?
os.chmod("test.txt",666)
Thanks for all the help.
RE: How to run python within java?
Btw, good suggestion for the API sedj, I find it really interesting, dealing with native filesystem is always a headache when you need cross-platform, but atm I have no time to take a look at that. Will put it on the TODO list.
Cheers.
Dian
RE: How to run python within java?
>>>> He he, when I started with this computer thingie, there was no mouse to right-click anything
Double Hehehehe - when I started there was bairly a cmd shell left in Windows !!
rockets12345 :
I would dump using Python. Its bad enough shelling out to run OS commands, without shelling out to run another interpreted langauge, which will ultimately just run the OS command anyway !
--------------------------------------------------
Free Database Connection Pooling Software
http://www.primrose.org.uk
RE: How to run python within java?
Runtime.getRuntime().exec("cmd /c attrib -R " +file);
I even tried:
Runtime.getRuntime().exec("cmd /c attrib +R " +file);
and no effect, that's why I used python and it does change the file permission.
Thanks
RE: How to run python within java?
Runtime.getRuntime().exec("cmd /c attrib -R " +file);
This will make your file read-only :
Runtime.getRuntime().exec("cmd /c attrib +R " +file);
I can assure you it works ... are you passing in the whole path to the file ?
--------------------------------------------------
Free Database Connection Pooling Software
http://www.primrose.org.uk
RE: How to run python within java?