Java as a scripting language

When developing in Python or Perl, it is very common to start with an executable script, and to edit/run/edit/run.  Java is slowed down by the cycle of edit/compile/run.  Here’s a proof of concept of coding in Java like you do in Python.

First, the code to compile and run the script. This is unrefactored, and uses the system command, which is basically a new shell, so not what the end product should look like:


#include 
#include 
#include 
#include 


void execute(const char * file_name){
  char * execute_command = "java ";
  int command_size = + strlen( execute_command ) +  strlen(file_name) + 1 ;
  char * command = malloc(command_size);
  sprintf(command,"%s %s", execute_command , file_name);
  system(command);
  free(command);
}
void compile(const char * file_name){
  char * compile_command = "javac ";
  int command_size = + strlen( compile_command ) +  strlen(file_name) + 1 ;
  char * command = malloc(command_size);
  sprintf(command,"%s %s", compile_command , file_name);
  system(command);
  free(command);
}
char *  make_java_file_name(const char * source){
  int command_size = strlen(source) + strlen(".java") + 1 ;
  char * command = malloc(command_size);
  sprintf(command,"%s.java", source);
  return command;
}
void copy_file(const char * source, const char * dest ){
  char * cp_command = "tail -n +2 ";
  int command_size =
    strlen( cp_command ) + 2 +
    strlen(source) + 1 +
    strlen(dest) + 1 ;
  char * command = malloc(command_size);
  sprintf(command,"%s %s > %s", cp_command , source, dest);
  system(command);
  free(command);
}

int main(int argc,  char ** argv){
  printf("Got argc=%d\n",argc);
  int i;
  for (i =0; i < argc; i ++){
    printf("[%d]:%s\n",i,argv[i]);
  }

  if (argc < 2){
    return;
  }

  char * file_name = basename (argv[1]);
  char * java_file_name = make_java_file_name(file_name);

  copy_file(file_name, java_file_name);
  compile(java_file_name);
  free(java_file_name);
  execute(file_name);
}


I save that in a file called javaexec.c and compile it.

Now to call it. The following file is named simply runexec

#!/home/ayoung/devel/javahacks/javaexec/javaexe
public class runexec{

       public static void main(String[] args){
              System.out.println("Running on Empty!");
       }
}

chmod it and execute:

[ayoung@ayoung javaexec]$ ./runexec
Got argc=2
[0]:/home/ayoung/devel/javahacks/javaexec/javaexec
[1]:./runexec
Running on Empty!

OK. It works in the simple case. Things to do: path handling, classpath, argument passing, cleanup the C code and maybe get rid of all those "systems".

9 thoughts on “Java as a scripting language

  1. Interesting.

    I like to solve the edit/compile/run with a simple ant build.xml such that “ant run” does the build step too. You can also setup vim so that typing “make” does the building and running if you like.

  2. Yeah, but for a short script, you need to: fire up eclipse, create a project, etc. I’m trying to come up with a low overhead approach for simple Sys Admin type tasks, but that could then work into a large project once they grow out of control.

  3. Because Groovy is not type safe, and refactoring from Ruby to Java would be painful. Scripts start off small, but quickly grow out of control, which is why I prefer languages that have strong Syntax .

  4. I love it! You could extend this concept to C and C++ as well.

    All you need to add is parsing for import and #include statements and we’ll never need a Makefile again.

  5. If you are compiling stuff anyway, why not just use C++? Need a rich class library? That’s what Qt is for. And even kdelibs. Even with no GUI, you can use QtCore and kdecore, and auxiliary modules such as QtXml, just fine.

  6. The same approach could be taken with C++, and I think it would be quite powerful. But I think that I’d still want it for Java. Why not both.

Leave a Reply

Your email address will not be published. Required fields are marked *