views:

43

answers:

1

I have a Python CGI script from which I am trying to call a Java program to perform a task. The Java program uses JExcelAPI. When I run the Python script from the browser, it fails with error messages that it can't find the class definitions for the classes from JExcelAPI. I suppose this happens because the Python CGI script is run under the apache user, and the apache user does not have the appropriate environment variables set (namely the CLASSPATH variable). I have tried calling the program with the -classpath /path/to/JExcelAPI switch, but that does not work either. Can you help me find the way to make the apache user aware of the JExcelAPI? Is there a way to set the CLASSPATH environment variable for the apache user?

Thanks

+2  A: 

Several solutions come to mind :

  1. Create a bash script which calls the java program. You can set all the variables you like and debug on the commandline, e.g. sudo -u apache /usr/local/bin/java-task-wrapper. This simplifies calling it from a cgi considerably and the overhead of bash is negligeable compared to spinning up a JVM.

  2. Create a standalone executable jar with tools like uberjar. No more classpah issues as everything is contained : java -jar java-task-standalone.jar

  3. exec java -cp /path/to/JExcelAPI:/my/program/classes com.acme.MainClass

  4. There is usually a variant of exec which takes an additional array or hashmap to add environment variables.

Some notes:

  • setting the CLASSPATH variable globally is not done anymore because it leads to many conflicts. In a wrapper script it is Ok as possibilities to clash is reduced.

  • JVM's take a long time to start and the execution will be slow since the JIT gets no chance to do its magic. Running your script in a lightweight webserver like jetty or winstone or listening on a socket will eliminate the startup cost and enabe the JIT to make things fast.

Peter Tillemans
I used the 3rd option you suggested and it worked. Thanks!
infrared