tags:

views:

152

answers:

4

For Example in shell script:

_CLASSPATH =.
for jar in lib/*.jar
do
    _CLASSPATH=${_CLASSPATH}:${jar}
done

How can I dynamically build a Java classpath in Perl?

A: 

Not Perl code (no code = no bugs :) but doesn't

export CLASSPATH=.:lib/*

work? In my bash script that starts Java app I set this and app can "see" all .jars I want, but you can set it "globally":

mn@test:~# export CLASSPATH=.:/usr/local/jars/*
mn@test:~# echo $CLASSPATH
.:/usr/local/jars/*
mn@test:~# ls /usr/local/jars/*.jar
/usr/local/jars/activation.jar ...

mn@test:/home# cat show_cp.java
public class show_cp
{
    public static void main(String[] args)
        {
        System.out.println(System.getProperty("java.class.path", "."));
        }
}

mn@test:/home# java show_cp
.:/usr/local/jars/postgresql-8.4-701.jdbc4.jar:/usr/local/jars/RmiJdbc.jar:/usr/local/...

EDIT:

You can use wildcards in CLASSPATH as described in Setting the class path


And Perl code to join file names:

my @files = glob "/jars/*.jar";
my $cp = join(":", @files);
print($cp)
Michał Niklas
-1 for untested code (specifically: the shell code) where it is trivial to try.
daxim
Can you please give more info what is untested? My Java apps works with `export CLASSPATH=.:/usr/local/jars/*`, so I asked if similar setting work in asker environment.
Michał Niklas
`_CLASSPATH=.:lib/*jar`, when interpreted by the shell, does not do what you think it does. It will not be in accord with the result from the code in joe's question. (Hint: the `:` seperators are important, you indeed pay attention to them in the Perl answer, why not in the shell answer, too?) By formulating your answer as a question you even admit to not having it tested. Simply trying your code before you post can save you the trouble.
daxim
No, my Java apps work from bash on Linux with `export CLASSPATH=.:/usr/local/jars/*`. On Windows I use .bat with `SET CLASSPATH=.;c:/jars/*`. I think this is handled by Java environment because with such setting my Java application report classpath with (Windows example): `class path: C:\test\test;C:\jars\xmlParserAPIs.jar;C:\jars\activation.jar`. By formulating question I showed that it works for my, with my environment. Other versions of JDK can work differently then version I use. In my Java env you can use wildcards in CLASSPATH and it works.
Michał Niklas
OK. I edited answer and changed `_CLASSPATH=.:lib/*jar` -> `export CLASSPATH=.:lib/*` (jar part was not necessary)
Michał Niklas
+4  A: 

As always with Perl, there's more than one way to do it, but one compact way is this:

$_CLASSPATH = join(":", ".", glob("lib/*.jar"));

If you want to set an environment variable you may need to make that:

$ENV{_CLASSPATH} = join(":", ".", glob("lib/*.jar"));
psmears
+3  A: 
my $_CLASSPATH = join(":", ".", glob("lib/*.jar"));
$ENV{CLASSPATH} = $_CLASSPATH;

NOTE: If you're in a web server environment, especially one that has shared Perl interpreter like mod_perl, always localize your $ENV{} assignments to avoid unpleasantness: local $ENV{CLASSPATH}=$_CLASSPATH;

DVK
What about "." ?
Vincent Robert
Ehh... didn't notice... I'm usually never using "." in CLASSPATH so didn't realize it was there. Thx
DVK
A: 

You can try:

$CP = '.';
foreach(<lib/*.jar>) {
  $CP .= ":$_";
}
$ENV{'_CLASSPATH'} = $CP;
codaddict