你如何编程'相对导入'jar文件目录? [英] How do you programatically 'relative import' a directory of jar files?

查看:150
本文介绍了你如何编程'相对导入'jar文件目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Drools Planner,它在目录二进制文件中附带了21个Jar文件。例如
drools-core-5.3.0.Final.jar 将提供 org.drools.someClasses

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} $*






我正在命令行中开发程序。但最终的应用程序(只是添加了一个HTTP接口)在JSP容器中运行,而不是从命令行运行。因此,我需要以编程方式将jar文件导入到我的项目中。

问题:我如何以编程方式导入jar文件?

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

我是否宁愿在环境变量中指定二进制文件路径?

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

我这样做是为了告诉Java全局,额外的课程可能可以在同一目录中找到,由 / home / jesvin / dev / drools / binaries 表示。请注意,是Linux中的分隔符。

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' 

您可以按照 Miserable Variable的答案

最后,在Tomcat部署中,我给它一个HTTP接口,按 havexz的答案

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

推荐答案

如果罐子需要在全球范围内:

如果你想让这些罐子对服务器上的所有应用程序都可用,请尝试将它们放入

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

`<tomcat_install_dir>/lib`

编辑: @Geoffery评论:如果您有权访问

@Geoffery comment: If you have access to

<tomcat_install_di>/conf/catalina.properties

然后你可以添加自己的公共罐子像:

then you can added your own common jars dir like:

常见。装载机= $ {catalina.base} / lib中,$ {catalina.base} / lib中/ *。罐子,$ {的catalina.home} / lib中,$ {的catalina.home} / lib中/ *。罐子,$ {卡塔利娜。 base} / your_common_jar_folder / * .jar

(最后仔细查看,我添加了一个新文件夹)

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

但无论如何,如果罐子意味着在全球范围内,它们应该在上述任何文件夹中。此外,你必须为每个应用程序安装其他常见的jar用于日志记录和jdbc等。

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.

如果jar需要在app范围内

如果你想让这些罐子只用一个特定的应用程序然后把它们放进去

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

`<your_web_app>/WEB-INF/lib`

注意:你可以把您的开发文件夹的< your_web_app> / WEB-INF / lib 如果您使用正确的工具,那么它将使这些罐子成为您的 .war file。

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注意:因为您也从命令行运行java程序并且遇到问题添加依赖jar。有时为此目的我写了一个shell脚本,希望这对你也有帮助。

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.

它的小片段:

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`

这篇关于你如何编程'相对导入'jar文件目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆