Becky has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl program that needs to call a java program called 'getseq.java'. The perl program and the java program are in different directories.

If I cd to the directory containing getseq.java on the command line, then run the perl program from here, then the following code works fine ('getseq.java' needs 3 arguments - a number like BC000898 and the names of 2 output files to write to).

system "java getseq BC000898 C:/testjava/DNA2.txt C:/testjava/protein2.txt";

However, if I try to run the perl program from outside this directory, it doesn't work; I get the "exception in thread main..." error. Why??

Replies are listed 'Best First'.
Re: system call to a java program
by Dog and Pony (Priest) on Oct 22, 2002 at 10:02 UTC
    Either do the classpath thingy as others pointed out, or do what you should *always* do with system: use complete and absolute paths.
    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
Re: system call to a java program
by robartes (Priest) on Oct 22, 2002 at 09:48 UTC
    What's the particular exception? Class not found? You probably need to add the path to getseq.class in your java classpath. You can do this by passing java a -classpath option, or defining the CLASSPATH environment variable.

    Also, you're probably better of asking this on a more Java oriented forum, as your question is more about Java than it is about Perl.

    CU
    Robartes-

Re: system call to a java program
by coreolyn (Parson) on Oct 22, 2002 at 11:00 UTC

    In addition to using the above suggestions make sure that JAVA_HOME is set in your environment. While win32 boxes will probably have this Unix boxes will not necesarily have this available to your environment by default.

    %ENV is your friend when running Java code from Perl.

    coreolyn
Re: system call to a java program
by lachoy (Parson) on Oct 22, 2002 at 11:56 UTC

    Actually if the file is called getseq.java, you need to compile it first. (Generally you do this with:

    javac getseq.java

    although if it uses extra libraries you'll have to add those to the classpath:

    javac -classpath foo.jar getseq.jar

    Then, assuming that the java binary is in your PATH and the getseq.class file is in /path/to/getseq, you can run:

    system "java -classpath /path/to/getseq getseq dir1 dir2"

    But this is getting OT for Perlmonks :-)

    Chris
    M-x auto-bs-mode

Re: system call to a java program
by Anonymous Monk on Oct 22, 2002 at 13:06 UTC
    As other people said you are not finding the java code. There are actually two problems. Perl runs 'system' in the directory you started perl from (frequently annoying) and it seems as though your classpath is only './'. I have not found a way to tell perl what cwd is when running system that works. If you specify the java class with full path you will get an exception b/c java expects that the directories you tell it about are package definitions.
Re: system call to a java program
by Ananda (Pilgrim) on Oct 22, 2002 at 09:55 UTC
    I guess you need to set the classpath and associate the directory containing getseq.class to the "classpath" OR the "path" environment variables. Anandatirtha