title: Running Java on Server from Django
body: <p>for a research project at school I need to run java executables from a web interface. I'm using Django because I have some familiarity with it (but not much). The website will display a list of keys from a file outputted by the first java program. The user will select two of the keys and hit "Run". Then, the Java executable will take those specified from the website and run a sorting algorithm, and output the results to another file (which will need to be rendered onto the website). </p>
  
  <p>The approach I'm using now is to use the <code>subprocess</code> call from the view to run a shell script which handles the compilation and running of the java files. <strong>However,</strong> when I run the shell file through Django, I can see that the shell file is being executed, but the Java code is never run. If I run the shell file locally, the Java code compiles and runs fine.</p>
  
  <p>Now, even if there is a way to get this to work, it's still pretty convoluted and seems like a security risk. Is there a better way to do this from a top-level design sense? </p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>#!/bin/bash                                                                                                                      
  echo HelloFromServerShell                                                                                                        
  JAVAFILE="/home/hduser/Documents/HelloWord.java"                                                                                 
  JAVAEXEC="/home/hduser/Documents/"                                                                                               
  JAVACLASS="HelloWord"                                                                                                            
  
  javac $JAVAFILE                                                                                                                  
  java -cp $JAVAEXEC $JAVACLASS
  </code></pre>
  
  <p>and</p>
  
  <pre><code>import java.io.*;                                                                                                                
  public class HelloWord {                                                                                                         
      public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
              PrintWriter writer = new PrintWriter("output.txt", "UTF-8");                                                     
              writer.println("O brave new world!");                                                                            
              writer.close();                                                                                                  
      }                                                                                                                        
  } 
  </code></pre>
  
  <p>views.py:</p>
  
  <pre><code>def java_button(request):                                                                                                        
      subprocess.call(["/home/hduser/Documents/JavaRun.sh"], shell=True)                                                           
      subprocess.call(["mkdir", "/home/hduser/Documents/test"], shell=False)                                                       
      template = loader.get_template('file_chooser.html')                                                                          
      context = {                                                                                                                  
      }                                                                                                                            
      return HttpResponse(template.render(context,request)) 
  </code></pre>
  
  <p>Thanks!</p>