Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run python within java?

Status
Not open for further replies.

abc73

Programmer
Apr 28, 2004
89
US
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
 
I will not try to understand why you would want to do something in python from Java ... in any case, if you can run your python command from the shell/command line console, then you can run it by using Runtime.exec().

--------------------------------------------------
Free Database Connection Pooling Software
 
You can use Jython which is a Python implementation in 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.


Note: Jython does at this time lag quite seriously to Python development
 
I am trying to execute some code written in python inside java. But so far no success. Here is the sample I am trying to run. Please see the below code:

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:
java.io.IOException: CreateProcess: C:\Documents and Settings\TEST\Desktop\PYTHON\c.py error=193

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[])
Now here is the java code that I am trying to execute:


Code:
import java.io.*;

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();
}
}
}
And here is the python code that I am trying to run inside java:


Code:
import os
from stat import *

print "SUCCESS"
Thanks for the help.
 
How do you run the code from the command line ? Post that.

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:
Process p = Runtime.getRuntime().exec("cmd /c dir \"C:/Documents and Settings\"");
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = br.readLine()) != null) {
	System.out.println(line);
}

--------------------------------------------------
Free Database Connection Pooling Software
 
I got execution part working. I am trying to make a read only file to writable. As I can't find a method to set a read only file to writable in java so I was trying to use python and invoke it inside java. Right now python code is being executed but it's npt changing the file permission. If I run the python code stand alone it changes the file permission but invoking inside java doesn't change the file permission. Can you please help. Here is my python code:

Code:
import os
from stat import *

os.chmod("test.txt",666)
print "SUCCESS"

And here is the java code, modified according to the above suggestions (Thanks):
Code:
import java.io.*;

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.
 
Why are you doing p.waitFor(); twice ?

Perhaps you should change the file to its full path in this line :

os.chmod("test.txt",666)

--------------------------------------------------
Free Database Connection Pooling Software
 
Well, if you have already lost cross-platform issue, I don't think you need Python anymore.

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
 
Nice suggestion on the attrib Dian ! My DOS is not up to much !

I bet that other API does something similar to this :

Code:
		String file = "C:/permissions.txt";

		// 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
 
PeDidn't specify the full path in this line :

os.chmod("test.txt",666)

Thanks for all the help.
 
He he, when I started with this computer thingie, there was no mouse to right-click anything, so command line was the only option. Even today, give a MS-DOS command or Unix shell and I'll give you my mouse :)

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



 
Diancecht :

>>>> 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
 
Anyways I did triee the following earlier too but somehow doesn't work: (file = permissions to be changed)

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
 
This will make your file read-write :
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
 
thanks my problem spacing. I got it thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top