views:

363

answers:

5

Hi, I've programmed an application that take about 300 kilobytes. The jar files that it uses (library) take about 10 megz.

These library jar files are used in other applications I wrote and so I would like to have them located in an external central location (Meaning - A path on some hard drive in the same computer). Hopefully, the path of the library files can be defined by a system environment variable.

I looked for good solutions to this issue and couldn't find something solid. I'm guessing the solution will include an ant build and perhaps some alteration to the MANIFEST file. Tried to do this, but to my understand, a class-path in the manifest file cannot contain variables of any kind.

Thank you in advance!

EDIT: After discussing this with kind people below. I now use the following command prompt. Still doesn't work.

java -cp C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/woodstox.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/activation.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/FastInfoset.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/http.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jaxb-api.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jaxb-impl.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jaxb-xjc.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jaxws-api.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jaxws-rt.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jaxws-tools.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jsr173_api.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jsr181-api.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jsr250-api.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/mimepull.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/resolver.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/saaj-api.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/saaj-impl.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/stax-ex.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/streambuffer.jar;C:/Work/svn/svn55/CommonLibs/lib/ws/jsr262-ri/jmxws.jar;C:/Work/svn/svn55/CommonLibs/lib/ws/jsr262-ri/jmxws-doctool.jar;C:/Work/svn/svn55/CommonLibs/lib/ws/jsr262-ri/wiseman-core.jar -Dcom.MyCompany.log.directory=C:/Oracle/Middleware/user_projects/domains/MyCompany/servers/AdminServer/logs -Dcom.MyCompany.config.directory=C:/temp/Apache/Config -jar jmx2snmp.jar
+2  A: 

Can you use Java Web Start to launch your application? This will allow you to externalize your dependencies at install time.

Kevin
+3  A: 

Just set your CLASSPATH environment variable to point to your .jar s.
Or pass -classpath option to java.

Here is how to do it under windows

Mykola Golubyev
If I run jar file, doesn't the classpath derive from MANIFEST.MF?I tried removing any classpath mentions from MANIFEST.MF and then running this command:java -cp C:/work/svn/svn1/CommonLibs/lib/ws/jsr262-ri/*;C:/work/svn/svn1/CommonLibs/lib/jmx/jaxws-ri/*;C:/work/svn/svn1/CommonLibs/lib/snmp/* -jar jmx2snmp.jar Didn't work. It couldn't find the required JAR files, even though it is in one of the specified directories.
Ben
You should point to the jar file and not to the directory with it.
Mykola Golubyev
Thanks. Tried It.Unfortunately, This did not work for me either.I specified the JAR files each and still no good.Very Weird.. I might be missing something.. Working from Windows, tried to reverse the slashes in the path too. Nothing works..
Ben
What do you use as jars separation symbol. Please add your CLASSPATH to the question
Mykola Golubyev
java -cp C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/woodstox.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/activation.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/FastInfoset.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/http.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jaxb-api.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jaxb-impl.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jaxb-xjc.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jaxws-api.jar;C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/jaxws-rt.jar -jar jmx2snmp.jar
Ben
Try to wrap it in quotes (") and/or change ';' to ':'
Mykola Golubyev
@Mykola : that's not the problem, if using the `-jar` option, CLASSPATH and `-cp` are NOT used.
Carlos Heuberger
A: 

Hopefully, the path of the library files can be defined by a system environment variable.

Isn't this what CLASSPATH is for?

For example, i keep all relevant jars in ${HOME}/jars. My .bashrc contains

CLASSPATH=$(cygpath -w $HOME/jars/log4j-1.2.15.jar)
for j in $HOME/jars/*.jar
        do
        h=$(cygpath -w $j)
        CLASSPATH=$CLASSPATH\;$h
done
export CLASSPATH

This is Windows and Cygwin, Unix would be straightforwardly similar (and there has to be away of doing this using Windows builtins, too). So if an application needs a jar, i just drop the dependency into jars and the next shell will include it in its classpath.

wallenborn
+3  A: 

Pay attention that you can not use -cp and -jar at the same time. When you do java -jar, java expects the referenced jar to be declared inside the MANIFEST.MF file. I think your command line should work putting your jar inside "cp", and calling explicitely the main class on the command line.

Another solution if you are concerned with the size is to deploy your application with JNLP and compress your jars with Pack200.

Damien B
Thank you! that worked!
Ben
A: 

If you use the -jar option to run the program, neither the CLASSPATH variable nor the -cp options are not used. Documentation states:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored

That is, the class path must be given in a Class-Path: line in the MANIFEST file.

Instead of using the -jar option, add the JAR to the CLASSPATH of -cp option and use the main class name to start

java -cp C:/Work/svn/svn55/CommonLibs/lib/jmx/jaxws-ri/woodstox.jar;
    ...;
    C:/Work/svn/svn55/CommonLibs/lib/ws/jsr262-ri/wiseman-core.jar;jmx2snmp.jar 
    -Dcom.MyCompany.log.directory=...
    -Dcom.MyCompany.config.directory=C:/temp/Apache/Config <package.classname>

with this LONG classpath it would be better to use the CLASSPATH variable...

Carlos Heuberger
Thank you Carlos, That's exactly what I did in the end. I marked Damien's answer since he beat you to it. But thank you :-)
Ben
@Ben : that's OK, you're welcome :)
Carlos Heuberger