How do you programatically 'relative import' a directory of jar files?

早过忘川 提交于 2019-12-08 06:13:20

问题


I am using Drools Planner which ships with 21 Jar files in a directory binaries. For example the drools-core-5.3.0.Final.jar would provide org.drools.someClasses.

The included examples run it in command line by running an all-inclusive command:

mainClasspath=
for i in binaries/*.jar; do mainClasspath=${mainClasspath}:$i; done

mainClass=org.drools.planner.examples.app.DroolsPlannerExamplesApp
$JAVA_HOME/bin/java -Xms256m -Xmx512m -server -cp ${mainClasspath} ${mainClass} $*

I am developing the program in commandline. But the final application (which just adds a HTTP interface) runs in a JSP container, not from a commandline. Hence I need to programmatically import the jar files into my project.

Question:How do I programatically import the the jar files?

Do I rather have to specify the binaries path in the environment variable?


Update

I did this to tell Java that globally, extra classes may be found in the same directory, meant by the . and /home/jesvin/dev/drools/binaries. Note that : is the separator in Linux.

declare -x CLASSPATH='.:/home/jesvin/dev/drools/binaries' 

You can do it per execution instance as per Miserable Variable's answer.

Finally, in a Tomcat deployment, I give it a HTTP interface, deploying it as per havexz's answer.


回答1:


If the jars need to be in global scope:

Well if you want these jars to be available to all apps on the server try putting them in

`<tomcat_install_dir>/lib`

EDIT: @Geoffery comment: If you have access to

<tomcat_install_di>/conf/catalina.properties

then you can added your own common jars dir like:

common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,${catalina.base}/your_common_jar_folder/*.jar

(See carefully at the end, I had added a new folder)

But either way, if the jars meant to be at global scope they should be in any of above folders. Else you have to put other common jars for logging and jdbc etc with every app.

If the jars need to be in app scope

And if you want these jars to be used only one specific app then put them in

`<your_web_app>/WEB-INF/lib`

Note: You can put the <your_web_app>/WEB-INF/lib of your development folder and if you are using the right tools then it will make these jars part of your .war file.

SIDE NOTE: Since you are running java program from command line too and having issues adding dependency jars. Sometime back I wrote a shell script for this purpose, hope this will help you too.

Small snippet of it:

Run Java easily from shell command

Usage: easyrunjava [-c <jar_name/jar_dir>,<jar_name/jar_dir>...] [-m <email_id1>,<email_id2>...] [-p <prop_file1>,<prop_file2>...] class_name [args_to_program]

Example:
`easyrunjava -p .,./dependency -m user.name@xyz.com com.example.HelloWorld`



回答2:


Are you trying to put all jars in a folder in classpath? import is the wrong word then.

Newer versions of java allow '*' as classpath:

$JAVA_HOME/bin/java -Xms256m -Xmx512m -server -cp 'binaries/*' ${mainClass} $*

should work. Note binaries/* is in single quotes to prevent shell expansion.



来源:https://stackoverflow.com/questions/8937409/how-do-you-programatically-relative-import-a-directory-of-jar-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!