简体   繁体   English

如何从jar或目录加载类

[英]How to load classes from a jar or a directory

I have found code to load classes from a directory, and code to load from a jar. 我找到了从目录加载类的代码,以及从jar加载的代码。 My project needs to do both - use a directory when it is stand alone, and use a jar when it is jarred up for other projects. 我的项目需要同时执行这两项工作-在单独使用目录时使用目录,在将其震撼用于其他项目时使用jar。

Right now I test if the jar exists and use one function, otherwise I use the other. 现在,我测试jar是否存在并使用一个功能,否则我使用另一个功能。 And while the code is vaguely similar, I can't see how to unify them. 虽然代码有些相似,但是我看不到如何统一它们。

It seems to me there must be some way that works in both cases.... 在我看来,在两种情况下都必须有某种方法可以起作用。

You should NOT do this by hand. 您不应该手动执行此操作。 Use appropriate classpath depending on where it is running. 根据运行的位置使用适当的类路径。

Running on stand-alond 独立运行

java -classpath <path-to-folder> <main-class>

Elsewhere 别处

java -classpath <path-to-jar> <other-main-class>

But why can't you use the jar when running stand-alone? 但是为什么在独立运行时不能使用jar?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM