Java jar 无法解析类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19814355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java jars unable to resolve class
提问by Bean
I'm relatively new to Java development...I thought I understood how class path works, but I must be missing something.
我对 Java 开发比较陌生……我以为我了解类路径的工作原理,但我一定遗漏了一些东西。
Using Groovy, I have 2 external classes referenced by imports. When I execute the script, I get "unable to resolve class" errors. I have the jars for these in the same directory as the script. If I un-jar them, the script works, but jar'd up the script does not.
使用 Groovy,我有 2 个由导入引用的外部类。当我执行脚本时,出现“无法解析类”错误。我将这些 jars 放在与脚本相同的目录中。如果我 un-jar 它们,脚本会工作,但 jar 脚本不会。
What am I missing here?
我在这里缺少什么?
EXAMPLE
例子
import org.apache.log4j.Logger
Logger log = Logger.getLogger("ldap_delete")
log.info("This is an informative log entry")
Putting log4j-1.2.17.jar in the same directory as the .groovy script does not work. Setting cp to . does not work. I have to do:
将 log4j-1.2.17.jar 放在与 .groovy 脚本相同的目录中不起作用。将 cp 设置为 . 不起作用。我要做:
groovy -cp log4j-1.2.17.jar myscript.groovy
to get the class to load.
让类加载。
采纳答案by wxkevin
Would need to see the script for your specific problem but in general you need to set the classpath when running a java program if it is dependent upon other jars. Here is an example:
需要查看您的特定问题的脚本,但通常您需要在运行 java 程序时设置类路径,如果它依赖于其他 jars。下面是一个例子:
java -cp /path/to/something.jar;/another/path/else.jar my.package.Program
java -cp /path/to/something.jar;/another/path/else.jar my.package.Program