How to embed a perl script in a jar file to execute?

I am new to Perl, but have been programming in java for several months now (coming from C ++ background). I wrote a Perl script that parses some data logs, and now the client I work for wants a graphical interface. The GUI was created as a java applet (using Netbeans), and I would like to "embed" the perl script in my jar file as the default security function. In the future, several updates are expected for the perl script, so I want to configure it so that the whole user should do when the update arrives - this will determine the new path to the latest version of the perl script through the GUI. I already implemented this function with a file browser, and everything works fine.

The problem I am facing is something very simple, which is probably not very difficult for someone who has more java experience. Just in case, if one of the updated perl scripts that they receive in the future does not work properly, I want them to be able to use the default "w370" if they have to resort to this. When I run the applet through Netbeans, everything works fine, however, when I try to run the jar file from the command line, the program returns an error stating that it cannot find the file. I may not use the correct terminology to find a solution to this problem, but I want my jar file to execute the built-in perl script at runtime. Any suggestions are welcome. I tried putting the perl file in the same package as the java files,and call the script only by its name, but that was not so.

+5
source share
2 answers

You can access any file in the bank as a class path resource, but the problem you are about to encounter is that users may not have the perl interpreter installed.

EDIT: since you mentioned that users will have Perl runtime, then this is doable. You can try to link the contents of the file with Process.getOutputStream()or simply copy the contents to a temporary file with File.createTempFile()and pass that file name as an argument to the perl interpreter.

+3
source

, . src/main/resources/perl ( Eclipse). , Perl perl jar src/main/resources/perl. Maven, src/main/resources/perl , , Maven perl .

, jars, .

, Maven, . , , , . Perl , . Maven ,, , Java- .

. , C .

( Apache Commons IO):

public class PerlTableParser {

  private static final String RESOURCES_DIR = "src/main/resources";
  private static final String LIB_PATH = RESOURCES_DIR + "perl/";
  private static final String PERL_PARSER = "perl/parser.pl";
  private static final String PERL_CMD = String.format("perl -I %s %s",
        LIB_PATH, RESOURCES_DIR + PERL_PARSER);

  public PerlTableParser() {
    File perlCodeDir = new File(LIB_PATH);
    if (!perlCodeDir.exists()) {
        perlCodeDir.mkdirs();
    }
    File perlParserFile = new File(RESOURCES_DIR, PERL_PARSER);
    try {
        if (!perlParserFile.exists()) {
            FileUtils.copyInputStreamToFile(getClass().getClassLoader()
                    .getResourceAsStream(PERL_PARSER), perlParserFile);
        }
    } catch (IOException e) {
        MyLogger.logger.error(
                "Failed to copy Perl code to local directory " + e, e);
    }
}
+1

All Articles