WebFaction
Community site: login faq

I'm trying to use python's subprocess module's call function to execute a java file, that will then write a text file to a pre-defined directory. When I pass the command manually to the command line it works, but when I try to use python to launch the command it doesn't. Does anyone know what about the subprocess module would cause this difference in behavior? Is there a way to get around it?

Edit:

I'm using django 1.4, but I'm certain that is not where the problem lies. I use the call function from python's subprocess module to call a java file from the command line, but even though the word for word argument passed to subprocess.call works manually from the command line outside the view function, it does not work when called by the view.

I've added the relevant code from my django view function here:

    ...
    if form.is_valid():
        form_data = form.cleaned_data
        # here I store the form data as a plaintext file
        # in a database, which works perfectly fine    
        ... 
        # here I execute the java file on the plain text from the
        # form data, first constructing some arguments for the java            
        command_text = ("java -jar JavaFile.jar " +
                        FileOutputPath + " " + # the path to which I'll write the output file
                        FileInputPath + " " + # the path from which I'll read the input file
                        *args) # other miscellaneous arguments I need to pass
        subprocess.call(command_text,  cwd='/path/to/directory/containing/javaFile')
        # then here I update the database entry to record the java file's output,
        # and redirect to a new url            
        return HttpResponseRedirect(redirect_url)

As I've said, I'm positive the problem is specifically with subprocess.call(), and some difference between it and the command line; however, being less familiar with the module and C's system module around which subprocess is a wrapper, I'm unsure as to what the issue exactly could be.

asked 03 Feb '13, 20:37

nalourie
1113
accept rate: 0%

edited 03 Feb '13, 23:53

I am running into the same problem... I can run the .jar directly from putty... but I can't from a view... in my computer it runs perfectly.

(18 Feb '16, 17:19) renesdl

Are you using this in pure python or behind Apache/mod_wsgi?

Can you paste the python code to reproduce the issue here? If not you can submit a support ticket so we can look directly.

permanent link

answered 03 Feb '13, 20:48

johns
5.4k412
accept rate: 23%

You're not using subprocess.call correctly. command_text should be a list of arguments, not a single string.

Something like this should work:

command_text = ['java', '-jar', 'JavaFile.jar', FileOutputPath, FileInputPath,] + args
subprocess.call(command_text, cwd='/path/to/directory/containing/javaFile')
permanent link

answered 18 Feb '16, 23:00

seanf
12.2k42136
accept rate: 37%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×337
×20
×1

question asked: 03 Feb '13, 20:37

question was seen: 8,023 times

last updated: 18 Feb '16, 23:00

                              
WEBFACTION
REACH US
SUPPORT
LEGAL
© COPYRIGHT 2003-2021 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM