有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么cp与jar选项一起使用时没有效果?

我用java -cp dependencyJarPath -jar MyJar.jar启动我的应用程序。但是它在dependencyJar中找不到该类,并给我java.lang.NoClassDefFoundError。然后我在代码中打印类路径,发现dependencyJarPath不在我的类路径上。然后在清单的Class-Path头中添加dependencyJarPath。使用java -jar MyJar.jar启动我的应用程序,它成功了

所以我的问题是-cp-jar一起使用时-cp会生效吗?如果它没有生效,我如何在运行jar时设置类路径,而不是设置Class-Path


共 (1) 个答案

  1. # 1 楼答案

    据此: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html

    "When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored."

    如果您不介意在启动应用程序时指定startup类,那么可以将jar添加到类路径中,然后像这样启动它

    java -cp dependencyPath;MyJar.jar My.StartUpClass