调用“java -jar MyFile.jar”使用其他classpath选项 [英] Call "java -jar MyFile.jar" with additional classpath option

查看:95
本文介绍了调用“java -jar MyFile.jar”使用其他classpath选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含所有编译内容的jar文件。另外,我的ant构建脚本将所需的lib复制到子文件夹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?

推荐答案

您使用 -jar -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> c>在复制依赖项之后 之前构建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”使用其他classpath选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆