Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling java from python

I'm writing a script to compile a .java file from within python But the error

import subprocess
def compile_java(java_file):
    cmd = 'javac ' + java_file 
    proc = subprocess.Popen(cmd, shell=True)

compile_java("Test.java")

Error:

javac is not recognized as an internal or external command windows 7

I know how to fix the problem for CMD on windows. But how do I solve it for python? What I mean is: how do i set the path?

like image 598
tgoossens Avatar asked Aug 10 '11 19:08

tgoossens


2 Answers

proc = subprocess.Popen(cmd, shell=True, env = {'PATH': '/path/to/javac'})

or

cmd = '/path/to/javac/javac ' + java_file 
proc = subprocess.Popen(cmd, shell=True)
like image 100
agf Avatar answered Sep 28 '22 01:09

agf


You can also send arguments as well:

            variableNamePython = subprocess.Popen(["java", "-jar", "lib/JavaTest.jar", variable1, variable2, variable3], stdout=subprocess.PIPE)
            JavaVariableReturned = variableNamePython.stdout.read()
            print "The Variable Returned by Java is: " + JavaVariableReturned

The Java code to receive these variables will look like:

public class JavaTest {
    public static void main(String[] args) throws Exception {
        String variable1 = args[0];
        String variable2 = args[1];
        String variable3 = args[2];
like image 27
3ck Avatar answered Sep 27 '22 23:09

3ck