调用“java -jar MyFile.jar"带有额外的类路径选项 [英] Call "java -jar MyFile.jar" with additional classpath option
问题描述
我创建了一个包含我所有编译内容的 jar 文件.此外,我的 ant 构建脚本将所需的库复制到子文件夹libs"中.结构如下:
I created a jar file containing all my compiled stuff. Additionally my ant build script copies the required libs into a subfolder "libs". The structure looks like this:
MyProgram.jar
libs/
所以当我现在尝试运行我的程序时,我收到以下错误:
So when I try to run my program now I get the following error:
java -cp ".:/home/user/java/MyProgram/jar/libs" -jar MyProgram.jar
java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at database.PostgresQL.getConnection(PostgresQL.java:38)
at recommender.dao.Creative2IdxDAO.createCreatives2Idx(Creative2IdxDAO.java:19)
at main.Main.calculateCorrelationMatrix(Main.java:51)
at main.Main.main(Main.java:28)
java.lang.NullPointerException
at recommender.dao.Creative2IdxDAO.createCreatives2Idx(Creative2IdxDAO.java:25)
at main.Main.calculateCorrelationMatrix(Main.java:51)
at main.Main.main(Main.java:28)
为什么会发生这种情况?
Why does this happen?
推荐答案
你使用 either -jar
or -cp
,你不能将两者结合起来.如果您想在类路径上放置额外的 JAR,那么您应该将它们放在主 JAR 的清单中,然后使用 java -jar
或者将完整的类路径(包括主 JAR 及其依赖项)放入-cp
并在命令行中明确命名主类
You use either -jar
or -cp
, you can't combine the two. If you want to put additional JARs on the classpath then you should either put them in the main JAR's manifest and then use java -jar
or you put the full classpath (including the main JAR and its dependencies) in -cp
and name the main class explicitly on the command line
java -cp 'MyProgram.jar:libs/*' main.Main
(我使用 dir/*
语法告诉 java
命令将所有 .jar
文件从特定目录添加到类路径.请注意,*
必须防止被 shell 扩展,这就是我使用单引号的原因.)
(I'm using the dir/*
syntax that tells the java
command to add all .jar
files from a particular directory to the classpath. Note that the *
must be protected from expansion by the shell, which is why I've used single quotes.)
您提到您正在使用 Ant,因此对于替代清单方法,您可以使用 ant 的 <manifestclasspath>
任务 after 复制依赖项但之前 构建 JAR.
You mention that you're using Ant so for the alternative manifest approach, you can use ant's <manifestclasspath>
task after copying the dependencies but before building the JAR.
<manifestclasspath property="myprogram.manifest.classpath" jarfile="MyProgram.jar">
<classpath>
<fileset dir="libs" includes="*.jar" />
</classpath>
</manifestclasspath>
<jar destfile="MyProgram.jar" basedir="classes">
<manifest>
<attribute name="Main-Class" value="main.Main" />
<attribute name="Class-Path" value="${myprogram.manifest.classpath}" />
</manifest>
</jar>
有了这个,java -jar MyProgram.jar
将正常工作,并且还将在类路径中包含所有 libs
JAR 文件.
With this in place, java -jar MyProgram.jar
will work correctly, and will include all the libs
JAR files on the classpath as well.
这篇关于调用“java -jar MyFile.jar"带有额外的类路径选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!