如何创建在动态位置使用外部 jar 文件的可运行 jar

发布于 2024-08-18 13:13:10 字数 1862 浏览 7 评论 0原文

我编写了一个大约 300 KB 的应用程序。 它使用的 jar 文件(库)大约需要 10 megz。

这些库 jar 文件在我编写的其他应用程序中使用,因此我希望将它们位于外部中心位置(含义 - 同一台计算机中某些硬盘驱动器上的路径)。 希望库文件的路径可以通过系统环境变量来定义。

我寻找这个问题的良好解决方案,但找不到可靠的解决方案。 我猜测解决方案将包括 ant 构建,也许还包括对 MANIFEST 文件的一些更改。尝试这样做,但据我了解,清单文件中的类路径不能包含任何类型的变量。

先感谢您!

编辑:在与下面的好心人讨论之后。我现在使用以下命令提示符。还是不行。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏日落 2024-08-25 13:13:10

注意-cp和-jar不能同时使用。当您执行 java -jar 时,java 期望引用的 jar 在 MANIFEST.MF 文件内声明。我认为你的命令行应该可以将你的 jar 放入“cp”中,并在命令行上显式调用主类。

如果您担心大小,另一个解决方案是使用 JNLP 部署应用程序并使用 Pack200.

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.

灰色世界里的红玫瑰 2024-08-25 13:13:10

只需将您的 CLASSPATH 环境变量设置为指向您的 .jar 即可。
或者将 -classpath 选项传递给 java。

以下是Windows 下的操作方法

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

ι不睡觉的鱼゛ 2024-08-25 13:13:10

您可以使用 Java Web Start启动您的应用程序?这将允许您在安装时外部化您的依赖项。

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

如日中天 2024-08-25 13:13:10

希望库文件的路径可以通过系统环境变量来定义。

这不是 CLASSPATH 的用途吗?

例如,我将所有相关的 jar 保存在 ${HOME}/jars 中。我的 .bashrc 包含

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 和 Cygwin,Unix 非常相似(并且也必须使用 Windows 内置函数来完成此操作)。因此,如果应用程序需要 jar,我只需将依赖项放入 jar 中,下一个 shell 会将其包含在其类路径中。

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.

木緿 2024-08-25 13:13:10

如果使用 -jar 选项运行程序,则不会使用 CLASSPATH 变量和 -cp 选项。文件指出:

使用此选项时,JAR 文件是所有用户类的源,其他用户类路径设置将被忽略

也就是说,类路径必须在 MANIFEST 文件中的 Class-Path: 行中给出。

不要使用 -jar 选项,而是将 JAR 添加到 -cp 选项的 CLASSPATH 中,并使用主类名

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>

以这个长类路径开头,最好使用 CLASSPATH 变量...

If you use the -jar option to run the program, neither the CLASSPATH variable nor the -cp options are 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文