A
A
asm0dey2010-11-05 16:46:03
Python
asm0dey, 2010-11-05 16:46:03

Calling python3 script from java?

I use uTorrent for Linux on my home server to download torrents.
It has one big (for me) drawback - it does not have the ability to determine in which folder to download this particular torrent.
There is a python3 script that helps to solve this problem.
Here is a thread of his discussion.
I wanted to write a small desktop face to it, in which you can enter the absolute path on the server where to save, select the torrent to download and enter the login password for the server.
Faced with the fact that I don’t know how to call a python script from java.
Please help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
apangin, 2010-11-05
@apangin

Is Runtime.exec() not suitable?

M
mardy_bum, 2010-11-05
@mardy_bum

Try like this:

Process p = Runtime.getRuntime().exec("sh script.py");

InputStream stdout = p.getInputStream();
InputStream stderr = p.getErrorStream();
InputStreamReader isr = new InputStreamReader(stdout);
InputStreamReader isrerr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
BufferedReader brerr = new BufferedReader(isrerr);

String line = null;

System.out.println("OUTPUT:");
while ((line = br.readLine()) != null) 
   System.out.println(line);
System.out.println();

System.out.println("ERROR:");
while ((line = brerr.readLine()) != null) 
   System.out.println(line);
System.out.println();

p.waitFor();

M
mardy_bum, 2010-11-05
@mardy_bum

Jython and JEPP do not support Python 3, so exec() is the best option.
The file already has a shebang (#!/usr/bin/env python3) so it can be run just like a script.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question