这是我的代码:
import java.net.URL;
import java.net.URLClassLoader;
public class App {
public static void main(String[] args) {
System.out.println("java.class.path="+System.getProperty("java.class.path"));
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url: urls){
System.out.println(url.getFile());
}
}
}
当我在Eclipse中使用LWJGL和Slick2d库运行它时,我得到了正如预期的那样:
java.class.path=/home/the-genius/workspace/classpath/bin:/home/the-geniu/workspace
/libs/slick/lib/slick.jar:/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar
/lwjgl.jar:/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar/lwjgl_util.jar:
/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar/jinput.jar
/home/the-genius/workspace/classpath/bin/
/home/the-genius/workspace/libs/slick/lib/slick.jar
/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar/lwjgl.jar
/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar/lwjgl_util.jar
/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar/jinput.jar
但是,当我将其导出为可运行的jar并通过
执行时java -cp /home/the-genius/workspace/classpath/bin:/home/the-geniu/workspace
/libs/slick/lib/slick.jar:/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar
/lwjgl.jar:/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar/lwjgl_util.jar: -jar app.jar
我得到了
java.class.path=classpath.jar
/home/the-genius/classpath.jar
有没有理由发生这种情况?我该如何解决?
我在Ubuntu上运行,如果这有所不同。我也尝试过使用OpenJDK-7和Sun Java-7。
答案 0 :(得分:3)
如果同时使用-cp
和-jar
选项,前者将被忽略。要修复它,您可以在没有-jar
的情况下运行它(将您的jar文件添加到类路径并调用主类:java -cp app.jar App
)或将类路径添加到jar清单文件中。